|
| 1 | +// silence chatty console |
| 2 | +import "src/_test_utilities/consoleMock"; |
| 3 | + |
| 4 | +import React from "react"; |
| 5 | +import { render, screen, fireEvent, act } from "src/_test_utilities/test-utils"; |
| 6 | +import userEvent from "@testing-library/user-event"; |
| 7 | +import RegistrationCodeFormModal, { |
| 8 | + DATA_TEST_ID, |
| 9 | + RegistrationCodeFormModalState, |
| 10 | +} from "src/auth/components/registrationCodeFormModal/RegistrationCodeFormModal"; |
| 11 | +import RequestInvitationCodeFormModal from "src/auth/components/requestInvitationCode/requestInvitationCodeFormModal/RequestInvitationCodeFormModal"; |
| 12 | +import * as Sentry from "@sentry/react"; |
| 13 | + |
| 14 | +// Mock InvitationsService |
| 15 | +jest.mock("src/auth/services/invitationsService/invitations.service", () => ({ |
| 16 | + getInstance: jest.fn().mockReturnValue({ |
| 17 | + checkInvitationCodeStatus: jest.fn(), |
| 18 | + }), |
| 19 | +})); |
| 20 | + |
| 21 | +// mock the snack bar provider |
| 22 | +jest.mock("src/theme/SnackbarProvider/SnackbarProvider", () => { |
| 23 | + const actual = jest.requireActual("src/theme/SnackbarProvider/SnackbarProvider"); |
| 24 | + return { |
| 25 | + ...actual, |
| 26 | + __esModule: true, |
| 27 | + useSnackbar: jest.fn().mockReturnValue({ |
| 28 | + enqueueSnackbar: jest.fn(), |
| 29 | + closeSnackbar: jest.fn(), |
| 30 | + }), |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +// mock the RequestInvitationFormModal component |
| 35 | +jest.mock("src/auth/components/requestInvitationCode/requestInvitationCodeFormModal/RequestInvitationCodeFormModal", () => { |
| 36 | + const actual = jest.requireActual( |
| 37 | + "src/auth/components/requestInvitationCode/requestInvitationCodeFormModal/RequestInvitationCodeFormModal" |
| 38 | + ); |
| 39 | + return { |
| 40 | + ...actual, |
| 41 | + __esModule: true, |
| 42 | + default: jest.fn().mockImplementation(() => { |
| 43 | + return <span data-testid={actual.DATA_TEST_ID.CONTAINER}></span>; |
| 44 | + }), |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +// Mock Sentry |
| 49 | +jest.mock("@sentry/react"); |
| 50 | + |
| 51 | +describe("RegistrationCodeFormModal", () => { |
| 52 | + test("renders correctly when modal is shown and call onSuccess with the provided code", () => { |
| 53 | + // GIVEN the component is shown |
| 54 | + const givenShown = RegistrationCodeFormModalState.SHOW; |
| 55 | + // AND GIVEN Sentry is initialized |
| 56 | + jest.spyOn(Sentry, "isInitialized").mockReturnValue(true); |
| 57 | + |
| 58 | + // AND the onClose function is a jest function |
| 59 | + const givenOnSuccess = jest.fn(); |
| 60 | + |
| 61 | + // WHEN the component is rendered |
| 62 | + render(<RegistrationCodeFormModal onClose={jest.fn} modalState={givenShown} onSuccess={givenOnSuccess} />); |
| 63 | + |
| 64 | + // THEN the component should render correctly |
| 65 | + const container = screen.getByTestId(DATA_TEST_ID.CONTAINER); |
| 66 | + expect(container).toBeInTheDocument(); |
| 67 | + |
| 68 | + // AND the component should contain the necessary elements |
| 69 | + expect(screen.getByTestId(DATA_TEST_ID.MODAL_TITLE)).toBeInTheDocument(); |
| 70 | + expect(screen.getByTestId(DATA_TEST_ID.MODAL_SUBTITLE)).toBeInTheDocument(); |
| 71 | + expect(screen.getByTestId(DATA_TEST_ID.INVITATION_CODE_INPUT)).toBeInTheDocument(); |
| 72 | + expect(screen.getByTestId(DATA_TEST_ID.SUBMIT_BUTTON)).toBeInTheDocument(); |
| 73 | + expect(screen.getByTestId(DATA_TEST_ID.CLOSE_ICON)).toBeInTheDocument(); |
| 74 | + expect(screen.getByTestId(DATA_TEST_ID.REQUEST_REGISTRATION_CODE_LINK)).toBeInTheDocument(); |
| 75 | + |
| 76 | + // AND the component should match the snapshot |
| 77 | + expect(container).toMatchSnapshot(); |
| 78 | + |
| 79 | + // WHEN the registration code is entered |
| 80 | + const registrationCode = "foo"; |
| 81 | + fireEvent.change(screen.getByTestId(DATA_TEST_ID.INVITATION_CODE_INPUT), { target: { value: registrationCode } }); |
| 82 | + |
| 83 | + // AND the registration code is submitted |
| 84 | + fireEvent.click(screen.getByTestId(DATA_TEST_ID.SUBMIT_BUTTON)); |
| 85 | + |
| 86 | + // THEN the onSuccess function should be called with the registration code |
| 87 | + expect(givenOnSuccess).toHaveBeenCalledWith(registrationCode); |
| 88 | + }); |
| 89 | + |
| 90 | + test("should show request registration code link when Sentry is initialized", () => { |
| 91 | + // GIVEN Sentry is initialized |
| 92 | + jest.spyOn(Sentry, "isInitialized").mockReturnValue(true); |
| 93 | + // AND the component is shown |
| 94 | + const givenShown = RegistrationCodeFormModalState.SHOW; |
| 95 | + |
| 96 | + // WHEN the component is rendered |
| 97 | + render(<RegistrationCodeFormModal onClose={jest.fn()} modalState={givenShown} onSuccess={jest.fn()} />); |
| 98 | + |
| 99 | + // THEN the request registration code link should be visible |
| 100 | + expect(screen.getByTestId(DATA_TEST_ID.REQUEST_REGISTRATION_CODE_LINK)).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + test("should not show request registration code link when Sentry is not initialized", () => { |
| 104 | + // GIVEN Sentry is not initialized |
| 105 | + jest.spyOn(Sentry, "isInitialized").mockReturnValue(false); |
| 106 | + // AND the component is shown |
| 107 | + const givenShown = RegistrationCodeFormModalState.SHOW; |
| 108 | + |
| 109 | + // WHEN the component is rendered |
| 110 | + render(<RegistrationCodeFormModal onClose={jest.fn()} modalState={givenShown} onSuccess={jest.fn()} />); |
| 111 | + |
| 112 | + // THEN the request registration code link should not be visible |
| 113 | + expect(screen.queryByTestId(DATA_TEST_ID.REQUEST_REGISTRATION_CODE_LINK)).not.toBeInTheDocument(); |
| 114 | + }); |
| 115 | + |
| 116 | + test("should render CircularProgress when modal state is loading", () => { |
| 117 | + // GIVEN the component is in loading state |
| 118 | + const givenShown = RegistrationCodeFormModalState.LOADING; |
| 119 | + |
| 120 | + // AND the onClose function is a jest function |
| 121 | + const givenOnClose = jest.fn(); |
| 122 | + |
| 123 | + // WHEN the component is rendered |
| 124 | + render(<RegistrationCodeFormModal onClose={givenOnClose} modalState={givenShown} onSuccess={jest.fn()} />); |
| 125 | + |
| 126 | + // THEN the circular progress should be in the document |
| 127 | + const circularProgress = screen.getByTestId(DATA_TEST_ID.PROGRESS_ELEMENT); |
| 128 | + expect(circularProgress).toBeInTheDocument(); |
| 129 | + }); |
| 130 | + |
| 131 | + test("closes the modal when the close icon is clicked", () => { |
| 132 | + // GIVEN the component is shown |
| 133 | + const givenShown = RegistrationCodeFormModalState.SHOW; |
| 134 | + |
| 135 | + // AND the onClose function is a jest function |
| 136 | + const givenOnClose = jest.fn(); |
| 137 | + |
| 138 | + // WHEN the component is rendered |
| 139 | + render(<RegistrationCodeFormModal onClose={givenOnClose} modalState={givenShown} onSuccess={jest.fn()} />); |
| 140 | + |
| 141 | + // THEN the component should render correctly |
| 142 | + const container = screen.getByTestId(DATA_TEST_ID.CONTAINER); |
| 143 | + expect(container).toBeInTheDocument(); |
| 144 | + |
| 145 | + // AND the component should contain the necessary elements |
| 146 | + expect(screen.getByTestId(DATA_TEST_ID.MODAL_TITLE)).toBeInTheDocument(); |
| 147 | + expect(screen.getByTestId(DATA_TEST_ID.MODAL_SUBTITLE)).toBeInTheDocument(); |
| 148 | + expect(screen.getByTestId(DATA_TEST_ID.INVITATION_CODE_INPUT)).toBeInTheDocument(); |
| 149 | + expect(screen.getByTestId(DATA_TEST_ID.SUBMIT_BUTTON)).toBeInTheDocument(); |
| 150 | + expect(screen.getByTestId(DATA_TEST_ID.CLOSE_ICON)).toBeInTheDocument(); |
| 151 | + |
| 152 | + // WHEN the close icon is clicked |
| 153 | + fireEvent.click(screen.getByTestId(DATA_TEST_ID.CLOSE_ICON)); |
| 154 | + |
| 155 | + // THEN the onClose function should be called |
| 156 | + expect(givenOnClose).toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + |
| 159 | + test("should show request invitation code modal when link is clicked and close it when onClose is called", async () => { |
| 160 | + // GIVEN the component is shown |
| 161 | + const givenShown = RegistrationCodeFormModalState.SHOW; |
| 162 | + const givenOnClose = jest.fn(); |
| 163 | + // AND GIVEN Sentry is initialized |
| 164 | + jest.spyOn(Sentry, "isInitialized").mockReturnValue(true); |
| 165 | + // AND the component is rendered |
| 166 | + render(<RegistrationCodeFormModal modalState={givenShown} onSuccess={jest.fn()} onClose={givenOnClose} />); |
| 167 | + // AND onClose mock |
| 168 | + const onCloseMock = (RequestInvitationCodeFormModal as jest.Mock).mock.calls[0][0].onClose; |
| 169 | + |
| 170 | + // WHEN the request invitation code link is clicked |
| 171 | + const requestRegistrationCodeLink = screen.getByTestId(DATA_TEST_ID.REQUEST_REGISTRATION_CODE_LINK); |
| 172 | + await userEvent.click(requestRegistrationCodeLink); |
| 173 | + |
| 174 | + // THEN expect the invitation code form modal to be closed |
| 175 | + expect(givenOnClose).toHaveBeenCalled(); |
| 176 | + // AND the request registration form modal should be displayed |
| 177 | + expect(RequestInvitationCodeFormModal).toHaveBeenCalledWith( |
| 178 | + expect.objectContaining({ open: true }), |
| 179 | + expect.anything() |
| 180 | + ); |
| 181 | + |
| 182 | + // WHEN the onClose function is called |
| 183 | + act(() => { |
| 184 | + onCloseMock(); |
| 185 | + }); |
| 186 | + |
| 187 | + // THEN the request invitation code form modal should be closed |
| 188 | + expect(RequestInvitationCodeFormModal).toHaveBeenCalledWith( |
| 189 | + expect.objectContaining({ open: false }), |
| 190 | + expect.anything() |
| 191 | + ); |
| 192 | + }); |
| 193 | +}); |
0 commit comments