-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
Copy pathinjectedDispatch.test-d.ts
95 lines (76 loc) · 2.45 KB
/
injectedDispatch.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import type { Action, Dispatch } from 'redux'
interface Component<P> {
props: P
}
interface HOC<T> {
<P>(wrapped: Component<P & T>): Component<P>
}
declare function connect<T, D extends Dispatch = Dispatch>(
mapDispatchToProps: (dispatch: D) => T
): HOC<T>
describe('type tests', () => {
test('inject default dispatch.', () => {
const hoc: HOC<{ onClick(): void }> = connect(dispatch => {
return {
onClick() {
expectTypeOf(dispatch).toBeCallableWith({ type: 'INCREMENT' })
expectTypeOf(dispatch)
.parameter(0)
.not.toMatchTypeOf(Promise.resolve({ type: 'INCREMENT' }))
expectTypeOf(dispatch).parameter(0).not.toMatchTypeOf('not-an-action')
}
}
})
})
test('inject dispatch 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 hoc: HOC<{ onClick(): void }> = connect(
(dispatch: Dispatch<MyAction>) => {
return {
onClick() {
expectTypeOf(dispatch).toBeCallableWith({ type: 'INCREMENT' })
expectTypeOf(dispatch).toBeCallableWith({
type: 'DECREMENT',
count: 10
})
expectTypeOf(dispatch)
.parameter(0)
.not.toMatchTypeOf({ type: 'DECREMENT', count: '' })
expectTypeOf(dispatch)
.parameter(0)
.not.toEqualTypeOf({ type: 'SOME_OTHER_TYPE' })
expectTypeOf(dispatch)
.parameter(0)
.not.toMatchTypeOf('not-an-action')
}
}
}
)
})
test('inject extended dispatch.', () => {
type PromiseDispatch = <T extends Action>(promise: Promise<T>) => Promise<T>
type MyDispatch = Dispatch & PromiseDispatch
const hoc: HOC<{ onClick(): void }> = connect((dispatch: MyDispatch) => {
return {
onClick() {
// `.toBeCallableWith or .parameter(0).toMatchTypeOf`
// do not work in this scenario.
dispatch({ type: 'INCREMENT' })
expectTypeOf(dispatch).toBeCallableWith(
Promise.resolve({ type: 'INCREMENT' })
)
expectTypeOf(dispatch).parameter(0).not.toMatchTypeOf('not-an-action')
}
}
})
})
})