Skip to content

Commit b736891

Browse files
LuisValgoiMarcusNotheis
authored andcommitted
Merge pull request #61 from LuisValgoi/test-pagination
[REFACTOR] Tests for Pagination Engine
1 parent f529790 commit b736891

File tree

2 files changed

+126
-19
lines changed

2 files changed

+126
-19
lines changed
Lines changed: 103 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22

33
import '@testing-library/jest-dom/extend-expect';
4-
import { render, screen } from '../../util/TestSetup';
4+
import { render, screen, fireEvent } from '../../util/TestSetup';
55

66
import { Pagination } from './Pagination';
77

@@ -11,9 +11,21 @@ const fourButtonsPagination = {
1111
number: 0,
1212
};
1313

14+
const fourButtonsSelectedAtLastPagination = {
15+
numberOfElements: 11,
16+
totalPages: 2,
17+
number: 1,
18+
};
19+
20+
const sixButtonsPagination = {
21+
numberOfElements: 61,
22+
totalPages: 7,
23+
number: 3,
24+
};
25+
1426
describe('Pagination.js Test Suite', () => {
1527
beforeEach(() => {
16-
render(<Pagination numberOfElements={fourButtonsPagination.numberOfElements} totalPages={fourButtonsPagination.totalPages} selectedPage={fourButtonsPagination.number} />);
28+
render(<Pagination numberOfElements={sixButtonsPagination.numberOfElements} totalPages={sixButtonsPagination.totalPages} selectedPage={sixButtonsPagination.number} />);
1729
});
1830

1931
test('should match snapshot', () => {
@@ -22,33 +34,108 @@ describe('Pagination.js Test Suite', () => {
2234
expect(pagination).toMatchSnapshot();
2335
});
2436

25-
test('should have 4 buttons (< 1 2 >) ', () => {
37+
test('should have 6 buttons (< 2 3 4 5 6 >) with 61 records and a size of 10', () => {
2638
const pagination = screen.getByTestId('pagination-wrapper');
27-
const leftarrow = screen.getByTestId('leftarrow-pagination-wrapper');
39+
const leftArrow = screen.getByTestId('leftarrow-pagination-wrapper');
40+
const morePreviousPage = screen.getByTestId('morePreviousPage-pagination-wrapper');
41+
const previousPage = screen.getByTestId('previousPage-pagination-wrapper');
2842
const selectedPage = screen.getByTestId('selectedPage-pagination-wrapper');
2943
const nextPage = screen.getByTestId('nextPage-pagination-wrapper');
30-
const afterarrow = screen.getByTestId('afterarrow-pagination-wrapper');
44+
const moreNextPage = screen.getByTestId('moreNextPage-pagination-wrapper');
45+
const afterArrow = screen.getByTestId('afterarrow-pagination-wrapper');
3146

3247
expect(pagination).toBeInTheDocument();
33-
expect(leftarrow).toHaveTextContent('<');
34-
expect(selectedPage).toHaveTextContent('1');
35-
expect(nextPage).toHaveTextContent('2');
36-
expect(afterarrow).toHaveTextContent('>');
48+
expect(leftArrow).toHaveTextContent('<');
49+
expect(morePreviousPage).toHaveTextContent('2');
50+
expect(previousPage).toHaveTextContent('3');
51+
expect(selectedPage).toHaveTextContent('4');
52+
expect(nextPage).toHaveTextContent('5');
53+
expect(moreNextPage).toHaveTextContent('6');
54+
expect(afterArrow).toHaveTextContent('>');
3755
});
3856

39-
test('should have button < disabled', () => {
57+
test('should have button 4 selected', () => {
4058
const pagination = screen.getByTestId('pagination-wrapper');
41-
const leftarrow = screen.getByTestId('leftarrow-pagination-wrapper');
59+
const selectedPage = screen.getByTestId('selectedPage-pagination-wrapper');
4260

4361
expect(pagination).toBeInTheDocument();
44-
expect(leftarrow).toHaveProperty('disabled', true);
62+
expect(selectedPage).toHaveProperty('design', 'Emphasized');
4563
});
4664

47-
test('should have button 1 selected', () => {
48-
const pagination = screen.getByTestId('pagination-wrapper');
49-
const selectedPage = screen.getByTestId('selectedPage-pagination-wrapper');
65+
test('should increment selected page when nextPage is selected', async () => {
66+
let selectedPageCount = 3;
67+
let setSelectedPageCount = (params) => {
68+
selectedPageCount = params;
69+
};
70+
render(
71+
<Pagination
72+
numberOfElements={sixButtonsPagination.numberOfElements}
73+
totalPages={sixButtonsPagination.totalPages}
74+
selectedPage={sixButtonsPagination.number}
75+
setPage={(params) => setSelectedPageCount(params)}
76+
/>,
77+
);
78+
const pagination = screen.getAllByTestId('pagination-wrapper')[1];
79+
const afterArrow = screen.getAllByTestId('afterarrow-pagination-wrapper')[1];
5080

5181
expect(pagination).toBeInTheDocument();
52-
expect(selectedPage).toHaveProperty('design', 'Emphasized');
82+
expect(selectedPageCount).toBe(3);
83+
fireEvent.click(afterArrow);
84+
expect(selectedPageCount).toBe(4);
85+
});
86+
87+
test('should decrease selected page when nextPage is selected', async () => {
88+
let selectedPageCount = 3;
89+
let setSelectedPageCount = (params) => {
90+
selectedPageCount = params;
91+
};
92+
render(
93+
<Pagination
94+
numberOfElements={sixButtonsPagination.numberOfElements}
95+
totalPages={sixButtonsPagination.totalPages}
96+
selectedPage={sixButtonsPagination.number}
97+
setPage={(params) => setSelectedPageCount(params)}
98+
/>,
99+
);
100+
const pagination = screen.getAllByTestId('pagination-wrapper')[1];
101+
const leftArrow = screen.getAllByTestId('leftarrow-pagination-wrapper')[1];
102+
103+
expect(pagination).toBeInTheDocument();
104+
expect(selectedPageCount).toBe(3);
105+
fireEvent.click(leftArrow);
106+
expect(selectedPageCount).toBe(2);
107+
});
108+
109+
test('should have button < disabled when first page is selected in a example with 2 pages', () => {
110+
render(<Pagination numberOfElements={fourButtonsPagination.numberOfElements} totalPages={fourButtonsPagination.totalPages} selectedPage={fourButtonsPagination.number} />);
111+
112+
const pagination = screen.getAllByTestId('pagination-wrapper')[1];
113+
const leftarrow = screen.getAllByTestId('leftarrow-pagination-wrapper')[1];
114+
115+
expect(pagination).toBeInTheDocument();
116+
expect(leftarrow).toHaveProperty('disabled', true);
117+
});
118+
119+
test('should have button > disabled when last page is selected in a example with 2 pages', () => {
120+
render(
121+
<Pagination
122+
numberOfElements={fourButtonsSelectedAtLastPagination.numberOfElements}
123+
totalPages={fourButtonsSelectedAtLastPagination.totalPages}
124+
selectedPage={fourButtonsSelectedAtLastPagination.number}
125+
/>,
126+
);
127+
128+
const afterarrow = screen.getAllByTestId('afterarrow-pagination-wrapper')[1];
129+
130+
expect(afterarrow).toBeInTheDocument();
131+
expect(afterarrow).toHaveProperty('disabled', true);
132+
});
133+
134+
test('should render nothing (return undefined) when no pagination data is passed', () => {
135+
render(<Pagination numberOfElements={null} totalPages={null} selectedPage={null} />);
136+
137+
const pagination = screen.getAllByTestId('pagination-wrapper')[1];
138+
139+
expect(pagination).toBeUndefined();
53140
});
54141
});

packages/ui5-webcomponents-react-seed/src/components/Pagination/__snapshots__/Pagination.test.js.snap

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,43 @@ exports[`Pagination.js Test Suite should match snapshot 1`] = `
99
<ui5-button
1010
data-testid="leftarrow-pagination-wrapper"
1111
design="Transparent"
12-
disabled="true"
1312
>
1413
&lt;
1514
</ui5-button>
15+
<ui5-button
16+
data-testid="morePreviousPage-pagination-wrapper"
17+
design="Transparent"
18+
style="margin-right: 0.5rem;"
19+
>
20+
2
21+
</ui5-button>
22+
<ui5-button
23+
data-testid="previousPage-pagination-wrapper"
24+
design="Transparent"
25+
style="margin-right: 0.5rem;"
26+
>
27+
3
28+
</ui5-button>
1629
<ui5-button
1730
data-testid="selectedPage-pagination-wrapper"
1831
design="Emphasized"
1932
style="margin-right: 0.5rem;"
2033
>
21-
1
34+
4
2235
</ui5-button>
2336
<ui5-button
2437
data-testid="nextPage-pagination-wrapper"
2538
design="Transparent"
2639
style="margin-right: 0.5rem;"
2740
>
28-
2
41+
5
42+
</ui5-button>
43+
<ui5-button
44+
data-testid="moreNextPage-pagination-wrapper"
45+
design="Transparent"
46+
style="margin-right: 0.5rem;"
47+
>
48+
6
2949
</ui5-button>
3050
<ui5-button
3151
data-testid="afterarrow-pagination-wrapper"

0 commit comments

Comments
 (0)