From c88a27ff8504f52125603e740f18020f93340f5d Mon Sep 17 00:00:00 2001 From: Bernt Christian Egeland Date: Fri, 9 Aug 2024 20:40:30 +0200 Subject: [PATCH] tests --- src/__tests__/pages/auth/signin.test.tsx | 29 ++++++++---------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/__tests__/pages/auth/signin.test.tsx b/src/__tests__/pages/auth/signin.test.tsx index efb1eb1b..3088b568 100644 --- a/src/__tests__/pages/auth/signin.test.tsx +++ b/src/__tests__/pages/auth/signin.test.tsx @@ -1,4 +1,4 @@ -import { render, screen, waitFor } from "@testing-library/react"; +import { act, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { NextIntlClientProvider } from "next-intl"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; @@ -80,24 +80,27 @@ describe("LoginPage", () => { render( - + , ); }; - it("renders login form correctly", () => { + it("renders login form correctly", async () => { const useQueryMock = jest.fn().mockReturnValue({ data: null, isLoading: true, refetch: jest.fn(), }); api.public.getWelcomeMessage.useQuery = useQueryMock; - renderLoginPage(); + + await act(async () => { + renderLoginPage(); + }); expect(screen.getByLabelText(/Email/i)).toBeInTheDocument(); expect(screen.getByLabelText(/Password/i)).toBeInTheDocument(); - expect(screen.getByRole("button", { name: /Sign in/i })).toBeInTheDocument(); + expect(screen.getByRole("button", { name: /^Sign in$/i })).toBeInTheDocument(); }); it("handles form submission with valid credentials", async () => { @@ -144,7 +147,7 @@ describe("LoginPage", () => { const emailInput = screen.getByLabelText(/Email/i); const passwordInput = screen.getByLabelText(/Password/i); - const submitButton = screen.getByRole("button", { name: /Sign in/i }); + const submitButton = screen.getByRole("button", { name: /^Sign in$/i }); await userEvent.type(emailInput, "invalid@example.com"); await userEvent.type(passwordInput, "wrongpassword"); @@ -160,20 +163,6 @@ describe("LoginPage", () => { }); }); - // it("validates email format", async () => { - // renderLoginPage(); - - // const emailInput = screen.getByLabelText(/Email/i); - // const submitButton = screen.getByRole("button", { name: /Sign in/i }); - - // await userEvent.type(emailInput, "invalidemail"); - // await userEvent.click(submitButton); - - // await waitFor(() => { - // expect(screen.getByText(/Please enter a valid email address/i)).toBeInTheDocument(); - // }); - // }); - it("requires password input", async () => { (signIn as jest.Mock).mockResolvedValue({ error: "email or password is wrong!",