Skip to content
This repository was archived by the owner on Feb 3, 2020. It is now read-only.

Commit 927a475

Browse files
build: add coverage for arbitrary return value enhancement
1 parent 5fc2c86 commit 927a475

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/handle-action.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,35 @@ test('handles specific action with payload', () => {
3232
expect(newState2).toEqual({ counter: 0 });
3333
expect(newState2).toBe(state);
3434
});
35+
36+
test('handles specific action with payload by returning value directly', () => {
37+
const ac1 = createAction<{ num: number }>('foo3');
38+
const state: { readonly counter: number } = { counter: 0 };
39+
const re = handleAction<typeof state>(ac1, (draft, { payload }) => ({
40+
counter: draft.counter + payload.num,
41+
}), state);
42+
const newState1 = re(state, ac1({ num: 7 }));
43+
expect(newState1).toEqual({ counter: 7 });
44+
expect(newState1).not.toBe(state);
45+
});
46+
47+
test('handles specific action with payload and ignores directly returned value if draft is mutated', () => {
48+
const ac1 = createAction<{ num: number }>('foo4');
49+
const state: { readonly counter: number } = { counter: 0 };
50+
const re = handleAction<typeof state>(ac1, (draft, { payload }) => {
51+
draft.counter += payload.num;
52+
return 'unintended return value';
53+
}, state);
54+
const newState1 = re(state, ac1({ num: 10 }));
55+
expect(newState1).toEqual({ counter: 10 });
56+
expect(newState1).not.toBe(state);
57+
});
58+
59+
test('handles specific action and uses previous state if directly return value is undefined', () => {
60+
const ac1 = createAction<void>('foo5');
61+
const state: { readonly baz: number } = { baz: 0 };
62+
const re = handleAction<typeof state>(ac1, () => undefined, state);
63+
const newState1 = re(state, ac1());
64+
expect(newState1).toEqual({ baz: 0 });
65+
expect(newState1).toBe(state);
66+
});

0 commit comments

Comments
 (0)