|
| 1 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import "@testing-library/jest-dom"; |
| 3 | +import * as hooks from "../../contexts/AuthContext"; |
| 4 | +import LogIn from "."; |
| 5 | +import { userClient } from "../../utils/api"; |
| 6 | + |
| 7 | +/* Mock useNavigate */ |
| 8 | +const mockUseNavigate = jest.fn(); |
| 9 | +jest.mock("react-router-dom", () => ({ |
| 10 | + ...jest.requireActual("react-router-dom"), |
| 11 | + useNavigate: () => mockUseNavigate, |
| 12 | +})); |
| 13 | + |
| 14 | +/* Mock userClient APIs */ |
| 15 | +jest.mock("../../utils/api", () => ({ |
| 16 | + userClient: { |
| 17 | + post: jest.fn(), |
| 18 | + }, |
| 19 | +})); |
| 20 | +const mockedPost = userClient.post as jest.MockedFunction<typeof userClient.post>; |
| 21 | + |
| 22 | +describe("Log In Components", () => { |
| 23 | + beforeEach(() => { |
| 24 | + jest.spyOn(hooks, "useAuth").mockImplementation(() => ({ |
| 25 | + signup: jest.fn(), |
| 26 | + login: jest.fn(), |
| 27 | + logout: jest.fn(), |
| 28 | + user: null, |
| 29 | + })); |
| 30 | + }); |
| 31 | + |
| 32 | + it("App name is rendered", () => { |
| 33 | + render(<LogIn />); |
| 34 | + expect(screen.getByText("PeerPrep")).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("Email field is rendered", () => { |
| 38 | + render(<LogIn />); |
| 39 | + expect(screen.getByTestId("Email")).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("Password field is rendered", () => { |
| 43 | + render(<LogIn />); |
| 44 | + expect(screen.getByTestId("Password")).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("Log in button is rendered", () => { |
| 48 | + render(<LogIn />); |
| 49 | + expect(screen.getByRole("button", { name: "Log in" })).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it("Prompt to sign up is rendered", () => { |
| 53 | + render(<LogIn />); |
| 54 | + const signUpButton = screen.getByRole("button", { name: "Sign up" }); |
| 55 | + expect(signUpButton).toBeInTheDocument(); |
| 56 | + |
| 57 | + fireEvent.click(signUpButton); |
| 58 | + expect(mockUseNavigate).toHaveBeenCalledWith("/signup"); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("Log In Events", () => { |
| 63 | + // valid inputs |
| 64 | + const email = "[email protected]"; |
| 65 | + const password = "Password@123"; |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + jest.spyOn(hooks, "useAuth").mockImplementation(() => ({ |
| 69 | + signup: jest.fn(), |
| 70 | + login: (email, password) => { |
| 71 | + return mockedPost("/auth/login", { |
| 72 | + email, |
| 73 | + password, |
| 74 | + }); |
| 75 | + }, |
| 76 | + logout: jest.fn(), |
| 77 | + user: null, |
| 78 | + })); |
| 79 | + }); |
| 80 | + |
| 81 | + it("Successful log in with valid inputs", async () => { |
| 82 | + mockedPost.mockResolvedValue({}); |
| 83 | + |
| 84 | + render(<LogIn />); |
| 85 | + |
| 86 | + fireEvent.change(screen.getByTestId("Email"), { target: { value: email } }); |
| 87 | + fireEvent.change(screen.getByTestId("Password"), { target: { value: password } }); |
| 88 | + fireEvent.click(screen.getByRole("button", { name: "Log in" })); |
| 89 | + |
| 90 | + await waitFor(() => { |
| 91 | + expect(mockedPost).toHaveBeenCalledWith("/auth/login", { |
| 92 | + email: email, |
| 93 | + password: password, |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("Unsuccessful log in with invalid email", async () => { |
| 99 | + const invalidEmail = "invalidEmail"; |
| 100 | + |
| 101 | + render(<LogIn />); |
| 102 | + |
| 103 | + fireEvent.change(screen.getByTestId("Email"), { target: { value: invalidEmail } }); |
| 104 | + fireEvent.change(screen.getByTestId("Password"), { target: { value: password } }); |
| 105 | + fireEvent.click(screen.getByRole("button", { name: "Log in" })); |
| 106 | + |
| 107 | + await waitFor(() => { |
| 108 | + expect(mockedPost).not.toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments