-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathPagination.test.tsx
More file actions
313 lines (244 loc) · 10.2 KB
/
Pagination.test.tsx
File metadata and controls
313 lines (244 loc) · 10.2 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import React from 'react'
import { mount } from 'enzyme'
import Pagination, { PageButton, NavigationButton, EmptyPageButton } from '../Pagination'
describe('NavigationButton', () => {
it('should render without crashing', () => {
const onClick = () => {}
mount(<NavigationButton directionIcon="prev" onClick={onClick} />)
})
it('should contain "Previous" aria-label based on directionIcon', () => {
const onClick = () => {}
const expected = 'Previous'
const wrapper = mount(<NavigationButton directionIcon="prev" onClick={onClick} />)
expect(wrapper.find('button').getDOMNode().getAttribute('aria-label')).toContain(expected)
})
it('should contain "Next" aria-label based on directionIcon', () => {
const onClick = () => {}
const expected = 'Next'
const wrapper = mount(<NavigationButton directionIcon="next" onClick={onClick} />)
expect(wrapper.find('button').getDOMNode().getAttribute('aria-label')).toContain(expected)
})
it('should contain a child SVG', () => {
const onClick = () => {}
const expectedSvg = 'h-3 w-3'
const wrapper = mount(<NavigationButton directionIcon="prev" onClick={onClick} />)
expect(wrapper.find('svg')).toBeDefined()
expect(wrapper.find('svg').getDOMNode().getAttribute('class')).toContain(expectedSvg)
})
it('should call onClick', () => {
const onClick = jest.fn()
const wrapper = mount(<NavigationButton directionIcon="next" onClick={onClick} />)
wrapper.find('button').simulate('click')
expect(onClick).toHaveBeenCalledTimes(1)
})
})
describe('PageButton', () => {
it('should render without crashing', () => {
const onClick = () => {}
mount(<PageButton page={1} onClick={onClick} />)
})
it('should render the right page', () => {
const onClick = () => {}
const expected = 1
const wrapper = mount(<PageButton isActive page={1} onClick={onClick} />)
expect(wrapper.find('button').text()).toContain(expected)
})
it('should contain text-xs', () => {
const onClick = () => {}
const expected = 'text-xs'
const wrapper = mount(<PageButton isActive page={1} onClick={onClick} />)
expect(wrapper.find('button').getDOMNode().getAttribute('class')).toContain(expected)
})
it('should call onClick', () => {
const onClick = jest.fn()
const wrapper = mount(<PageButton page={1} onClick={onClick} />)
wrapper.find('button').simulate('click')
expect(onClick).toHaveBeenCalledTimes(1)
})
})
describe('EmptyPageButton', () => {
it('should render without crashing', () => {
mount(<EmptyPageButton />)
})
it('should render three dots as children', () => {
const expected = '...'
const wrapper = mount(<EmptyPageButton />)
expect(wrapper.find('span').text()).toContain(expected)
})
})
describe('Pagination', () => {
it('should render without crashing', () => {
const onChange = () => {}
mount(<Pagination totalResults={123} label="Navigation" onChange={onChange} />)
})
it('should render with base styles', () => {
const onChange = () => {}
const expected =
'flex flex-col justify-between text-xs sm:flex-row text-gray-600 dark:text-gray-400'
const wrapper = mount(<Pagination totalResults={123} label="Navigation" onChange={onChange} />)
expect(wrapper.find(Pagination).getDOMNode().getAttribute('class')).toContain(expected)
})
it('should render next and previous buttons', () => {
const onChange = () => {}
const wrapper = mount(<Pagination totalResults={123} label="Navigation" onChange={onChange} />)
expect(wrapper.find('button[aria-label="Next"]')).toBeTruthy()
expect(wrapper.find('button[aria-label="Previous"]')).toBeTruthy()
})
it('should render a list with exact children', () => {
const onChange = () => {}
const expected = 7
const wrapper = mount(<Pagination totalResults={50} label="Navigation" onChange={onChange} />)
expect(wrapper.find('ul').children().length).toBe(expected)
})
it('should never render a list with more than 9 children', () => {
const onChange = () => {}
// accounts for 2 nav buttons
const expected = 9
const wrapper = mount(<Pagination totalResults={120} label="Navigation" onChange={onChange} />)
expect(wrapper.find('ul').children().length).toBe(expected)
})
it('should render a list taking into account resultsPerPage', () => {
const onChange = () => {}
const expected = 6
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
expect(wrapper.find(PageButton).children().length).toBe(expected)
})
it('should call click handler on next button', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
nextButton.simulate('click')
// one on render and another on click
expect(onChange).toHaveBeenCalledTimes(2)
})
it('should call click handler on prev button', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
// go forward one page to activate prev button
nextButton.simulate('click')
const prevButton = wrapper.find('button[aria-label="Previous"]')
// click the active prev button
prevButton.simulate('click')
// one on render, one on next and another on prev
expect(onChange).toHaveBeenCalledTimes(3)
})
it('should not call click handler on prev button if first page', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const prevButton = wrapper.find('button[aria-label="Previous"]')
prevButton.simulate('click')
// one on render
expect(onChange).toHaveBeenCalledTimes(1)
})
it('should not call click handler on next button if last page', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={10} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
nextButton.simulate('click')
nextButton.simulate('click')
// one on render and one of the above clicks are ignored
expect(onChange).toHaveBeenCalledTimes(2)
})
it('should call click handler on prev button', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
// go forward one page to activate prev button
nextButton.simulate('click')
const prevButton = wrapper.find('button[aria-label="Previous"]')
// click the active prev button
prevButton.simulate('click')
// one on render, one on next and another on prev
expect(onChange).toHaveBeenCalledTimes(3)
})
it('should render two "..." in the middle of a big list', () => {
const onChange = () => {}
const wrapper = mount(
<Pagination totalResults={50} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
// go to page 5
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
expect(wrapper.find(EmptyPageButton)).toHaveLength(2)
})
it('should render the last 5 items', () => {
const onChange = () => {}
const wrapper = mount(
<Pagination totalResults={50} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const nextButton = wrapper.find('button[aria-label="Next"]')
// go to page 7
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
nextButton.simulate('click')
const pageButtons = wrapper.find(PageButton).children()
// not considering '...' see last line test
const pagesArray = ['1', '6', '7', '8', '9', '10']
pageButtons.forEach((b, i) => {
expect(b.text()).toBe(pagesArray[i])
})
expect(wrapper.find(EmptyPageButton)).toHaveLength(1)
})
it('should call click handler on page button', () => {
const onChange = jest.fn()
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
const thirdPage = wrapper.find(PageButton).at(2)
// go forward one page to activate prev button
thirdPage.simulate('click')
expect(onChange).toHaveBeenCalledWith(3)
})
it('should update the pages total when totalResults is updated', () => {
const onChange = () => {}
const expectedBeforeUpdate = 6
const expectedAfterUpdate = 4
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
expect(wrapper.find(PageButton).children().length).toBe(expectedBeforeUpdate)
wrapper.setProps({ totalResults: 20, resultsPerPage: 5 })
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})
it('should update the pages total when resultsPerPage is updated', () => {
const onChange = () => {}
const expectedBeforeUpdate = 6
const expectedAfterUpdate = 3
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} label="Navigation" onChange={onChange} />
)
expect(wrapper.find(PageButton).children().length).toBe(expectedBeforeUpdate)
wrapper.setProps({ resultsPerPage: 10 })
wrapper.update()
expect(wrapper.find(PageButton).children().length).toBe(expectedAfterUpdate)
})
it('should start with different active page', () => {
const onChange = () => {}
const expected = 'bg-purple-600'
const wrapper = mount(
<Pagination totalResults={30} resultsPerPage={5} activePage={3} label="Navigation" onChange={onChange} />
)
// Active page
expect(wrapper.find('button').at(3).getDOMNode().getAttribute('class')).toContain(expected)
})
})