-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhooked-on-redux.test.js
210 lines (176 loc) · 6.26 KB
/
hooked-on-redux.test.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @jest-environment jsdom
*/
import React from 'react'
import { render, fireEvent, getNodeText } from '@testing-library/react'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import { createHookedOnReducer, useHookedOnState } from './index'
const Counter = props => {
const [value, updateValue] = useHookedOnState('app.counterValue', 0, props.options)
const [, updateAll] = useHookedOnState('app', {})
return (
<main>
<div><button data-testid='deincrement' onClick={() => updateValue(value - 1)}>-</button></div>
<div data-testid='countervalue'>{value}</div>
<div><button data-testid='increment' onClick={() => updateValue(value + 1)}>+</button></div>
<div><button data-testid='set' onClick={() => updateAll({ counterValue: 42 })}>Set to 42</button></div>
</main>
)
}
const TodoList = props => {
const [list, updateList] = useHookedOnState('list', ['milk'])
const mutateList = () => {
list.push('eggs')
list.push('bread')
list.push('taters')
updateList(list)
}
return (
<main>
<div data-testid='list'>
{list.map(item => `${item},`)}
...
</div>
<button
data-testid='add'
onClick={mutateList}
/>
<button
data-testid='replace'
onClick={() => updateList(['cookies', 'candy', 'cheese whiz'])}
/>
</main>
)
}
const AddressBook = props => {
const [fields, updateFields] = useHookedOnState('fields.user1', {
first: 'Darth',
last: 'Vader',
planet: 'Hoth'
})
const updateAll = () => {
updateFields({ ...fields, last: 'Maul', password: 12345 })
}
return (
<main>
<div data-testid='details'>{fields.first} {fields.last} {fields.planet} {fields.password}</div>
<button data-testid='update' onClick={updateAll}>Update</button>
</main>
)
}
describe('HookedOnRedux', () => {
it('should increment and deincrement a counter', () => {
const reducer = createHookedOnReducer()
const store = createStore(reducer, {})
const App = () =>
<Provider store={store}>
<Counter />
</Provider>
const { getByTestId } = render(<App />)
const counter = getByTestId('countervalue')
const increment = getByTestId('increment')
const deincrement = getByTestId('deincrement')
const set = getByTestId('set')
expect(getNodeText(counter)).toBe('0')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('1')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('2')
;[1, 2, 3].forEach(() => fireEvent.click(deincrement))
expect(getNodeText(counter)).toBe('-1')
fireEvent.click(set)
expect(getNodeText(counter)).toBe('42')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('43')
})
it('should allow an initial state', () => {
const reducer = createHookedOnReducer()
const store = createStore(reducer, { app: { counterValue: 5 } })
const App = () =>
<Provider store={store}>
<Counter />
</Provider>
const { getByTestId } = render(<App />)
const counter = getByTestId('countervalue')
const increment = getByTestId('increment')
expect(getNodeText(counter)).toBe('5')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('6')
})
it('should allow immutable updates on arrays', () => {
const reducer = createHookedOnReducer()
const store = createStore(reducer)
const App = () =>
<Provider store={store}>
<TodoList />
</Provider>
const { getByTestId } = render(<App />)
const list = getByTestId('list')
const addItems = getByTestId('add')
const replaceItems = getByTestId('replace')
expect(getNodeText(list)).toBe('milk,...')
fireEvent.click(addItems)
expect(getNodeText(list)).toBe('milk,eggs,bread,taters,...')
fireEvent.click(replaceItems)
expect(getNodeText(list)).toBe('cookies,candy,cheese whiz,...')
})
it('should predictably update objects', () => {
const reducer = createHookedOnReducer()
const store = createStore(reducer)
const App = () =>
<Provider store={store}>
<AddressBook />
</Provider>
const { getByTestId } = render(<App />)
const details = getByTestId('details')
const update = getByTestId('update')
expect(getNodeText(details)).toBe('Darth Vader Hoth ')
fireEvent.click(update)
expect(getNodeText(details)).toBe('Darth Maul Hoth 12345')
})
it('should allow a namespace w/a string', () => {
const reducer = createHookedOnReducer({}, 'MY_NAMESPACE')
const store = createStore(reducer, { app: { counterValue: 2 } })
const App = () =>
<Provider store={store}>
<Counter options='MY_NAMESPACE' />
</Provider>
const { getByTestId } = render(<App />)
const counter = getByTestId('countervalue')
const increment = getByTestId('increment')
expect(getNodeText(counter)).toBe('2')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('3')
})
it('should allow a namespace in an options object', () => {
const reducer = createHookedOnReducer({}, 'MY_NAMESPACE2')
const store = createStore(reducer, { app: { counterValue: 5 } })
const App = () =>
<Provider store={store}>
<Counter options={{ namespace: 'MY_NAMESPACE2' }} />
</Provider>
const { getByTestId } = render(<App />)
const counter = getByTestId('countervalue')
const increment = getByTestId('increment')
expect(getNodeText(counter)).toBe('5')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('6')
})
it('should allow setting a root path to work with combined reducers', () => {
const counterReducer = createHookedOnReducer()
const otherReducer = (state = {}) => state
const reducers = combineReducers({ counterReducer, otherReducer })
const store = createStore(reducers, { counterReducer: { app: { counterValue: 3 } } })
const App = () =>
<Provider store={store}>
<Counter options={{ rootPath: 'counterReducer' }} />
</Provider>
const { getByTestId } = render(<App />)
const counter = getByTestId('countervalue')
const increment = getByTestId('increment')
expect(getNodeText(counter)).toBe('3')
fireEvent.click(increment)
expect(getNodeText(counter)).toBe('4')
})
})