refactor: simplify auth and instrument pages
This commit is contained in:
@@ -3,7 +3,7 @@ import { QueryClientProvider } from "@tanstack/react-query";
|
||||
import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { cssVariablesResolver, theme } from "@wealthledger/ui/theme";
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { MemoryRouter } from "react-router";
|
||||
import { type InitialEntry, MemoryRouter } from "react-router";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { AuthProvider } from "@/features/auth";
|
||||
@@ -12,7 +12,7 @@ import { server } from "@/test/server";
|
||||
|
||||
import { AppRoutes } from "./AppRoutes";
|
||||
|
||||
function renderRoutes(path: string) {
|
||||
function renderRoutes(path: InitialEntry) {
|
||||
return render(
|
||||
<QueryClientProvider client={createTestQueryClient()}>
|
||||
<MemoryRouter initialEntries={[path]}>
|
||||
@@ -57,10 +57,18 @@ describe("AppRoutes", () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("logs in through the public form and reaches the protected landing", async () => {
|
||||
it.each([
|
||||
{ endpoint: "login", path: "/accounts", title: "Вход" },
|
||||
{ endpoint: "register", path: "/register", title: "Регистрация" },
|
||||
{
|
||||
endpoint: "login",
|
||||
path: { pathname: "/login", state: { returnTo: "//example.com" } },
|
||||
title: "Вход",
|
||||
},
|
||||
])("authenticates via $endpoint and reaches the protected landing from $path", async ({ endpoint, path, title }) => {
|
||||
let credentials: unknown;
|
||||
server.use(
|
||||
http.post("*/api/v1/auth/login", async ({ request }) => {
|
||||
http.post(`*/api/v1/auth/${endpoint}`, async ({ request }) => {
|
||||
credentials = await request.json();
|
||||
|
||||
return HttpResponse.json({
|
||||
@@ -69,16 +77,23 @@ describe("AppRoutes", () => {
|
||||
});
|
||||
}),
|
||||
);
|
||||
renderRoutes("/accounts");
|
||||
renderRoutes(path);
|
||||
|
||||
await screen.findByRole("heading", { level: 1, name: "Вход" });
|
||||
await screen.findByRole("heading", { level: 1, name: title });
|
||||
fireEvent.change(screen.getByLabelText("Email"), {
|
||||
target: { value: " User@Example.com " },
|
||||
});
|
||||
fireEvent.change(screen.getByLabelText("Пароль"), {
|
||||
target: { value: "a secure password" },
|
||||
});
|
||||
fireEvent.click(screen.getByRole("button", { name: "Вход" }));
|
||||
|
||||
if (endpoint === "register") {
|
||||
fireEvent.change(screen.getByLabelText("Подтвердите пароль"), {
|
||||
target: { value: "a secure password" },
|
||||
});
|
||||
}
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: title }));
|
||||
|
||||
expect(
|
||||
await screen.findByRole("heading", {
|
||||
|
||||
Reference in New Issue
Block a user