-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
useActions.spec.js
178 lines (143 loc) · 4.5 KB
/
useActions.spec.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import React from 'react'
import { createStore } from 'redux'
import * as rtl from 'react-testing-library'
import { Provider as ProviderMock, useActions } from '../../src/index.js'
describe('React', () => {
describe('hooks', () => {
describe('useActions', () => {
let store
let dispatchedActions = []
beforeEach(() => {
const reducer = (state = 0, action) => {
dispatchedActions.push(action)
if (action.type === 'inc1') {
return state + 1
}
if (action.type === 'inc') {
return state + action.amount
}
return state
}
store = createStore(reducer)
dispatchedActions = []
})
afterEach(() => rtl.cleanup())
it('supports a single action creator', () => {
const Comp = () => {
const inc1 = useActions(() => ({ type: 'inc1' }))
return (
<>
<button id="bInc1" onClick={inc1} />
</>
)
}
const { container } = rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
const bInc1 = container.querySelector('#bInc1')
rtl.fireEvent.click(bInc1)
expect(dispatchedActions).toEqual([{ type: 'inc1' }])
})
it('supports an object of action creators', () => {
const Comp = () => {
const { inc1, inc2 } = useActions({
inc1: () => ({ type: 'inc1' }),
inc2: () => ({ type: 'inc', amount: 2 })
})
return (
<>
<button id="bInc1" onClick={inc1} />
<button id="bInc2" onClick={inc2} />
</>
)
}
const { container } = rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
const bInc1 = container.querySelector('#bInc1')
const bInc2 = container.querySelector('#bInc2')
rtl.fireEvent.click(bInc1)
rtl.fireEvent.click(bInc2)
expect(dispatchedActions).toEqual([
{ type: 'inc1' },
{ type: 'inc', amount: 2 }
])
})
it('supports an array of action creators', () => {
const Comp = () => {
const [inc1, inc2] = useActions([
() => ({ type: 'inc1' }),
() => ({ type: 'inc', amount: 2 })
])
return (
<>
<button id="bInc1" onClick={inc1} />
<button id="bInc2" onClick={inc2} />
</>
)
}
const { container } = rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
const bInc1 = container.querySelector('#bInc1')
const bInc2 = container.querySelector('#bInc2')
rtl.fireEvent.click(bInc1)
rtl.fireEvent.click(bInc2)
expect(dispatchedActions).toEqual([
{ type: 'inc1' },
{ type: 'inc', amount: 2 }
])
})
it('passes through arguments', () => {
const reducer = (state = 0, action) => {
dispatchedActions.push(action)
if (action.type === 'adjust') {
return action.isAdd ? state + action.amount : state - action.amount
}
return state
}
const store = createStore(reducer)
dispatchedActions = []
const Comp = () => {
const { adjust } = useActions({
adjust: (amount, isAdd = true) => ({
type: 'adjust',
amount,
isAdd
})
})
return (
<>
<button id="bInc1" onClick={() => adjust(1)} />
<button id="bInc2" onClick={() => adjust(2)} />
<button id="bDec1" onClick={() => adjust(1, false)} />
</>
)
}
const { container } = rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)
const bInc1 = container.querySelector('#bInc1')
const bInc2 = container.querySelector('#bInc2')
const bDec1 = container.querySelector('#bDec1')
rtl.fireEvent.click(bInc1)
rtl.fireEvent.click(bInc2)
rtl.fireEvent.click(bDec1)
expect(dispatchedActions).toEqual([
{ type: 'adjust', amount: 1, isAdd: true },
{ type: 'adjust', amount: 2, isAdd: true },
{ type: 'adjust', amount: 1, isAdd: false }
])
})
// TODO: test for deps
})
})
})