diff --git a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts index 54c110fd39cde8..0dde75382926b9 100644 --- a/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts +++ b/x-pack/plugins/enterprise_search/public/applications/__mocks__/kea.mock.ts @@ -198,17 +198,29 @@ export class LogicMounter { // from: { dataLoading: true }, // to: { dataLoading: false }, // }); - public expectAction = (action: any) => { + // + // For keyed logic: + // + // ex. + // expectAction((logic) => { + // logic.actions.dataInitialized(); + // }, PROPS).toChangeState({ + // from: { dataLoading: true }, + // to: { dataLoading: false }, + // }); + // + + public expectAction = (action: (logic: LogicFile) => void, props: object = {}) => { 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[] = []) => { - this.mount(from); + const logic = this.mount(from, props); const originalValues = { - ...this.logicFile.values, + ...logic.values, }; - action(); - expect(this.logicFile.values).toEqual({ + action(logic); + expect(logic.values).toEqual({ ...originalValues, ...to, ...ignoreFields.reduce((acc: Record, field: string) => { diff --git a/x-pack/plugins/enterprise_search/public/applications/app_search/components/multi_input_rows/multi_input_rows_logic.test.ts b/x-pack/plugins/enterprise_search/public/applications/app_search/components/multi_input_rows/multi_input_rows_logic.test.ts index 0e2d28a6fc3e1f..79b1d6a1caba2f 100644 --- a/x-pack/plugins/enterprise_search/public/applications/app_search/components/multi_input_rows/multi_input_rows_logic.test.ts +++ b/x-pack/plugins/enterprise_search/public/applications/app_search/components/multi_input_rows/multi_input_rows_logic.test.ts @@ -7,12 +7,10 @@ import { LogicMounter } from '../../../__mocks__'; -import { Logic } from 'kea'; - import { MultiInputRowsLogic } from './multi_input_rows_logic'; describe('MultiInputRowsLogic', () => { - const { mount } = new LogicMounter(MultiInputRowsLogic); + const { mount, expectAction } = new LogicMounter(MultiInputRowsLogic); const MOCK_VALUES = ['a', 'b', 'c']; @@ -37,67 +35,70 @@ describe('MultiInputRowsLogic', () => { }); describe('actions', () => { - let logic: Logic; - - beforeEach(() => { - logic = mount({}, DEFAULT_PROPS); - }); - - afterEach(() => { - // Should not mutate the original array - expect(logic.values.values).not.toBe(MOCK_VALUES); // Would fail if we did not clone a new array - }); - describe('addValue', () => { it('appends an empty string to the values array & sets addedNewRow to true', () => { - logic.actions.addValue(); - - expect(logic.values).toEqual({ - ...DEFAULT_VALUES, - addedNewRow: true, - hasEmptyValues: true, - values: ['a', 'b', 'c', ''], + expectAction((logic) => { + logic.actions.addValue(); + }, DEFAULT_PROPS).toChangeState({ + from: { + addedNewRow: false, + hasEmptyValues: false, + values: ['a', 'b', 'c'], + }, + to: { + addedNewRow: true, + hasEmptyValues: true, + values: ['a', 'b', 'c', ''], + }, }); }); }); describe('deleteValue', () => { it('deletes the value at the specified array index', () => { - logic.actions.deleteValue(1); - - expect(logic.values).toEqual({ - ...DEFAULT_VALUES, - values: ['a', 'c'], + expectAction((logic) => { + logic.actions.deleteValue(1); + }, DEFAULT_PROPS).toChangeState({ + from: { + values: ['a', 'b', 'c'], + }, + to: { + values: ['a', 'c'], + }, }); }); }); describe('editValue', () => { it('edits the value at the specified array index', () => { - logic.actions.editValue(2, 'z'); - - expect(logic.values).toEqual({ - ...DEFAULT_VALUES, - values: ['a', 'b', 'z'], + expectAction((logic) => { + logic.actions.editValue(2, 'z'); + }, DEFAULT_PROPS).toChangeState({ + from: { + values: ['a', 'b', 'c'], + }, + to: { + values: ['a', 'b', 'z'], + }, }); }); }); - }); - describe('selectors', () => { - describe('hasEmptyValues', () => { - it('returns true if values has any empty strings', () => { - const logic = mount({}, { ...DEFAULT_PROPS, values: ['', '', ''] }); + describe('selectors', () => { + describe('hasEmptyValues', () => { + it('returns true if values has any empty strings', () => { + const logic = mount({}, { ...DEFAULT_PROPS, values: ['', '', ''] }); - expect(logic.values.hasEmptyValues).toEqual(true); + expect(logic.values.hasEmptyValues).toEqual(true); + }); }); - }); - describe('hasOnlyOneValue', () => { - it('returns true if values only has one item', () => { - const logic = mount({}, { ...DEFAULT_PROPS, values: ['test'] }); + describe('hasOnlyOneValue', () => { + it('returns true if values only has one item', () => { + const logic = mount({}, { ...DEFAULT_PROPS, values: ['test'] }); - expect(logic.values.hasOnlyOneValue).toEqual(true); + expect(logic.values.hasOnlyOneValue).toEqual(true); + }); }); }); });