forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaginationSpec.js
More file actions
305 lines (263 loc) · 9.33 KB
/
PaginationSpec.js
File metadata and controls
305 lines (263 loc) · 9.33 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
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
import Pagination from '../src/Pagination';
describe('<Pagination>', () => {
it('should have class', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination>Item content</Pagination>
);
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'pagination'));
});
it('should show the correct active button', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
items={5}
activePage={3}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons.length, 5);
pageButtons[2].className.should.match(/\bactive\b/);
});
it('should call onSelect when page button is selected', (done) => {
function onSelect(eventKey) {
assert.equal(eventKey, 2);
done();
}
const instance = ReactTestUtils.renderIntoDocument(
<Pagination items={5} onSelect={onSelect} />
);
ReactTestUtils.Simulate.click(
ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'a')[1]
);
});
it('should only show part of buttons and active button in the middle of buttons when given maxButtons', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
items={30}
activePage={10}
maxButtons={9}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// 9 visible page buttons and 1 ellipsis button
assert.equal(pageButtons.length, 10);
// active button is the second one
assert.equal(pageButtons[0].firstChild.textContent, '6');
pageButtons[4].className.should.match(/\bactive\b/);
});
it('should show the ellipsis, boundaryLinks, first, last, prev and next button with default labels', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
first
last
prev
next
ellipsis
maxButtons={3}
activePage={10}
items={20}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons.length, 8);
assert.equal(pageButtons[0].textContent, '«');
assert.equal(pageButtons[1].textContent, '‹');
assert.equal(pageButtons[5].textContent, '…');
assert.equal(pageButtons[6].textContent, '›');
assert.equal(pageButtons[7].textContent, '»');
});
it('should show the boundaryLinks, first, last, prev and next button', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
first
last
prev
next
ellipsis
boundaryLinks
maxButtons={3}
activePage={10}
items={20}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons[2].textContent, '1');
assert.equal(pageButtons[3].textContent, '…');
assert.equal(pageButtons[7].textContent, '…');
assert.equal(pageButtons[8].textContent, '20');
});
it('should show the ellipsis, first, last, prev and next button with custom labels', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
first="first"
last="last"
prev="prev"
next="next"
ellipsis="more"
maxButtons={3}
activePage={10}
items={20}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons.length, 8);
assert.equal(pageButtons[0].textContent, 'first');
assert.equal(pageButtons[1].textContent, 'prev');
assert.equal(pageButtons[5].textContent, 'more');
assert.equal(pageButtons[6].textContent, 'next');
assert.equal(pageButtons[7].textContent, 'last');
});
it('should enumerate pagenums correctly when ellipsis=true', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
first
last
prev
next
ellipsis
maxButtons={5}
activePage={1}
items={1}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].textContent, '«');
assert.equal(pageButtons[1].textContent, '‹');
assert.equal(pageButtons[2].textContent, '1');
assert.equal(pageButtons[3].textContent, '›');
assert.equal(pageButtons[4].textContent, '»');
});
it('should render next and last buttons as disabled when items=0 and ellipsis=true', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
last
next
ellipsis
maxButtons={1}
activePage={1}
items={0}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].textContent, '›');
assert.equal(pageButtons[1].textContent, '»');
assert.include(pageButtons[0].className, 'disabled');
assert.include(pageButtons[1].className, 'disabled');
});
it('should wrap buttons in SafeAnchor when no buttonComponentClass prop is supplied', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
maxButtons={2}
activePage={1}
items={2}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].children[0].tagName, 'A');
assert.equal(pageButtons[1].children[0].tagName, 'A');
assert.equal(pageButtons[0].children[0].getAttribute('href'), '#');
assert.equal(pageButtons[1].children[0].getAttribute('href'), '#');
});
it('should wrap each button in a buttonComponentClass when it is present', () => {
class DummyElement extends React.Component {
render() {
return <div>{this.props.children}</div>;
}
}
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
maxButtons={2}
activePage={1}
items={2}
buttonComponentClass={DummyElement}
/>
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].children[0].tagName, 'DIV');
assert.equal(pageButtons[1].children[0].tagName, 'DIV');
});
it('should call onSelect with custom buttonComponentClass', (done) => {
class DummyElement extends React.Component {
render() {
return <div onClick={this.props.onClick} />;
}
}
function onSelect(eventKey) {
assert.equal(eventKey, 3);
done();
}
const instance = ReactTestUtils.renderIntoDocument(
<Pagination items={5} onSelect={onSelect} buttonComponentClass={DummyElement} />
);
ReactTestUtils.Simulate.click(
ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'div')[2]
);
});
it('should not fire "onSelect" event on disabled buttons', () => {
function onSelect() {
throw Error('this event should not happen');
}
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
onSelect={onSelect}
last
next
ellipsis
maxButtons={1}
activePage={1}
items={0}
/>
);
const liElements = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// buttons are disabled
assert.include(liElements[0].className, 'disabled');
assert.include(liElements[1].className, 'disabled');
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'a');
const nextButton = pageButtons[0];
const lastButton = pageButtons[1];
ReactTestUtils.Simulate.click(nextButton);
ReactTestUtils.Simulate.click(lastButton);
});
it('should pass page number to buttonComponentClass', () => {
class DummyElement extends React.Component {
render() {
return (
<a href={`?page=${this.props.eventKey}`}>
{this.props.eventKey}
</a>
);
}
}
const instance = ReactTestUtils.renderIntoDocument(
<Pagination items={5} buttonComponentClass={DummyElement} />
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'a');
assert.equal(pageButtons[1].getAttribute('href'), '?page=2');
});
it('should render three items from 1 to 3 when activePage=1', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination items={4} maxButtons={3} activePage={1} />
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].textContent, '1');
assert.equal(pageButtons[1].textContent, '2');
assert.equal(pageButtons[2].textContent, '3');
});
it('should render three items from 2 to 4 when activePage=3', () => {
const instance = ReactTestUtils.renderIntoDocument(
<Pagination
onSelect={()=>{}}
items={4}
maxButtons={3}
activePage={3} />
);
const pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
assert.equal(pageButtons[0].textContent, '2');
assert.equal(pageButtons[1].textContent, '3');
assert.equal(pageButtons[2].textContent, '4');
});
});