65 lines
3.2 KiB
TypeScript
65 lines
3.2 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
||
import userEvent from "@testing-library/user-event";
|
||
import { createMemoryRouter } from "react-router";
|
||
import { beforeEach, describe, expect, it } from "vitest";
|
||
import { App } from "./App.tsx";
|
||
import { clearToken, getAuth, setToken } from "./auth/token.ts";
|
||
import { routes } from "./routes.tsx";
|
||
import { PAGE_ROUTES } from "./shell/pages.tsx";
|
||
import { json, mockFetch } from "./test/fetchMock.ts";
|
||
|
||
const at = (path: string) => createMemoryRouter(routes, { initialEntries: [path], basename: "/app" });
|
||
|
||
describe("оболочка", () => {
|
||
beforeEach(() => { setToken("tok"); mockFetch(() => json(200, {})); });
|
||
|
||
it("/app ведёт на доску", async () => {
|
||
render(<App router={at("/app")} />);
|
||
expect(await screen.findByTestId("page-board")).toBeInTheDocument();
|
||
});
|
||
|
||
it("переход «Задачи» и обратно", async () => {
|
||
const router = at("/app/board");
|
||
render(<App router={router} />);
|
||
await userEvent.click(screen.getByRole("link", { name: "Задачи" }));
|
||
expect(router.state.location.pathname).toBe("/app/issues");
|
||
expect(await screen.findByTestId("page-issues")).toBeInTheDocument();
|
||
await userEvent.click(screen.getByRole("link", { name: "Доска" }));
|
||
expect(router.state.location.pathname).toBe("/app/board");
|
||
expect(await screen.findByTestId("page-board")).toBeInTheDocument();
|
||
});
|
||
|
||
it("неизвестный путь — «не найдено» со ссылками", async () => {
|
||
render(<App router={at("/app/nope")} />);
|
||
expect(await screen.findByRole("heading", { name: "Страница не найдена" })).toBeInTheDocument();
|
||
expect(screen.getByRole("link", { name: "доска" })).toHaveAttribute("href", "/app/board");
|
||
expect(screen.getByRole("link", { name: "задачи" })).toHaveAttribute("href", "/app/issues");
|
||
});
|
||
|
||
it("«Сменить токен» показывает экран входа", async () => {
|
||
render(<App router={at("/app/board")} />);
|
||
await userEvent.click(screen.getByRole("button", { name: "Сменить токен" }));
|
||
expect(getAuth().token).toBe("");
|
||
expect(screen.getByRole("heading", { name: "Вход в хаб" })).toBeInTheDocument();
|
||
});
|
||
|
||
it("исключение в странице — сообщение и «Перезагрузить», панель на месте", async () => {
|
||
const Boom = () => { throw new Error("сломалось"); };
|
||
const boom = { path: "/x", element: <Boom /> };
|
||
PAGE_ROUTES.unshift(boom);
|
||
try {
|
||
const router = createMemoryRouter(routes, { initialEntries: ["/app/x"], basename: "/app" });
|
||
render(<App router={router} />);
|
||
expect(await screen.findByText(/сломалось/)).toBeInTheDocument();
|
||
expect(screen.getByRole("button", { name: "Перезагрузить" })).toBeInTheDocument();
|
||
expect(screen.getByRole("link", { name: "Доска" })).toBeInTheDocument();
|
||
} finally { PAGE_ROUTES.splice(PAGE_ROUTES.indexOf(boom), 1); }
|
||
});
|
||
|
||
it("без токена — только экран входа", () => {
|
||
clearToken();
|
||
render(<App router={at("/app/board")} />);
|
||
expect(screen.queryByRole("link", { name: "Доска" })).not.toBeInTheDocument();
|
||
});
|
||
});
|