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
|
import { describe, expect, it } from '@jest/globals';
import { render, screen as rtlScreen } from '@testing-library/react';
import {
Pagination,
type RenderPaginationItemAriaLabel,
type RenderPaginationLink,
} from './pagination';
const pagePrefix = '#page-';
const backwardLabel = 'omnis assumenda ex';
const forwardLabel = 'voluptatum aut molestiae';
const currentPageLabelPrefix = 'est nostrum a';
const pageLabelPrefix = 'reprehenderit qui unde';
const renderLink: RenderPaginationLink = (num: number) => `${pagePrefix}${num}`;
const renderItemAriaLabel: RenderPaginationItemAriaLabel = ({
kind,
pageNumber,
isCurrentPage,
}) => {
switch (kind) {
case 'backward':
return backwardLabel;
case 'forward':
return forwardLabel;
case 'number':
default:
return isCurrentPage
? `${currentPageLabelPrefix}${pageNumber}`
: `${pageLabelPrefix}${pageNumber}`;
}
};
describe('Pagination', () => {
it('renders a list of items in a nav element', () => {
const ariaLabel = 'expedita repellat rem';
const current = 1;
const total = 1;
render(
<Pagination
aria-label={ariaLabel}
current={current}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
total={total}
/>
);
expect(
rtlScreen.getByRole('navigation', { name: ariaLabel })
).toBeInTheDocument();
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(total);
});
it('can render a forward link when there is more than one page', () => {
const ariaLabel = 'expedita repellat rem';
const current = 1;
const total = 3;
render(
<Pagination
aria-label={ariaLabel}
current={current}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
total={total}
/>
);
expect(
rtlScreen.getByRole('link', { name: forwardLabel })
).toBeInTheDocument();
// the pages links + the forward link
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(total + 1);
});
it('can render a backward link when the last page is selected', () => {
const ariaLabel = 'expedita repellat rem';
const total = 3;
render(
<Pagination
aria-label={ariaLabel}
current={total}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
total={total}
/>
);
expect(
rtlScreen.getByRole('link', { name: backwardLabel })
).toBeInTheDocument();
// the pages links + the backward link
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(total + 1);
});
it('can skip next pages when the total is high and first page is selected', () => {
const ariaLabel = 'expedita repellat rem';
const current = 1;
const total = 50;
/*
* First page & the two next pages + 1 ellipsis + last page + forward link
*/
const expectedItemsCount = 6;
render(
<Pagination
aria-label={ariaLabel}
current={current}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
total={total}
/>
);
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(expectedItemsCount);
// The current page and the ellipsis should not be linked.
expect(rtlScreen.getAllByRole('link')).toHaveLength(expectedItemsCount - 2);
});
it('can skip previous pages when the total is high and last page is selected', () => {
const ariaLabel = 'expedita repellat rem';
const current = 50;
const total = 50;
/*
* Last page & the two previous pages + 1 ellipsis + first page + backward
* link
*/
const expectedItemsCount = 6;
render(
<Pagination
aria-label={ariaLabel}
current={current}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
total={total}
/>
);
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(expectedItemsCount);
// The current page and the ellipsis should not be linked.
expect(rtlScreen.getAllByRole('link')).toHaveLength(expectedItemsCount - 2);
});
it('can render a custom number of siblings', () => {
const ariaLabel = 'expedita repellat rem';
const siblings = 3;
const current = 10;
const total = 20;
/*
* Current page + 3 siblings on each side + first page + last page + 2
* ellipsis + backward and forward links
*/
const expectedItemsCount = 13;
render(
<Pagination
aria-label={ariaLabel}
current={current}
renderItemAriaLabel={renderItemAriaLabel}
renderLink={renderLink}
siblings={siblings}
total={total}
/>
);
expect(rtlScreen.getAllByRole('listitem')).toHaveLength(expectedItemsCount);
/* eslint-disable-next-line @typescript-eslint/no-magic-numbers -- The
current page and the two ellipsis should not be linked. */
expect(rtlScreen.getAllByRole('link')).toHaveLength(expectedItemsCount - 3);
});
});
|