-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.test.js
159 lines (138 loc) · 4.06 KB
/
index.test.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
import React from 'react';
import renderer from 'react-test-renderer';
// eslint-disable-next-line unicorn/import-index
import Pagination, { Page, getVisiblePages } from '../index';
const render = element => renderer.create(element).toJSON();
describe('Page', () => {
test('should display a numbered element', () => {
expect(
render(<Page currentPage={1} index={19} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should be different for matching current', () => {
expect(
render(<Page currentPage={1} index={1} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should use className', () => {
expect(
renderer
.create(
<Page
currentPage={1}
index={1}
className="foo-bar"
onChange={() => {}}
/>
)
.root.findByProps({ className: 'foo-bar' })
).toBeDefined();
});
test('should use onChange callback', () => {
const onChange = jest.fn();
const tree = renderer.create(
<Page currentPage={1} index={1} onChange={onChange} />
);
tree.root.find(e => e.type === 'a').props.onClick();
expect(onChange).toHaveBeenCalledWith(1);
});
});
describe('getVisiblePages', () => {
test('at start of list', () => {
expect(getVisiblePages(1, 1, 10)).toEqual([1, 2, 3]);
});
test('at end of list', () => {
expect(getVisiblePages(1, 10, 10)).toEqual([8, 9, 10]);
});
test('in the middle of a list', () => {
expect(getVisiblePages(1, 5, 10)).toEqual([4, 5, 6]);
});
test('less pages than radius', () => {
expect(getVisiblePages(1, 1, 2)).toEqual([1, 2]);
});
});
describe('Pagination', () => {
test('should render pages at start', () => {
expect(
render(<Pagination pages={10} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should render pages at start with many', () => {
expect(
render(
<Pagination
pages={10}
currentPage={4}
visibleRadius={2}
onChange={() => {}}
/>
)
).toMatchSnapshot();
});
test('should render pages in middle', () => {
expect(
render(<Pagination pages={10} currentPage={5} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should render pages at end', () => {
expect(
render(<Pagination pages={10} currentPage={10} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should render with no ellipses if not needed - visible == max', () => {
expect(
render(<Pagination pages={3} currentPage={1} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should render with no ellipses if not needed - end', () => {
expect(
render(<Pagination pages={10} currentPage={8} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should render with no ellipses if not needed - start', () => {
expect(
render(<Pagination pages={10} currentPage={3} onChange={() => {}} />)
).toMatchSnapshot();
});
test('should use boolean flags as classnames', () => {
expect(
render(
<Pagination
isRounded
isCentered
isLarge
isMedium
isRight
isSmall
pages={10}
currentPage={3}
onChange={() => {}}
/>
)
).toMatchSnapshot();
});
test('should call callback when previous clicked', () => {
const onChange = jest.fn();
const tree = renderer.create(
<Pagination currentPage={1} pages={10} onChange={onChange} />
);
tree.root
.find(
e => e.type === 'a' && e.props.className.includes('pagination-previous')
)
.props.onClick();
expect(onChange).toHaveBeenCalledWith(0);
});
test('should call callback when next clicked', () => {
const onChange = jest.fn();
const tree = renderer.create(
<Pagination currentPage={1} pages={10} onChange={onChange} />
);
tree.root
.find(
e => e.type === 'a' && e.props.className.includes('pagination-next')
)
.props.onClick();
expect(onChange).toHaveBeenCalledWith(2);
});
});