Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/new.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,55 @@ describe("withStore", () => {

expect(getState().baz).toBe("baz");
});

it("are not called when its state/props dependencies do not change", async () => {
const sum = jest.fn(({ a }, { b }) => a + b);
const props = { b: 2, c: 9 };
const { effects, getState, setParentProps } = makeTestInstance(
{
initialState: () => ({ a: 1, d: 4 }),
computed: {
sum,
},
},
props
);

expect(getState().sum).toBe(3);
expect(sum.mock.calls.length).toBe(1);

setParentProps({ c: 8 });
await effects._setState({ d: 8 });

expect(getState().sum).toBe(3);
expect(sum.mock.calls.length).toBe(1);
});

it("is called when its state/props dependencies change", async () => {
const sum = jest.fn(({ a }, { b }) => a + b);
const props = { b: 2, c: 9 };
const { effects, getState, setParentProps } = makeTestInstance(
{
initialState: () => ({ a: 1, d: 4 }),
computed: {
sum,
},
},
props
);

expect(getState().sum).toBe(3);
expect(sum.mock.calls.length).toBe(1);

await effects._setState({ a: 2 });

expect(getState().sum).toBe(4);
expect(sum.mock.calls.length).toBe(2);

setParentProps({ b: 3 });

expect(getState().sum).toBe(5);
expect(sum.mock.calls.length).toBe(3);
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be almost exactly the same than the previous one except that we are changing:

  1. its state dependency
  2. its props dependency

});
});