-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
48 lines (38 loc) · 1.21 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import useCombine from './index';
import { vi, assert, expect, test, expectTypeOf } from 'vitest'
const userInitialState = { name: 'name' };
const todosInitialState = { todos: [1, 2, 3] };
test('should merge states with provided keys', () => {
const [state] = useCombine({
user: [userInitialState, (state) => state],
todos: [todosInitialState, (state) => state],
});
assert.deepEqual(state, {
user: userInitialState,
todos: todosInitialState,
});
});
test('should infer state type', () => {
const [state] = useCombine({
user: [userInitialState, (state) => state],
todos: [todosInitialState, (state) => state],
});
expectTypeOf(state).toEqualTypeOf<{
user: {name: string}
todos: {todos: number[]}
}>()
})
test('should call all passed reducers', () => {
const reducerA = vi.fn();
const reducerB = vi.fn();
const action = { type: 'init' };
const [_, dispatch] = useCombine({
user: [userInitialState, reducerA],
todos: [todosInitialState, reducerB],
});
dispatch(action);
expect(reducerA).toHaveBeenCalledTimes(1);
expect(reducerA).toHaveBeenCalledWith(action);
expect(reducerB).toHaveBeenCalledTimes(1);
expect(reducerB).toHaveBeenCalledWith(action);
});