Skip to content

Commit

Permalink
feat: update form submission tests
Browse files Browse the repository at this point in the history
  • Loading branch information
typeWolffo committed Aug 6, 2024
1 parent f18419f commit fb064f2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function useLoginUser() {
return useMutation({
mutationFn: async (options: LoginUserOptions) => {
const response = await ApiClient.auth.authControllerLogin(options.data);
console.log();
return response.data;
},
onSuccess: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import { createRemixStub } from "@remix-run/testing";
import { screen, waitFor } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { PropsWithChildren } from "react";
import { describe, expect, it, vi } from "vitest";
import {
mockRemixReact,
mockedUseNavigate,
} from "~/utils/mocks/remix-run-mock";
import { renderWith } from "~/utils/testUtils";
import LoginPage from "./Login.page";
import { server } from "../../utils/mocks/node";
import { http, HttpResponse } from "msw";

const mockedUseNavigate = vi.fn();

vi.mock("../../../api/api-client");
vi.mock("@remix-run/react", async () => {
const module =
await vi.importActual<typeof import("@remix-run/react")>(
"@remix-run/react"
);
return {
...module,
useNavigate: () => mockedUseNavigate,
Link: ({ to, children }: PropsWithChildren<{ to: string }>) => (
<a href={to}>{children}</a>
),
};
});

mockRemixReact();

describe("Login page", () => {
beforeEach(() => {
Expand All @@ -43,29 +31,16 @@ describe("Login page", () => {
expect(screen.getByRole("heading", { name: "Login" })).toBeInTheDocument();
});

it.skip("submits the form with valid data", async () => {
it("submits the form with valid data", async () => {
renderWith({ withQuery: true }).render(<RemixStub />);

const user = userEvent.setup();
await user.type(screen.getByLabelText("Email"), "test@example.com");
await user.type(screen.getByLabelText("Password"), "password123");
await user.click(screen.getByRole("button", { name: "Login" }));

await waitFor(() => {
expect(server.use).toHaveBeenCalledWith(
http.post("/api/auth/register", async ({ request }) => {
const body = await request.json();
expect(body).toEqual({
email: "test@example.com",
password: "password123",
});
return HttpResponse.json({ data: "" }, { status: 200 });
})
);
expect(mockedUseNavigate).toHaveBeenCalledWith("/dashboard");
});
// expect(ApiClient.auth.authControllerLogin).toHaveBeenCalledWith({
// email: "test@example.com",
// password: "password123",
// });
// expect(mockedUseNavigate).toHaveBeenCalledWith("/dashboard");
});
});
5 changes: 0 additions & 5 deletions examples/common_nestjs_remix/apps/web/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,5 @@ export default defineConfig({
name: "firefox",
use: { ...devices["Desktop Firefox"] },
},

{
name: "webkit",
use: { ...devices["Desktop Safari"] },
},
],
});

0 comments on commit fb064f2

Please sign in to comment.