|
1 | 1 | import React from 'react'; |
2 | 2 | import PageButtons from './page-buttons'; |
3 | | -import { render } from '@testing-library/react'; |
| 3 | +import { fireEvent, render } from '@testing-library/react'; |
4 | 4 | import { RecoilRoot } from 'recoil'; |
5 | 5 |
|
6 | 6 | describe('PageButtons component', () => { |
@@ -86,4 +86,53 @@ describe('PageButtons component', () => { |
86 | 86 | const lastButton = getByRole('button', { name: `${tableName}-page10` }); |
87 | 87 | expect(lastButton).toBeInTheDocument(); |
88 | 88 | }); |
| 89 | + |
| 90 | + it('correctly changes to a new page when its button-number is clicked', () => { |
| 91 | + const { getByRole } = render( |
| 92 | + <RecoilRoot> |
| 93 | + <PageButtons pageButtonProps={pageButtonProps} /> |
| 94 | + </RecoilRoot> |
| 95 | + ); |
| 96 | + const secondButton = getByRole('button', { name: `${tableName}-page2` }); |
| 97 | + expect(secondButton).toBeInTheDocument(); |
| 98 | + fireEvent.click(secondButton); |
| 99 | + expect(secondButton).toHaveClass('active'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('correctly changes pages when clicking the next button', () => { |
| 103 | + pageButtonProps.currentPage = 1; |
| 104 | + const { getByRole } = render( |
| 105 | + <RecoilRoot> |
| 106 | + <PageButtons pageButtonProps={pageButtonProps} /> |
| 107 | + </RecoilRoot> |
| 108 | + ); |
| 109 | + const nextButton = getByRole('button', { name: 'Next page' }); |
| 110 | + fireEvent.click(nextButton); |
| 111 | + const secondButton = getByRole('button', { name: `${tableName}-page2` }); |
| 112 | + expect(secondButton).toHaveClass('active'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('correctly changes pages when clicking the previous button', () => { |
| 116 | + pageButtonProps.currentPage = 2; |
| 117 | + const { getByRole } = render( |
| 118 | + <RecoilRoot> |
| 119 | + <PageButtons pageButtonProps={pageButtonProps} /> |
| 120 | + </RecoilRoot> |
| 121 | + ); |
| 122 | + const prevButton = getByRole('button', { name: 'Previous page' }); |
| 123 | + fireEvent.click(prevButton); |
| 124 | + const firstButton = getByRole('button', { name: `${tableName}-page1` }); |
| 125 | + expect(firstButton).toHaveClass('active'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('renders the correct page range and ellipsis (when range > 7)', () => { |
| 129 | + pageButtonProps.maxPage = 8; |
| 130 | + const { getByRole } = render( |
| 131 | + <RecoilRoot> |
| 132 | + <PageButtons pageButtonProps={pageButtonProps} /> |
| 133 | + </RecoilRoot> |
| 134 | + ); |
| 135 | + const ellipsisButton = getByRole('button', { name: 'Page number overflow ellipsis' }); |
| 136 | + expect(ellipsisButton).toBeInTheDocument(); |
| 137 | + }); |
89 | 138 | }); |
0 commit comments