@@ -84,6 +84,27 @@ describe('Table.Pagination', () => {
84
84
await user . click ( getPreviousPage ( ) )
85
85
expect ( onChange ) . not . toHaveBeenCalled ( )
86
86
} )
87
+
88
+ it ( 'should rerender many pages correctly' , async ( ) => {
89
+ const onChange = jest . fn ( )
90
+
91
+ const { rerender} = render (
92
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 0 } pageSize = { 25 } totalCount = { 25 } /> ,
93
+ )
94
+ expect ( getPages ( ) ) . toHaveLength ( 1 )
95
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 0 ) )
96
+ expect ( getPageRange ( ) ) . toEqual ( '1 through 25 of 25' )
97
+
98
+ rerender (
99
+ < Pagination onChange = { onChange } aria-label = "Test label" defaultPageIndex = { 2 } pageSize = { 5 } totalCount = { 300 } /> ,
100
+ )
101
+ expect ( getPageRange ( ) ) . toEqual ( '11 through 15 of 300' )
102
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 2 ) )
103
+ expect ( getInvalidPages ( ) ) . toHaveLength ( 0 )
104
+ expect ( onChange ) . toHaveBeenCalledWith ( {
105
+ pageIndex : 2 ,
106
+ } )
107
+ } )
87
108
} )
88
109
89
110
describe ( 'with two pages' , ( ) => {
@@ -114,6 +135,32 @@ describe('Table.Pagination', () => {
114
135
expect ( getPageRange ( ) ) . toEqual ( '1 through 25 of 50' )
115
136
} )
116
137
138
+ it ( 'should rerender pager with correct page highlighted when clicking on pages and defaultPageIndex set' , async ( ) => {
139
+ const user = userEvent . setup ( )
140
+ const onChange = jest . fn ( )
141
+
142
+ render (
143
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 3 } pageSize = { 25 } totalCount = { 200 } /> ,
144
+ )
145
+
146
+ expect ( getPageRange ( ) ) . toEqual ( '76 through 100 of 200' )
147
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 3 ) )
148
+
149
+ await user . click ( getPage ( 1 ) )
150
+ expect ( onChange ) . toHaveBeenCalledWith ( {
151
+ pageIndex : 1 ,
152
+ } )
153
+ expect ( getPageRange ( ) ) . toEqual ( '26 through 50 of 200' )
154
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 1 ) )
155
+
156
+ await user . click ( getPage ( 0 ) )
157
+ expect ( onChange ) . toHaveBeenCalledWith ( {
158
+ pageIndex : 0 ,
159
+ } )
160
+ expect ( getPageRange ( ) ) . toEqual ( '1 through 25 of 200' )
161
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 0 ) )
162
+ } )
163
+
117
164
it ( 'should call `onChange` when using the keyboard to interact with pages' , async ( ) => {
118
165
const user = userEvent . setup ( )
119
166
const onChange = jest . fn ( )
@@ -165,6 +212,31 @@ describe('Table.Pagination', () => {
165
212
} )
166
213
} )
167
214
215
+ it ( 'should rerender pager with correct page highlighted when clicking on previous or next and defaultPageIndex set' , async ( ) => {
216
+ const user = userEvent . setup ( )
217
+ const onChange = jest . fn ( )
218
+
219
+ render (
220
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 3 } pageSize = { 25 } totalCount = { 200 } /> ,
221
+ )
222
+
223
+ expect ( getPageRange ( ) ) . toEqual ( '76 through 100 of 200' )
224
+
225
+ await user . click ( getNextPage ( ) )
226
+ expect ( onChange ) . toHaveBeenCalledWith ( {
227
+ pageIndex : 4 ,
228
+ } )
229
+ expect ( getPageRange ( ) ) . toEqual ( '101 through 125 of 200' )
230
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 4 ) )
231
+
232
+ await user . click ( getPreviousPage ( ) )
233
+ expect ( onChange ) . toHaveBeenCalledWith ( {
234
+ pageIndex : 3 ,
235
+ } )
236
+ expect ( getPageRange ( ) ) . toEqual ( '76 through 100 of 200' )
237
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 3 ) )
238
+ } )
239
+
168
240
it ( 'should call `onChange` when using the keyboard to interact with previous or next' , async ( ) => {
169
241
const user = userEvent . setup ( )
170
242
const onChange = jest . fn ( )
@@ -197,6 +269,27 @@ describe('Table.Pagination', () => {
197
269
pageIndex : 0 ,
198
270
} )
199
271
} )
272
+
273
+ it ( 'should rerender many pages correctly' , async ( ) => {
274
+ const onChange = jest . fn ( )
275
+
276
+ const { rerender} = render (
277
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 1 } pageSize = { 25 } totalCount = { 50 } /> ,
278
+ )
279
+ expect ( getPages ( ) ) . toHaveLength ( 2 )
280
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 1 ) )
281
+ expect ( getPageRange ( ) ) . toEqual ( '26 through 50 of 50' )
282
+
283
+ rerender (
284
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 0 } pageSize = { 5 } totalCount = { 300 } /> ,
285
+ )
286
+ expect ( getPageRange ( ) ) . toEqual ( '1 through 5 of 300' )
287
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 0 ) )
288
+ expect ( getInvalidPages ( ) ) . toHaveLength ( 0 )
289
+ expect ( onChange ) . toHaveBeenCalledWith ( {
290
+ pageIndex : 0 ,
291
+ } )
292
+ } )
200
293
} )
201
294
202
295
describe ( 'with three or more pages' , ( ) => {
@@ -242,6 +335,34 @@ describe('Table.Pagination', () => {
242
335
expect ( pages ) . toHaveLength ( 7 )
243
336
} )
244
337
} )
338
+
339
+ it ( 'should rerender many pages correctly' , async ( ) => {
340
+ const onChange = jest . fn ( )
341
+ const { rerender} = render (
342
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 1 } pageSize = { 10 } totalCount = { 1000 } /> ,
343
+ )
344
+ expect ( getPages ( ) ) . toHaveLength ( 8 )
345
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 1 ) )
346
+ expect ( getPageRange ( ) ) . toEqual ( '11 through 20 of 1000' )
347
+
348
+ rerender (
349
+ < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 0 } pageSize = { 5 } totalCount = { 300 } /> ,
350
+ )
351
+ expect ( getPageRange ( ) ) . toEqual ( '1 through 5 of 300' )
352
+ expect ( getFirstPage ( ) ) . toEqual ( getCurrentPage ( ) )
353
+ expect ( getInvalidPages ( ) ) . toHaveLength ( 0 )
354
+ expect ( onChange ) . toHaveBeenCalledWith ( {
355
+ pageIndex : 0 ,
356
+ } )
357
+ } )
358
+
359
+ it ( 'when rendering 3 pages and the second page is selected we should render a page number not ...' , async ( ) => {
360
+ const onChange = jest . fn ( )
361
+ render ( < Pagination aria-label = "Test label" onChange = { onChange } defaultPageIndex = { 1 } pageSize = { 2 } totalCount = { 6 } /> )
362
+ expect ( getPageRange ( ) ) . toEqual ( '3 through 4 of 6' )
363
+ expect ( getCurrentPage ( ) ) . toEqual ( getPage ( 1 ) )
364
+ expect ( getInvalidPages ( ) ) . toHaveLength ( 0 )
365
+ } )
245
366
} )
246
367
247
368
function getPages ( ) {
@@ -288,6 +409,10 @@ function getLastPage() {
288
409
return pages [ pages . length - 1 ]
289
410
}
290
411
412
+ function getInvalidPages ( ) {
413
+ return getPages ( ) . filter ( p => p . textContent ?. match ( / P a g e \s - / g) || p . textContent ?. match ( / P a g e \s 0 $ / g) )
414
+ }
415
+
291
416
function getPageRange ( ) {
292
417
const element = document . querySelector ( '.TablePaginationRange' )
293
418
if ( element && element . textContent ) {
0 commit comments