|
| 1 | +import { Mock } from 'vitest'; |
| 2 | +import getUserInformation from '../../../../helpers/requests/userPatientRestrictions/getUserInformation'; |
| 3 | +import useSmartcardNumber from '../../../../helpers/hooks/useSmartcardNumber'; |
| 4 | +import { buildUserInformation, buildUserRestrictions } from '../../../../helpers/test/testBuilders'; |
| 5 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 6 | +import UserPatientRestrictionsSearchStaffStage from './UserPatientRestrictionsSearchStaffStage'; |
| 7 | +import userEvent from '@testing-library/user-event'; |
| 8 | +import { routeChildren, routes } from '../../../../types/generic/routes'; |
| 9 | +import { |
| 10 | + userRestrictionsStaffSearchNotFoundError, |
| 11 | + userRestrictionStaffSearchEmptyValueError, |
| 12 | + userRestrictionStaffSearchInvalidFormatError, |
| 13 | + userRestrictionStaffSearchRestrictionExistsError, |
| 14 | +} from '../../../../helpers/constants/errors'; |
| 15 | +import { UIErrorCode } from '../../../../types/generic/errors'; |
| 16 | + |
| 17 | +vi.mock('react-router-dom', () => ({ |
| 18 | + ...vi.importActual('react-router-dom'), |
| 19 | + useNavigate: (): Mock => mockNavigate, |
| 20 | +})); |
| 21 | +vi.mock('../../../../helpers/requests/userPatientRestrictions/getUserInformation'); |
| 22 | +vi.mock('../../../../helpers/hooks/useBaseAPIUrl'); |
| 23 | +vi.mock('../../../../helpers/hooks/useBaseAPIHeaders'); |
| 24 | +vi.mock('../../../../helpers/hooks/useSmartcardNumber'); |
| 25 | + |
| 26 | +const mockNavigate = vi.fn(); |
| 27 | +const mockGetUserInformation = getUserInformation as Mock; |
| 28 | +const mockUseSmartcardNumber = useSmartcardNumber as Mock; |
| 29 | +const mockSetUserInformation = vi.fn(); |
| 30 | +const mockRestrictions = buildUserRestrictions(); |
| 31 | +const mockUserInformation = buildUserInformation(); |
| 32 | + |
| 33 | +describe('UserPatientRestrictionsSearchStaffStage', () => { |
| 34 | + beforeEach(() => { |
| 35 | + vi.resetAllMocks(); |
| 36 | + mockUseSmartcardNumber.mockReturnValue('777777777777'); |
| 37 | + mockGetUserInformation.mockResolvedValue(mockUserInformation); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should fetch user information and navigate on valid form submission', async () => { |
| 41 | + renderComponent(); |
| 42 | + |
| 43 | + const smartcardNumber = '111111111111'; |
| 44 | + const input = screen.getByTestId('smartcard-number-input'); |
| 45 | + await userEvent.type(input, smartcardNumber); |
| 46 | + const submitButton = screen.getByTestId('continue-button'); |
| 47 | + await userEvent.click(submitButton); |
| 48 | + |
| 49 | + expect(mockGetUserInformation).toHaveBeenCalledWith( |
| 50 | + expect.objectContaining({ |
| 51 | + smartcardId: smartcardNumber, |
| 52 | + }), |
| 53 | + ); |
| 54 | + expect(mockSetUserInformation).toHaveBeenCalledWith(mockUserInformation); |
| 55 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 56 | + routeChildren.USER_PATIENT_RESTRICTIONS_VERIFY_STAFF, |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should show validation error for empty input', async () => { |
| 61 | + renderComponent(); |
| 62 | + |
| 63 | + const submitButton = screen.getByTestId('continue-button'); |
| 64 | + await userEvent.click(submitButton); |
| 65 | + |
| 66 | + expect(screen.getByText(userRestrictionStaffSearchEmptyValueError)).toBeInTheDocument(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should show validation error for invalid input', async () => { |
| 70 | + renderComponent(); |
| 71 | + |
| 72 | + const input = screen.getByTestId('smartcard-number-input'); |
| 73 | + await userEvent.type(input, 'invalid'); |
| 74 | + const submitButton = screen.getByTestId('continue-button'); |
| 75 | + await userEvent.click(submitButton); |
| 76 | + |
| 77 | + expect(screen.getByText(userRestrictionStaffSearchInvalidFormatError)).toBeInTheDocument(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should navigate to error page when search for yourself', async () => { |
| 81 | + renderComponent(); |
| 82 | + |
| 83 | + const input = screen.getByTestId('smartcard-number-input'); |
| 84 | + await userEvent.type(input, '777777777777'); |
| 85 | + const submitButton = screen.getByTestId('continue-button'); |
| 86 | + await userEvent.click(submitButton); |
| 87 | + |
| 88 | + expect(mockNavigate).toHaveBeenCalledWith( |
| 89 | + routes.GENERIC_ERROR + `?errorCode=${UIErrorCode.USER_PATIENT_RESTRICTIONS_SELF_ADD}`, |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should show validation error when trying to add a restriction that already exists', async () => { |
| 94 | + renderComponent(); |
| 95 | + |
| 96 | + const existingSmartcardNumber = mockRestrictions[0].restrictedUser; |
| 97 | + const input = screen.getByTestId('smartcard-number-input'); |
| 98 | + await userEvent.type(input, existingSmartcardNumber); |
| 99 | + const submitButton = screen.getByTestId('continue-button'); |
| 100 | + await userEvent.click(submitButton); |
| 101 | + |
| 102 | + expect( |
| 103 | + screen.getByText(userRestrictionStaffSearchRestrictionExistsError), |
| 104 | + ).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should navigate to session expired page on 403 error', async () => { |
| 108 | + mockGetUserInformation.mockRejectedValue({ |
| 109 | + response: { status: 403 }, |
| 110 | + }); |
| 111 | + |
| 112 | + renderComponent(); |
| 113 | + |
| 114 | + const input = screen.getByTestId('smartcard-number-input'); |
| 115 | + await userEvent.type(input, '111111111111'); |
| 116 | + const submitButton = screen.getByTestId('continue-button'); |
| 117 | + await userEvent.click(submitButton); |
| 118 | + |
| 119 | + expect(mockNavigate).toHaveBeenCalledWith(routes.SESSION_EXPIRED); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should navigate to server error page on 500 error', async () => { |
| 123 | + mockGetUserInformation.mockRejectedValue({ |
| 124 | + response: { status: 500 }, |
| 125 | + }); |
| 126 | + |
| 127 | + renderComponent(); |
| 128 | + |
| 129 | + const input = screen.getByTestId('smartcard-number-input'); |
| 130 | + await userEvent.type(input, '111111111111'); |
| 131 | + const submitButton = screen.getByTestId('continue-button'); |
| 132 | + await userEvent.click(submitButton); |
| 133 | + |
| 134 | + expect(mockNavigate).toHaveBeenCalledWith(expect.stringContaining(routes.SERVER_ERROR)); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should show error message when staff member not found', async () => { |
| 138 | + mockGetUserInformation.mockRejectedValue({ |
| 139 | + response: { status: 404 }, |
| 140 | + }); |
| 141 | + |
| 142 | + renderComponent(); |
| 143 | + |
| 144 | + const input = screen.getByTestId('smartcard-number-input'); |
| 145 | + await userEvent.type(input, '111111111111'); |
| 146 | + const submitButton = screen.getByTestId('continue-button'); |
| 147 | + await userEvent.click(submitButton); |
| 148 | + |
| 149 | + await waitFor(() => { |
| 150 | + expect(screen.getByText(userRestrictionsStaffSearchNotFoundError)).toBeInTheDocument(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
| 155 | +const renderComponent = (): void => { |
| 156 | + render( |
| 157 | + <UserPatientRestrictionsSearchStaffStage |
| 158 | + existingRestrictions={mockRestrictions} |
| 159 | + setUserInformation={mockSetUserInformation} |
| 160 | + />, |
| 161 | + ); |
| 162 | +}; |
0 commit comments