Skip to content

Commit e3a2bdf

Browse files
authored
Improve code coverage in Pagination.tsx (PalisadoesFoundation#3246)
1 parent e343b0c commit e3a2bdf

File tree

2 files changed

+96
-44
lines changed

2 files changed

+96
-44
lines changed

src/components/Pagination/Pagination.spec.tsx

Lines changed: 96 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,115 @@ import { createTheme, ThemeProvider } from '@mui/material/styles';
66
import Pagination from './Pagination';
77
import { store } from 'state/store';
88
import userEvent from '@testing-library/user-event';
9-
import { describe, it } from 'vitest';
10-
11-
describe('Testing Pagination component', () => {
12-
const props = {
13-
count: 5,
14-
page: 10,
15-
rowsPerPage: 5,
16-
onPageChange: (): number => {
17-
return 10;
18-
},
9+
import { describe, it, vi, expect } from 'vitest';
10+
11+
describe('Pagination component tests', () => {
12+
const mockOnPageChange = vi.fn();
13+
14+
const defaultProps = {
15+
count: 20, // Total items
16+
page: 2, // Current page
17+
rowsPerPage: 5, // Items per page
18+
onPageChange: mockOnPageChange, // Mocked callback for page change
1919
};
2020

21-
it('Component should be rendered properly on rtl', async () => {
21+
it('should render all pagination buttons and invoke onPageChange for navigation', async () => {
2222
render(
2323
<BrowserRouter>
2424
<Provider store={store}>
25-
<Pagination {...props} />
25+
<Pagination {...defaultProps} />
2626
</Provider>
2727
</BrowserRouter>,
2828
);
29+
30+
// Ensure all buttons are rendered
31+
expect(screen.getByTestId('firstPage')).toBeInTheDocument();
32+
expect(screen.getByTestId('previousPage')).toBeInTheDocument();
33+
expect(screen.getByTestId('nextPage')).toBeInTheDocument();
34+
expect(screen.getByTestId('lastPage')).toBeInTheDocument();
35+
36+
// Simulate button clicks and verify callback invocation
37+
await act(async () => {
38+
userEvent.click(screen.getByTestId('nextPage'));
39+
});
40+
expect(mockOnPageChange).toHaveBeenCalledWith(expect.anything(), 3); // Next page
41+
42+
await act(async () => {
43+
userEvent.click(screen.getByTestId('previousPage'));
44+
});
45+
expect(mockOnPageChange).toHaveBeenCalledWith(expect.anything(), 1); // Previous page
46+
47+
await act(async () => {
48+
userEvent.click(screen.getByTestId('firstPage'));
49+
});
50+
expect(mockOnPageChange).toHaveBeenCalledWith(expect.anything(), 0); // First page
51+
2952
await act(async () => {
30-
userEvent.click(screen.getByTestId(/nextPage/i));
31-
userEvent.click(screen.getByTestId(/previousPage/i));
53+
userEvent.click(screen.getByTestId('lastPage'));
3254
});
55+
expect(mockOnPageChange).toHaveBeenCalledWith(expect.anything(), 3); // Last page
3356
});
34-
});
3557

36-
const props = {
37-
count: 5,
38-
page: 10,
39-
rowsPerPage: 5,
40-
onPageChange: (): number => {
41-
return 10;
42-
},
43-
theme: { direction: 'rtl' },
44-
};
45-
46-
it('Component should be rendered properly', async () => {
47-
const theme = createTheme({
48-
direction: 'rtl',
58+
it('should disable navigation buttons at the boundaries', () => {
59+
render(
60+
<BrowserRouter>
61+
<Provider store={store}>
62+
<Pagination {...defaultProps} page={0} />
63+
</Provider>
64+
</BrowserRouter>,
65+
);
66+
67+
// First and Previous buttons should be disabled on the first page
68+
expect(screen.getByTestId('firstPage')).toBeDisabled();
69+
expect(screen.getByTestId('previousPage')).toBeDisabled();
70+
71+
// Next and Last buttons should not be disabled
72+
expect(screen.getByTestId('nextPage')).not.toBeDisabled();
73+
expect(screen.getByTestId('lastPage')).not.toBeDisabled();
4974
});
5075

51-
render(
52-
<BrowserRouter>
53-
<Provider store={store}>
54-
<ThemeProvider theme={theme}>
55-
<Pagination {...props} />
56-
</ThemeProvider>
57-
</Provider>
58-
</BrowserRouter>,
59-
);
60-
61-
await act(async () => {
62-
userEvent.click(screen.getByTestId(/nextPage/i));
63-
userEvent.click(screen.getByTestId(/previousPage/i));
76+
it('should render correctly with RTL direction', async () => {
77+
const rtlTheme = createTheme({ direction: 'rtl' });
78+
79+
render(
80+
<BrowserRouter>
81+
<Provider store={store}>
82+
<ThemeProvider theme={rtlTheme}>
83+
<Pagination {...defaultProps} />
84+
</ThemeProvider>
85+
</Provider>
86+
</BrowserRouter>,
87+
);
88+
89+
// Verify buttons render properly in RTL mode
90+
expect(screen.getByTestId('firstPage')).toBeInTheDocument();
91+
expect(screen.getByTestId('lastPage')).toBeInTheDocument();
92+
93+
// Simulate a button click in RTL mode
94+
await act(async () => {
95+
userEvent.click(screen.getByTestId('nextPage'));
96+
});
97+
expect(mockOnPageChange).toHaveBeenCalledWith(expect.anything(), 3); // Next page
98+
});
99+
100+
it('should disable Next and Last buttons on the last page', () => {
101+
render(
102+
<BrowserRouter>
103+
<Provider store={store}>
104+
<Pagination
105+
{...defaultProps}
106+
page={Math.ceil(defaultProps.count / defaultProps.rowsPerPage) - 1}
107+
/>
108+
</Provider>
109+
</BrowserRouter>,
110+
);
111+
112+
// Next and Last buttons should be disabled on the last page
113+
expect(screen.getByTestId('nextPage')).toBeDisabled();
114+
expect(screen.getByTestId('lastPage')).toBeDisabled();
115+
116+
// First and Previous buttons should not be disabled
117+
expect(screen.getByTestId('firstPage')).not.toBeDisabled();
118+
expect(screen.getByTestId('previousPage')).not.toBeDisabled();
64119
});
65120
});

src/components/Pagination/Pagination.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function pagination(props: InterfaceTablePaginationActionsProps): JSX.Element {
3737
*
3838
* @param event - The click event.
3939
*/
40-
/* istanbul ignore next */
4140
const handleFirstPageButtonClick = (
4241
event: React.MouseEvent<HTMLButtonElement>,
4342
): void => {
@@ -63,7 +62,6 @@ function pagination(props: InterfaceTablePaginationActionsProps): JSX.Element {
6362
* @param event - The click event.
6463
*/
6564

66-
/* istanbul ignore next */
6765
const handleNextButtonClick = (
6866
event: React.MouseEvent<HTMLButtonElement>,
6967
): void => {
@@ -76,7 +74,6 @@ function pagination(props: InterfaceTablePaginationActionsProps): JSX.Element {
7674
*
7775
* @param event - The click event.
7876
*/
79-
/* istanbul ignore next */
8077
const handleLastPageButtonClick = (
8178
event: React.MouseEvent<HTMLButtonElement>,
8279
): void => {

0 commit comments

Comments
 (0)