|
| 1 | +import { fireEvent, render } from '@testing-library/react'; |
| 2 | +import { RecommendationPagination, RecommendationPaginationProps } from './index'; |
| 3 | + |
| 4 | +describe('RecommendationPagination', () => { |
| 5 | + it('renders pagination buttons when shouldRenderPagination is true', () => { |
| 6 | + const props: RecommendationPaginationProps = { |
| 7 | + gotoNextPage: jest.fn(), |
| 8 | + gotoPreviousPage: jest.fn(), |
| 9 | + handleApplyRecommendation: jest.fn(), |
| 10 | + shouldRenderPagination: true, |
| 11 | + currentPage: 1, |
| 12 | + totalPages: 5, |
| 13 | + }; |
| 14 | + const { getByTestId } = render(<RecommendationPagination {...props} />); |
| 15 | + const applyButton = getByTestId('apply-button'); |
| 16 | + const gotoPreviousButton = getByTestId('goto-previous-button'); |
| 17 | + const gotoNextButton = getByTestId('goto-next-button'); |
| 18 | + |
| 19 | + expect(applyButton).toBeInTheDocument(); |
| 20 | + expect(gotoPreviousButton).toBeInTheDocument(); |
| 21 | + expect(gotoNextButton).toBeInTheDocument(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('does not render pagination buttons when shouldRenderPagination is false', () => { |
| 25 | + const props: RecommendationPaginationProps = { |
| 26 | + gotoNextPage: jest.fn(), |
| 27 | + gotoPreviousPage: jest.fn(), |
| 28 | + handleApplyRecommendation: jest.fn(), |
| 29 | + shouldRenderPagination: false, |
| 30 | + currentPage: 1, |
| 31 | + totalPages: 5, |
| 32 | + }; |
| 33 | + |
| 34 | + const { getByTestId, queryByTestId } = render(<RecommendationPagination {...props} />); |
| 35 | + |
| 36 | + expect(getByTestId('apply-button')).toBeInTheDocument(); |
| 37 | + expect(queryByTestId('goto-previous-button')).toBeNull(); |
| 38 | + expect(queryByTestId('goto-next-button')).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('calls gotoNextPage when Next Page button is clicked', () => { |
| 42 | + const gotoNextPageMock = jest.fn(); |
| 43 | + const props: RecommendationPaginationProps = { |
| 44 | + gotoNextPage: gotoNextPageMock, |
| 45 | + gotoPreviousPage: jest.fn(), |
| 46 | + handleApplyRecommendation: jest.fn(), |
| 47 | + shouldRenderPagination: true, |
| 48 | + currentPage: 1, |
| 49 | + totalPages: 5, |
| 50 | + }; |
| 51 | + const { getByTestId } = render(<RecommendationPagination {...props} />); |
| 52 | + |
| 53 | + const gotoNextButton = getByTestId('goto-next-button'); |
| 54 | + fireEvent.click(gotoNextButton); |
| 55 | + expect(gotoNextPageMock).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('calls gotoPreviousPage when Previous Page button is clicked', () => { |
| 59 | + const gotoPreviousPageMock = jest.fn(); |
| 60 | + const props: RecommendationPaginationProps = { |
| 61 | + gotoNextPage: jest.fn(), |
| 62 | + gotoPreviousPage: gotoPreviousPageMock, |
| 63 | + handleApplyRecommendation: jest.fn(), |
| 64 | + shouldRenderPagination: true, |
| 65 | + currentPage: 1, |
| 66 | + totalPages: 5, |
| 67 | + }; |
| 68 | + const { getByTestId } = render(<RecommendationPagination {...props} />); |
| 69 | + |
| 70 | + const gotoPreviousButton = getByTestId('goto-previous-button'); |
| 71 | + fireEvent.click(gotoPreviousButton); |
| 72 | + expect(gotoPreviousPageMock).toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('calls handleApplyRecommendation when Apply button is clicked', () => { |
| 76 | + const handleApplyRecommendationMock = jest.fn(); |
| 77 | + const props: RecommendationPaginationProps = { |
| 78 | + gotoNextPage: jest.fn(), |
| 79 | + gotoPreviousPage: jest.fn(), |
| 80 | + handleApplyRecommendation: handleApplyRecommendationMock, |
| 81 | + shouldRenderPagination: true, |
| 82 | + currentPage: 1, |
| 83 | + totalPages: 5, |
| 84 | + }; |
| 85 | + |
| 86 | + const { getByTestId } = render(<RecommendationPagination {...props} />); |
| 87 | + |
| 88 | + const applyButton = getByTestId('apply-button'); |
| 89 | + fireEvent.click(applyButton); |
| 90 | + expect(handleApplyRecommendationMock).toHaveBeenCalled(); |
| 91 | + }); |
| 92 | +}); |
0 commit comments