-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
Copy pathdispatch.test-d.ts
46 lines (33 loc) · 1.26 KB
/
dispatch.test-d.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
import type { Dispatch } from 'redux'
describe('type tests', () => {
test('default Dispatch type accepts any object with `type` property.', () => {
const dispatch: Dispatch = null as any
const a = dispatch({ type: 'INCREMENT', count: 10 })
expectTypeOf(a).toHaveProperty('count')
expectTypeOf(a).not.toHaveProperty('wrongProp')
expectTypeOf(dispatch).parameter(0).not.toMatchTypeOf('not-an-action')
})
test('Dispatch accepts type argument that restricts allowed action types.', () => {
interface IncrementAction {
type: 'INCREMENT'
count?: number
}
interface DecrementAction {
type: 'DECREMENT'
count?: number
}
// Union of all actions in the app.
type MyAction = IncrementAction | DecrementAction
const dispatch: Dispatch<MyAction> = null as any
expectTypeOf(dispatch).parameter(0).toMatchTypeOf({ type: 'INCREMENT' })
expectTypeOf(dispatch).toBeCallableWith({ type: 'DECREMENT', count: 10 })
// Known actions are strictly checked.
expectTypeOf(dispatch)
.parameter(0)
.not.toMatchTypeOf({ type: 'DECREMENT', count: '' })
// Unknown actions are rejected.
expectTypeOf(dispatch)
.parameter(0)
.not.toEqualTypeOf({ type: 'SOME_OTHER_TYPE' })
})
})