[App Search] Refactor Result Settings logic (#95531)
This commit is contained in:
parent
fe66162ef2
commit
3f86bab334
|
@ -26,20 +26,20 @@ describe('ResultSettingsLogic', () => {
|
|||
dataLoading: true,
|
||||
saving: false,
|
||||
openModal: OpenModal.None,
|
||||
nonTextResultFields: {},
|
||||
resultFields: {},
|
||||
serverResultFields: {},
|
||||
textResultFields: {},
|
||||
lastSavedResultFields: {},
|
||||
schema: {},
|
||||
schemaConflicts: {},
|
||||
};
|
||||
|
||||
const SELECTORS = {
|
||||
serverResultFields: {},
|
||||
reducedServerResultFields: {},
|
||||
resultFieldsAtDefaultSettings: true,
|
||||
resultFieldsEmpty: true,
|
||||
stagedUpdates: false,
|
||||
nonTextResultFields: {},
|
||||
textResultFields: {},
|
||||
};
|
||||
|
||||
// Values without selectors
|
||||
|
@ -138,37 +138,6 @@ describe('ResultSettingsLogic', () => {
|
|||
snippetFallback: false,
|
||||
},
|
||||
},
|
||||
// The resultFields are also partitioned to either nonTextResultFields or textResultFields
|
||||
// depending on their type within the passed schema
|
||||
nonTextResultFields: {
|
||||
bar: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
},
|
||||
textResultFields: {
|
||||
// Baz was not part of the original serverResultFields, it was injected based on the schema
|
||||
baz: {
|
||||
raw: false,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
foo: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
},
|
||||
// It stores the originally passed results as serverResultFields
|
||||
serverResultFields: {
|
||||
foo: { raw: { size: 5 } },
|
||||
// Baz was not part of the original serverResultFields, it was injected based on the schema
|
||||
baz: {},
|
||||
bar: { raw: { size: 5 } },
|
||||
},
|
||||
// The modal should be reset back to closed if it had been opened previously
|
||||
openModal: OpenModal.None,
|
||||
// Stores the provided schema details
|
||||
|
@ -230,44 +199,20 @@ describe('ResultSettingsLogic', () => {
|
|||
describe('clearAllFields', () => {
|
||||
it('should remove all settings that have been set for each field', () => {
|
||||
mount({
|
||||
nonTextResultFields: {
|
||||
foo: { raw: false, snippet: false, snippetFallback: false },
|
||||
bar: { raw: true, snippet: false, snippetFallback: true },
|
||||
},
|
||||
textResultFields: {
|
||||
qux: { raw: false, snippet: false, snippetFallback: false },
|
||||
quux: { raw: true, snippet: false, snippetFallback: true },
|
||||
},
|
||||
resultFields: {
|
||||
quuz: { raw: false, snippet: false, snippetFallback: false },
|
||||
corge: { raw: true, snippet: false, snippetFallback: true },
|
||||
},
|
||||
serverResultFields: {
|
||||
grault: { raw: { size: 5 } },
|
||||
garply: { raw: true },
|
||||
},
|
||||
});
|
||||
|
||||
ResultSettingsLogic.actions.clearAllFields();
|
||||
|
||||
expect(resultSettingLogicValues()).toEqual({
|
||||
...DEFAULT_VALUES,
|
||||
nonTextResultFields: {
|
||||
foo: {},
|
||||
bar: {},
|
||||
},
|
||||
textResultFields: {
|
||||
qux: {},
|
||||
quux: {},
|
||||
},
|
||||
resultFields: {
|
||||
quuz: {},
|
||||
corge: {},
|
||||
},
|
||||
serverResultFields: {
|
||||
grault: {},
|
||||
garply: {},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -275,44 +220,20 @@ describe('ResultSettingsLogic', () => {
|
|||
describe('resetAllFields', () => {
|
||||
it('should reset all settings to their default values per field', () => {
|
||||
mount({
|
||||
nonTextResultFields: {
|
||||
foo: { raw: true, snippet: true, snippetFallback: true },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
textResultFields: {
|
||||
qux: { raw: true, snippet: true, snippetFallback: true },
|
||||
quux: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
resultFields: {
|
||||
quuz: { raw: true, snippet: true, snippetFallback: true },
|
||||
corge: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
serverResultFields: {
|
||||
grault: { raw: { size: 5 } },
|
||||
garply: { raw: true },
|
||||
},
|
||||
});
|
||||
|
||||
ResultSettingsLogic.actions.resetAllFields();
|
||||
|
||||
expect(resultSettingLogicValues()).toEqual({
|
||||
...DEFAULT_VALUES,
|
||||
nonTextResultFields: {
|
||||
bar: { raw: true, snippet: false, snippetFallback: false },
|
||||
foo: { raw: true, snippet: false, snippetFallback: false },
|
||||
},
|
||||
textResultFields: {
|
||||
qux: { raw: true, snippet: false, snippetFallback: false },
|
||||
quux: { raw: true, snippet: false, snippetFallback: false },
|
||||
},
|
||||
resultFields: {
|
||||
quuz: { raw: true, snippet: false, snippetFallback: false },
|
||||
corge: { raw: true, snippet: false, snippetFallback: false },
|
||||
},
|
||||
serverResultFields: {
|
||||
grault: { raw: {} },
|
||||
garply: { raw: {} },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -332,22 +253,10 @@ describe('ResultSettingsLogic', () => {
|
|||
|
||||
describe('updateField', () => {
|
||||
const initialValues = {
|
||||
nonTextResultFields: {
|
||||
foo: { raw: true, snippet: true, snippetFallback: true },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
textResultFields: {
|
||||
foo: { raw: true, snippet: true, snippetFallback: true },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
resultFields: {
|
||||
foo: { raw: true, snippet: true, snippetFallback: true },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
serverResultFields: {
|
||||
foo: { raw: { size: 5 } },
|
||||
bar: { raw: true },
|
||||
},
|
||||
};
|
||||
|
||||
it('should update settings for an individual field', () => {
|
||||
|
@ -362,23 +271,10 @@ describe('ResultSettingsLogic', () => {
|
|||
expect(resultSettingLogicValues()).toEqual({
|
||||
...DEFAULT_VALUES,
|
||||
// the settings for foo are updated below for any *ResultFields state in which they appear
|
||||
nonTextResultFields: {
|
||||
foo: { raw: true, snippet: false, snippetFallback: false },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
textResultFields: {
|
||||
foo: { raw: true, snippet: false, snippetFallback: false },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
resultFields: {
|
||||
foo: { raw: true, snippet: false, snippetFallback: false },
|
||||
bar: { raw: true, snippet: true, snippetFallback: true },
|
||||
},
|
||||
serverResultFields: {
|
||||
// Note that the specified settings for foo get converted to a "server" format here
|
||||
foo: { raw: {} },
|
||||
bar: { raw: true },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -417,6 +313,49 @@ describe('ResultSettingsLogic', () => {
|
|||
});
|
||||
|
||||
describe('selectors', () => {
|
||||
describe('textResultFields', () => {
|
||||
it('should return only resultFields that have a type of "text" in the engine schema', () => {
|
||||
mount({
|
||||
schema: {
|
||||
foo: 'text',
|
||||
bar: 'number',
|
||||
baz: 'text',
|
||||
},
|
||||
resultFields: {
|
||||
foo: { raw: true, rawSize: 5 },
|
||||
bar: { raw: true, rawSize: 5 },
|
||||
baz: { raw: true, rawSize: 5 },
|
||||
},
|
||||
});
|
||||
|
||||
expect(ResultSettingsLogic.values.textResultFields).toEqual({
|
||||
baz: { raw: true, rawSize: 5 },
|
||||
foo: { raw: true, rawSize: 5 },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('nonTextResultFields', () => {
|
||||
it('should return only resultFields that have a type other than "text" in the engine schema', () => {
|
||||
mount({
|
||||
schema: {
|
||||
foo: 'text',
|
||||
bar: 'number',
|
||||
baz: 'text',
|
||||
},
|
||||
resultFields: {
|
||||
foo: { raw: true, rawSize: 5 },
|
||||
bar: { raw: true, rawSize: 5 },
|
||||
baz: { raw: true, rawSize: 5 },
|
||||
},
|
||||
});
|
||||
|
||||
expect(ResultSettingsLogic.values.nonTextResultFields).toEqual({
|
||||
bar: { raw: true, rawSize: 5 },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('resultFieldsAtDefaultSettings', () => {
|
||||
it('should return true if all fields are at their default settings', () => {
|
||||
mount({
|
||||
|
@ -499,11 +438,45 @@ describe('ResultSettingsLogic', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('reducedServerResultFields', () => {
|
||||
it('filters out fields that do not have any settings', () => {
|
||||
describe('serverResultFields', () => {
|
||||
it('returns resultFields formatted for the server', () => {
|
||||
mount({
|
||||
serverResultFields: {
|
||||
foo: { raw: { size: 5 } },
|
||||
resultFields: {
|
||||
foo: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
snippet: true,
|
||||
snippetFallback: true,
|
||||
snippetSize: 3,
|
||||
},
|
||||
bar: {},
|
||||
baz: {
|
||||
raw: false,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(ResultSettingsLogic.values.serverResultFields).toEqual({
|
||||
foo: {
|
||||
raw: { size: 5 },
|
||||
snippet: { fallback: true, size: 3 },
|
||||
},
|
||||
bar: {},
|
||||
baz: {},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('reducedServerResultFields', () => {
|
||||
it('returns server formatted fields with empty settings filtered out', () => {
|
||||
mount({
|
||||
resultFields: {
|
||||
foo: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
},
|
||||
bar: {},
|
||||
},
|
||||
});
|
||||
|
|
|
@ -27,11 +27,9 @@ import {
|
|||
areFieldsAtDefaultSettings,
|
||||
areFieldsEmpty,
|
||||
clearAllFields,
|
||||
clearAllServerFields,
|
||||
convertServerResultFieldsToResultFields,
|
||||
convertToServerFieldResultSetting,
|
||||
resetAllFields,
|
||||
resetAllServerFields,
|
||||
splitResultFields,
|
||||
} from './utils';
|
||||
|
||||
|
@ -44,12 +42,9 @@ interface ResultSettingsActions {
|
|||
schema: Schema,
|
||||
schemaConflicts?: SchemaConflicts
|
||||
): {
|
||||
serverResultFields: ServerFieldResultSettingObject;
|
||||
resultFields: FieldResultSettingObject;
|
||||
schema: Schema;
|
||||
schemaConflicts: SchemaConflicts;
|
||||
nonTextResultFields: FieldResultSettingObject;
|
||||
textResultFields: FieldResultSettingObject;
|
||||
};
|
||||
clearAllFields(): void;
|
||||
resetAllFields(): void;
|
||||
|
@ -98,18 +93,11 @@ export const ResultSettingsLogic = kea<MakeLogicType<ResultSettingsValues, Resul
|
|||
closeModals: () => true,
|
||||
initializeResultFields: (serverResultFields, schema, schemaConflicts) => {
|
||||
const resultFields = convertServerResultFieldsToResultFields(serverResultFields, schema);
|
||||
Object.keys(schema).forEach((fieldName) => {
|
||||
if (!serverResultFields.hasOwnProperty(fieldName)) {
|
||||
serverResultFields[fieldName] = {};
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
serverResultFields,
|
||||
resultFields,
|
||||
schema,
|
||||
schemaConflicts,
|
||||
...splitResultFields(resultFields, schema),
|
||||
};
|
||||
},
|
||||
clearAllFields: () => true,
|
||||
|
@ -151,30 +139,6 @@ export const ResultSettingsLogic = kea<MakeLogicType<ResultSettingsValues, Resul
|
|||
saving: () => OpenModal.None,
|
||||
},
|
||||
],
|
||||
nonTextResultFields: [
|
||||
{},
|
||||
{
|
||||
initializeResultFields: (_, { nonTextResultFields }) => nonTextResultFields,
|
||||
clearAllFields: (nonTextResultFields) => clearAllFields(nonTextResultFields),
|
||||
resetAllFields: (nonTextResultFields) => resetAllFields(nonTextResultFields),
|
||||
updateField: (nonTextResultFields, { fieldName, settings }) =>
|
||||
nonTextResultFields.hasOwnProperty(fieldName)
|
||||
? { ...nonTextResultFields, [fieldName]: settings }
|
||||
: nonTextResultFields,
|
||||
},
|
||||
],
|
||||
textResultFields: [
|
||||
{},
|
||||
{
|
||||
initializeResultFields: (_, { textResultFields }) => textResultFields,
|
||||
clearAllFields: (textResultFields) => clearAllFields(textResultFields),
|
||||
resetAllFields: (textResultFields) => resetAllFields(textResultFields),
|
||||
updateField: (textResultFields, { fieldName, settings }) =>
|
||||
textResultFields.hasOwnProperty(fieldName)
|
||||
? { ...textResultFields, [fieldName]: settings }
|
||||
: textResultFields,
|
||||
},
|
||||
],
|
||||
resultFields: [
|
||||
{},
|
||||
{
|
||||
|
@ -187,22 +151,6 @@ export const ResultSettingsLogic = kea<MakeLogicType<ResultSettingsValues, Resul
|
|||
: resultFields,
|
||||
},
|
||||
],
|
||||
serverResultFields: [
|
||||
{},
|
||||
{
|
||||
initializeResultFields: (_, { serverResultFields }) => serverResultFields,
|
||||
clearAllFields: (serverResultFields) => clearAllServerFields(serverResultFields),
|
||||
resetAllFields: (serverResultFields) => resetAllServerFields(serverResultFields),
|
||||
updateField: (serverResultFields, { fieldName, settings }) => {
|
||||
return serverResultFields.hasOwnProperty(fieldName)
|
||||
? {
|
||||
...serverResultFields,
|
||||
[fieldName]: convertToServerFieldResultSetting(settings),
|
||||
}
|
||||
: serverResultFields;
|
||||
},
|
||||
},
|
||||
],
|
||||
lastSavedResultFields: [
|
||||
{},
|
||||
{
|
||||
|
@ -223,6 +171,31 @@ export const ResultSettingsLogic = kea<MakeLogicType<ResultSettingsValues, Resul
|
|||
],
|
||||
}),
|
||||
selectors: ({ selectors }) => ({
|
||||
textResultFields: [
|
||||
() => [selectors.resultFields, selectors.schema],
|
||||
(resultFields: FieldResultSettingObject, schema: Schema) => {
|
||||
const { textResultFields } = splitResultFields(resultFields, schema);
|
||||
return textResultFields;
|
||||
},
|
||||
],
|
||||
nonTextResultFields: [
|
||||
() => [selectors.resultFields, selectors.schema],
|
||||
(resultFields: FieldResultSettingObject, schema: Schema) => {
|
||||
const { nonTextResultFields } = splitResultFields(resultFields, schema);
|
||||
return nonTextResultFields;
|
||||
},
|
||||
],
|
||||
serverResultFields: [
|
||||
() => [selectors.resultFields],
|
||||
(resultFields: FieldResultSettingObject) => {
|
||||
return Object.entries(resultFields).reduce((serverResultFields, [fieldName, settings]) => {
|
||||
return {
|
||||
...serverResultFields,
|
||||
[fieldName]: convertToServerFieldResultSetting(settings as FieldResultSetting),
|
||||
};
|
||||
}, {});
|
||||
},
|
||||
],
|
||||
resultFieldsAtDefaultSettings: [
|
||||
() => [selectors.resultFields],
|
||||
(resultFields) => areFieldsAtDefaultSettings(resultFields),
|
||||
|
|
|
@ -12,9 +12,7 @@ import {
|
|||
areFieldsEmpty,
|
||||
convertServerResultFieldsToResultFields,
|
||||
convertToServerFieldResultSetting,
|
||||
clearAllServerFields,
|
||||
clearAllFields,
|
||||
resetAllServerFields,
|
||||
resetAllFields,
|
||||
splitResultFields,
|
||||
} from './utils';
|
||||
|
@ -33,20 +31,6 @@ describe('clearAllFields', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('clearAllServerFields', () => {
|
||||
it('will reset every key in an object back to an empty object', () => {
|
||||
expect(
|
||||
clearAllServerFields({
|
||||
foo: { raw: { size: 5 } },
|
||||
bar: { raw: true },
|
||||
})
|
||||
).toEqual({
|
||||
foo: {},
|
||||
bar: {},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('resetAllFields', () => {
|
||||
it('will reset every key in an object back to a default object', () => {
|
||||
expect(
|
||||
|
@ -61,20 +45,6 @@ describe('resetAllFields', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('resetAllServerFields', () => {
|
||||
it('will reset every key in an object back to a default object', () => {
|
||||
expect(
|
||||
resetAllServerFields({
|
||||
foo: { raw: { size: 5 } },
|
||||
bar: { snippet: true },
|
||||
})
|
||||
).toEqual({
|
||||
foo: { raw: {} },
|
||||
bar: { raw: {} },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertServerResultFieldsToResultFields', () => {
|
||||
it('will convert a server settings object to a format that the front-end expects', () => {
|
||||
expect(
|
||||
|
|
|
@ -59,15 +59,9 @@ const convertToFieldResultSetting = (serverFieldResultSetting: ServerFieldResult
|
|||
|
||||
export const clearAllFields = (fields: FieldResultSettingObject) => updateAllFields(fields, {});
|
||||
|
||||
export const clearAllServerFields = (fields: ServerFieldResultSettingObject) =>
|
||||
updateAllFields(fields, {});
|
||||
|
||||
export const resetAllFields = (fields: FieldResultSettingObject) =>
|
||||
updateAllFields(fields, DEFAULT_FIELD_SETTINGS);
|
||||
|
||||
export const resetAllServerFields = (fields: ServerFieldResultSettingObject) =>
|
||||
updateAllFields(fields, { raw: {} });
|
||||
|
||||
export const convertServerResultFieldsToResultFields = (
|
||||
serverResultFields: ServerFieldResultSettingObject,
|
||||
schema: Schema
|
||||
|
|
Loading…
Reference in a new issue