|
| 1 | +import "@testing-library/jest-dom" |
| 2 | +import EmailForm from "@/components/popups/profile-settings/EmailForm"; |
| 3 | +import { act, fireEvent, screen } from "@testing-library/react"; |
| 4 | +import { renderWithRedux } from "@__mocks__/renderWithRedux"; |
| 5 | + |
| 6 | +jest.mock("next/router", () => ({ |
| 7 | + useRouter: () => ({ |
| 8 | + push: jest.fn(), |
| 9 | + events: { |
| 10 | + on: jest.fn(), |
| 11 | + off: jest.fn(), |
| 12 | + emit: jest.fn(), |
| 13 | + }, |
| 14 | + isFallback: false, |
| 15 | + pathname: "mocked-pathname", |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +const handleClose = jest.fn() |
| 20 | + |
| 21 | +const onSubmit = jest.fn() |
| 22 | + |
| 23 | +const renderEmailEditForm = () => { |
| 24 | + renderWithRedux(<EmailForm show onClose={handleClose} />) |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + renderEmailEditForm() |
| 29 | +}) |
| 30 | + |
| 31 | +afterEach(() => { |
| 32 | + onSubmit.mockReset() |
| 33 | +}) |
| 34 | + |
| 35 | +describe("EmailEditForm", () => { |
| 36 | + it('should render the email edit form modal.', () => { |
| 37 | + expect(screen.getByTestId('profileModal')).toBeInTheDocument() |
| 38 | + }) |
| 39 | + |
| 40 | + it('should render a form', () => { |
| 41 | + const form = screen.getByTestId('edit-email-form') |
| 42 | + expect(form).toBeInTheDocument() |
| 43 | + }) |
| 44 | + |
| 45 | + it('should contain 2 inputs, and a close button', () => { |
| 46 | + const emailInput = screen.getByPlaceholderText('login-page.email.placeholder'); |
| 47 | + const emailInputConfirm = screen.getByPlaceholderText('profile.settings.edit.email.confirm') |
| 48 | + const closeButton = screen.getByTestId('close-button') |
| 49 | + |
| 50 | + expect(emailInput).toBeInTheDocument() |
| 51 | + expect(emailInputConfirm).toBeInTheDocument() |
| 52 | + expect(closeButton).toBeInTheDocument() |
| 53 | + }) |
| 54 | + |
| 55 | + it("should not modify user's input values", async () => { |
| 56 | + const emailInput = screen.getByPlaceholderText('login-page.email.placeholder'); |
| 57 | + const emailInputConfirm = screen.getByPlaceholderText('profile.settings.edit.email.confirm') |
| 58 | + |
| 59 | + act(() => { |
| 60 | + fireEvent.change(emailInput, { target: { value: "[email protected]" } }); |
| 61 | + fireEvent.change(emailInputConfirm, { target: { value: "[email protected]" } }); |
| 62 | + fireEvent.submit(screen.getByTestId("edit-email-form")); |
| 63 | + }) |
| 64 | + }); |
| 65 | + |
| 66 | + it("should not submit the form when emails do not match", async () => { |
| 67 | + const emailInput = screen.getByPlaceholderText('login-page.email.placeholder'); |
| 68 | + const emailInputConfirm = screen.getByPlaceholderText('profile.settings.edit.email.confirm'); |
| 69 | + const form = screen.getByTestId('edit-email-form'); |
| 70 | + |
| 71 | + fireEvent.change(emailInput, { target: { value: "[email protected]" } }); |
| 72 | + fireEvent.change(emailInputConfirm, { target: { value: "[email protected]" } }); |
| 73 | + |
| 74 | + await act(async () => { |
| 75 | + fireEvent.submit(form); |
| 76 | + }); |
| 77 | + |
| 78 | + expect(onSubmit).not.toHaveBeenCalled(); |
| 79 | + expect(screen.queryByText("Emails should match.")).toBeInTheDocument(); |
| 80 | + |
| 81 | + }) |
| 82 | + |
| 83 | + it("it should close the modal when its open", () => { |
| 84 | + expect(screen.getByTestId('profileModal')).toBeInTheDocument() |
| 85 | + fireEvent.click(screen.getByTestId('close-button')) |
| 86 | + expect(handleClose).toHaveBeenCalled() |
| 87 | + }) |
| 88 | +}) |
0 commit comments