forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaginationSpec.js
253 lines (217 loc) · 7.89 KB
/
PaginationSpec.js
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
import React from 'react';
import ReactTestUtils from 'react/lib/ReactTestUtils';
import Pagination from '../src/Pagination';
describe('Pagination', () => {
it('Should have class', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination>Item content</Pagination>
);
assert.ok(ReactTestUtils.findRenderedDOMComponentWithClass(instance, 'pagination'));
});
it('Should show the correct active button', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
items={5}
activePage={3} />
);
let 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(event, selectedEvent) {
assert.equal(selectedEvent.eventKey, 2);
done();
}
let 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', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
items={30}
activePage={10}
maxButtons={9} />
);
let 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.innerText, '6');
pageButtons[4].className.should.match(/\bactive\b/);
});
it('Should show the ellipsis, boundaryLinks, first, last, prev and next button with default labels', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
first={true}
last={true}
prev={true}
next={true}
ellipsis={true}
maxButtons={3}
activePage={10}
items={20} />
);
let pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons.length, 8);
assert.equal(pageButtons[0].innerText, '«');
assert.equal(pageButtons[1].innerText, '‹');
assert.equal(pageButtons[5].innerText, '...');
assert.equal(pageButtons[6].innerText, '›');
assert.equal(pageButtons[7].innerText, '»');
});
it('Should show the boundaryLinks, first, last, prev and next button', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
first={true}
last={true}
prev={true}
next={true}
ellipsis={true}
boundaryLinks={true}
maxButtons={3}
activePage={10}
items={20} />
);
let pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons[2].innerText, '1');
assert.equal(pageButtons[3].innerText, '...');
assert.equal(pageButtons[7].innerText, '...');
assert.equal(pageButtons[8].innerText, '20');
});
it('Should show the ellipsis, first, last, prev and next button with custom labels', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
first='first'
last='last'
prev='prev'
next='next'
ellipsis='more'
maxButtons={3}
activePage={10}
items={20} />
);
let pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
// add first, last, prev, next and ellipsis button
assert.equal(pageButtons.length, 8);
assert.equal(pageButtons[0].innerText, 'first');
assert.equal(pageButtons[1].innerText, 'prev');
assert.equal(pageButtons[5].innerText, 'more');
assert.equal(pageButtons[6].innerText, 'next');
assert.equal(pageButtons[7].innerText, '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].innerText, '«');
assert.equal(pageButtons[1].innerText, '‹');
assert.equal(pageButtons[2].innerText, '1');
assert.equal(pageButtons[3].innerText, '›');
assert.equal(pageButtons[4].innerText, '»');
});
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].innerText, '›');
assert.equal(pageButtons[1].innerText, '»');
assert.include(pageButtons[0].className, 'disabled');
assert.include(pageButtons[1].className, 'disabled');
});
it('Should wrap buttons in SafeAnchor when no buttonComponentClass prop is supplied', () => {
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
maxButtons={2}
activePage={1}
items={2} />
);
let pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
let tagName = 'A';
assert.equal(pageButtons[0].children[0].tagName, tagName);
assert.equal(pageButtons[1].children[0].tagName, tagName);
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}/>;
}
}
let instance = ReactTestUtils.renderIntoDocument(
<Pagination
maxButtons={2}
activePage={1}
items={2}
buttonComponentClass={DummyElement} />
);
let pageButtons = ReactTestUtils.scryRenderedDOMComponentsWithTag(instance, 'li');
let tagName = 'DIV';
assert.equal(pageButtons[0].children[0].tagName, tagName);
assert.equal(pageButtons[1].children[0].tagName, tagName);
});
it('Should call onSelect with custom buttonComponentClass', (done) => {
class DummyElement extends React.Component {
render() {
return <div {...this.props}/>;
}
}
function onSelect(event, selectedEvent) {
assert.equal(selectedEvent.eventKey, 3);
done();
}
let 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 );
});
});