Skip to content

Commit

Permalink
Ignore certain field changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed May 14, 2021
1 parent f084195 commit c346668
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ export class LogicMounter {
return {
// Mount state with "from" values and test that the specified "to" values are present in
// the updated state, and that no other values have changed.
toChangeState: ({ from, to }: { from: object; to: object }, ignoreFields: string[] = []) => {
toChangeState: ({ from, to, ignore }: { from: object; to: object; ignore?: string[] }) => {
const logic = this.mount(from, props);
const originalValues = {
...logic.values,
Expand All @@ -223,7 +223,7 @@ export class LogicMounter {
expect(logic.values).toEqual({
...originalValues,
...to,
...ignoreFields.reduce((acc: Record<string, object>, field: string) => {
...(ignore || []).reduce((acc: Record<string, object>, field: string) => {
acc[field] = expect.anything();
return acc;
}, {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const expectToHaveBeenCalledWithStrict = (
};

describe('ResultSettingsLogic', () => {
const { mount } = new LogicMounter(ResultSettingsLogic);
const { mount, expectAction } = new LogicMounter(ResultSettingsLogic);

const DEFAULT_VALUES = {
dataLoading: true,
Expand All @@ -54,6 +54,8 @@ describe('ResultSettingsLogic', () => {
queryPerformanceScore: 0,
};

const SELECTOR_FIELDS = Object.keys(SELECTORS);

// Values without selectors
const resultSettingLogicValues = () => omit(ResultSettingsLogic.values, Object.keys(SELECTORS));

Expand Down Expand Up @@ -91,117 +93,112 @@ describe('ResultSettingsLogic', () => {
};

it('will initialize all result field state within the UI, based on provided server data', () => {
mount({
dataLoading: true,
saving: true,
});

ResultSettingsLogic.actions.initializeResultFields(
serverResultFields,
schema,
schemaConflicts
);

expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
dataLoading: false,
saving: false,
// It converts the passed server result fields to a client results field and stores it
// as resultFields
resultFields: {
foo: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
// Baz was not part of the original serverResultFields, it was injected based on the schema
baz: {
raw: false,
snippet: false,
snippetFallback: false,
},
bar: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
},
// It also saves it as lastSavedResultFields
lastSavedResultFields: {
foo: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
// Baz was not part of the original serverResultFields, it was injected based on the schema
baz: {
raw: false,
snippet: false,
snippetFallback: false,
expectAction(() => {
ResultSettingsLogic.actions.initializeResultFields(
serverResultFields,
schema,
schemaConflicts
);
}).toChangeState({
from: {
dataLoading: true,
saving: true,
},
ignore: SELECTOR_FIELDS,
to: {
dataLoading: false,
saving: false,
// It converts the passed server result fields to a client results field and stores it
// as resultFields
resultFields: {
foo: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
// Baz was not part of the original serverResultFields, it was injected based on the schema
baz: {
raw: false,
snippet: false,
snippetFallback: false,
},
bar: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
},
bar: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
// It also saves it as lastSavedResultFields
lastSavedResultFields: {
foo: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
// Baz was not part of the original serverResultFields, it was injected based on the schema
baz: {
raw: false,
snippet: false,
snippetFallback: false,
},
bar: {
raw: true,
rawSize: 5,
snippet: false,
snippetFallback: false,
},
},
// Stores the provided schema details
schema,
schemaConflicts,
},
// Stores the provided schema details
schema,
schemaConflicts,
});
});

it('default schema conflicts data if none was provided', () => {
mount();

ResultSettingsLogic.actions.initializeResultFields(serverResultFields, schema);

expect(ResultSettingsLogic.values.schemaConflicts).toEqual({});
});
});

describe('clearAllFields', () => {
it('should remove all settings that have been set for each field', () => {
mount({
resultFields: {
quuz: { raw: false, snippet: false, snippetFallback: false },
corge: { raw: true, snippet: false, snippetFallback: true },
expectAction(() => {
ResultSettingsLogic.actions.clearAllFields();
}).toChangeState({
from: {
resultFields: {
quuz: { raw: false, snippet: false, snippetFallback: false },
corge: { raw: true, snippet: false, snippetFallback: true },
},
},
});

ResultSettingsLogic.actions.clearAllFields();

expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
resultFields: {
quuz: {},
corge: {},
to: {
resultFields: {
quuz: {},
corge: {},
},
},
ignore: SELECTOR_FIELDS,
});
});
});

describe('resetAllFields', () => {
it('should reset all settings to their default values per field', () => {
mount({
resultFields: {
quuz: { raw: true, snippet: true, snippetFallback: true },
corge: { raw: true, snippet: true, snippetFallback: true },
expectAction(() => {
ResultSettingsLogic.actions.resetAllFields();
}).toChangeState({
from: {
resultFields: {
quuz: { raw: true, snippet: true, snippetFallback: true },
corge: { raw: true, snippet: true, snippetFallback: true },
},
},
});

ResultSettingsLogic.actions.resetAllFields();

expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
resultFields: {
quuz: { raw: true, snippet: false, snippetFallback: false },
corge: { raw: true, snippet: false, snippetFallback: false },
to: {
resultFields: {
quuz: { raw: true, snippet: false, snippetFallback: false },
corge: { raw: true, snippet: false, snippetFallback: false },
},
},
ignore: SELECTOR_FIELDS,
});
});
});
Expand All @@ -215,52 +212,45 @@ describe('ResultSettingsLogic', () => {
};

it('should update settings for an individual field', () => {
mount(initialValues);

ResultSettingsLogic.actions.updateField('foo', {
raw: true,
snippet: false,
snippetFallback: false,
});

expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
// the settings for foo are updated below for any *ResultFields state in which they appear
resultFields: {
foo: { raw: true, snippet: false, snippetFallback: false },
bar: { raw: true, snippet: true, snippetFallback: true },
expectAction(() => {
ResultSettingsLogic.actions.updateField('foo', {
raw: true,
snippet: false,
snippetFallback: false,
});
}).toChangeState({
from: initialValues,
to: {
resultFields: {
foo: { raw: true, snippet: false, snippetFallback: false },
bar: { raw: true, snippet: true, snippetFallback: true },
},
},
ignore: SELECTOR_FIELDS,
});
});

it('should do nothing if the specified field does not exist', () => {
mount(initialValues);

ResultSettingsLogic.actions.updateField('baz', {
raw: false,
snippet: false,
snippetFallback: false,
});

// 'baz' does not exist in state, so nothing is updated
expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
...initialValues,
expectAction(() => {
ResultSettingsLogic.actions.updateField('baz', {
raw: false,
snippet: false,
snippetFallback: false,
});
}).toChangeState({
from: initialValues,
to: initialValues,
});
});
});

describe('saving', () => {
it('sets saving to true', () => {
mount({
saving: false,
});

ResultSettingsLogic.actions.saving();

expect(resultSettingLogicValues()).toEqual({
...DEFAULT_VALUES,
saving: true,
expectAction(() => {
ResultSettingsLogic.actions.saving();
}).toChangeState({
from: { saving: false },
to: { saving: true },
});
});
});
Expand Down

0 comments on commit c346668

Please sign in to comment.