|
| 1 | +import { buildPatientDetails } from '../../helpers/test/testBuilders'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { runAxeTest } from '../../helpers/test/axeTestHelper'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi, Mock } from 'vitest'; |
| 5 | +import DownloadCompletePage from './DownloadCompletePage'; |
| 6 | +import userEvent from '@testing-library/user-event'; |
| 7 | +import { routes } from '../../types/generic/routes'; |
| 8 | +import usePatient from '../../helpers/hooks/usePatient'; |
| 9 | + |
| 10 | +vi.mock('../../helpers/hooks/usePatient'); |
| 11 | + |
| 12 | +const mockedUseNavigate = vi.fn(); |
| 13 | +const mockUsePatient = usePatient as Mock; |
| 14 | + |
| 15 | +vi.mock('react-router-dom', () => ({ |
| 16 | + useNavigate: () => mockedUseNavigate, |
| 17 | +})); |
| 18 | + |
| 19 | +describe('DownloadCompletePage', () => { |
| 20 | + const mockPatient = buildPatientDetails(); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + import.meta.env.VITE_ENVIRONMENT = 'vitest'; |
| 24 | + mockUsePatient.mockReturnValue(mockPatient); |
| 25 | + }); |
| 26 | + afterEach(() => { |
| 27 | + vi.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('renders the download complete screen', () => { |
| 31 | + render(<DownloadCompletePage />); |
| 32 | + |
| 33 | + expect(screen.getByTestId('page-title')).toBeInTheDocument(); |
| 34 | + expect( |
| 35 | + screen.getByText(`Patient name: ${mockPatient.familyName}, ${mockPatient.givenName}`), |
| 36 | + ).toBeInTheDocument(); |
| 37 | + expect(screen.getByText('Your responsibilities with this record')).toBeInTheDocument(); |
| 38 | + expect( |
| 39 | + screen.getByText('Follow the Record Management Code of Practice'), |
| 40 | + ).toBeInTheDocument(); |
| 41 | + expect( |
| 42 | + screen.getByRole('button', { |
| 43 | + name: 'Go to home', |
| 44 | + }), |
| 45 | + ).toBeInTheDocument(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('navigates to the home screen when go to home is clicked', async () => { |
| 49 | + render(<DownloadCompletePage />); |
| 50 | + |
| 51 | + await userEvent.click(screen.getByRole('button', {name: 'Go to home'})); |
| 52 | + |
| 53 | + expect(mockedUseNavigate).toHaveBeenCalledWith(routes.HOME); |
| 54 | + }); |
| 55 | + |
| 56 | + it('navigates to the home screen if patient details are undefined', async () => { |
| 57 | + mockUsePatient.mockReturnValue(undefined); |
| 58 | + render(<DownloadCompletePage />); |
| 59 | + |
| 60 | + expect(mockedUseNavigate).toHaveBeenCalledWith(routes.HOME); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('Accessibility', () => { |
| 64 | + it('passes accessibility checks', async () => { |
| 65 | + render(<DownloadCompletePage />); |
| 66 | + const results = await runAxeTest(document.body); |
| 67 | + expect(results).toHaveNoViolations(); |
| 68 | + }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments