-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.test.jsx
More file actions
180 lines (146 loc) · 4.44 KB
/
index.test.jsx
File metadata and controls
180 lines (146 loc) · 4.44 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
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { render, screen } from '@testing-library/react';
import { isVisible } from '../../../../../../tests/utilities.mjs';
import BasePagination from '#ui/Common/BasePagination';
const getPageLabel = number => number.toString();
const labels = {
aria: 'Aria',
prevAria: 'Previous Aria',
prev: 'Previous',
nextAria: 'Next Aria',
next: 'Next',
};
function renderPagination({
currentPage = 1,
pages,
currentPageSiblingsCount,
}) {
const parsedPages = new Array(pages)
.fill({ url: 'page' })
.map(item => ({ url: `${item.url}-${Math.random()}` }));
render(
<BasePagination
currentPage={currentPage}
pages={parsedPages}
currentPageSiblingsCount={currentPageSiblingsCount}
getPageLabel={getPageLabel}
labels={labels}
/>
);
return {
currentPage,
parsedPages,
};
}
describe('Pagination', () => {
describe('Rendering', () => {
it('Renders the navigation buttons even if no pages are passed to it', () => {
renderPagination({ currentPage: 0, pages: 0 });
assert.ok(isVisible(screen.getByRole('navigation')));
assert.ok(
isVisible(
screen.getByRole('button', {
name: labels.prevAria,
})
)
);
assert.ok(
isVisible(
screen.getByRole('button', {
name: labels.nextAria,
})
)
);
});
it('Renders the passed pages and current page', () => {
const { currentPage, parsedPages } = renderPagination({
pages: 4,
});
const pageElements = screen.getAllByRole('link');
assert.equal(pageElements.length, parsedPages.length);
pageElements.forEach((page, index) => {
if (index + 1 === currentPage) {
assert.strictEqual(page.getAttribute('aria-current'), 'page');
}
assert.ok(isVisible(page));
});
});
});
describe('Ellipsis behavior', () => {
it('When the pages size is equal or smaller than currentPageSiblingsCount + 5, all pages are shown', () => {
renderPagination({
pages: 6,
});
assert.ok(!screen.queryByText('...')?.ownerDocument);
const pageElements = screen.getAllByRole('link');
assert.deepEqual(
pageElements.map(element => element.textContent),
['1', '2', '3', '4', '5', '6']
);
});
it('Shows left ellipsis when the left sibling of the current page is at least two pages away from the first page', () => {
renderPagination({
currentPage: 5,
pages: 8,
});
assert.ok(isVisible(screen.getByText('...')));
const pageElements = screen.getAllByRole('link');
assert.deepEqual(
pageElements.map(element => element.textContent),
['1', '4', '5', '6', '7', '8']
);
});
it('Shows right ellipsis when the right sibling of the current page is at least two pages away from the last page', () => {
renderPagination({
currentPage: 3,
pages: 8,
});
assert.ok(isVisible(screen.getByText('...')));
const pageElements = screen.getAllByRole('link');
assert.deepEqual(
pageElements.map(element => element.textContent),
['1', '2', '3', '4', '5', '8']
);
});
it('Shows right and left ellipses when the current page siblings are both at least two pages away from the first and last pages', () => {
renderPagination({
currentPage: 5,
pages: 10,
});
assert.equal(screen.getAllByText('...').length, 2);
const pageElements = screen.getAllByRole('link');
assert.deepEqual(
pageElements.map(element => element.textContent),
['1', '4', '5', '6', '10']
);
});
});
describe('Navigation buttons', () => {
it('Disables "Previous" button when the currentPage is equal to the first page', () => {
renderPagination({
pages: 2,
});
assert.ok(
screen
.getByRole('button', {
name: labels.prevAria,
})
.getAttribute('aria-disabled')
);
});
it('Disables "Next" button when the currentPage is equal to the last page', () => {
renderPagination({
currentPage: 2,
pages: 2,
});
assert.ok(
screen
.getByRole('button', {
name: labels.nextAria,
})
.getAttribute('aria-disabled')
);
});
});
});