Skip to content

Commit

Permalink
Handle keyed logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed May 14, 2021
1 parent 49fe60a commit f084195
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>, field: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand All @@ -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);
});
});
});
});
Expand Down

0 comments on commit f084195

Please sign in to comment.