chore: finish frontend ESLint migration #18
Enable every plugin recommended preset and replace Biome with ESLint formatting. Preserve API contracts and JSX whitespace; verify source, tests, builds and tooling.
This commit is contained in:
@@ -3,11 +3,14 @@ import { fireEvent, render, screen } from "@testing-library/react";
|
||||
import { type FC, lazy, Suspense } from "react";
|
||||
import { MemoryRouter, useLocation, useNavigate } from "react-router";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { RouteErrorBoundary } from "./RouteErrorBoundary";
|
||||
|
||||
let authenticated = false;
|
||||
const session = { isAuthenticated: false };
|
||||
vi.mock("@/features/auth", () => ({
|
||||
useAuth: () => ({ user: authenticated ? {} : null }),
|
||||
useAuth: () => ((/* eslint-disable unicorn/no-null -- Fixture preserves the production contract’s explicit null values. */
|
||||
{ user: session.isAuthenticated ? {} : null }
|
||||
/* eslint-enable unicorn/no-null */)),
|
||||
}));
|
||||
|
||||
const ThrowingRoute: FC = () => {
|
||||
@@ -17,11 +20,13 @@ const ThrowingRoute: FC = () => {
|
||||
const HealthyRoute: FC = () => {
|
||||
return (
|
||||
<div>
|
||||
{useLocation().pathname === "/healthy" ? (
|
||||
"healthy route"
|
||||
) : (
|
||||
<ThrowingRoute />
|
||||
)}
|
||||
{useLocation().pathname === "/healthy"
|
||||
? (
|
||||
"healthy route"
|
||||
)
|
||||
: (
|
||||
<ThrowingRoute />
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
@@ -31,7 +36,7 @@ const RouteHarness: FC = () => {
|
||||
|
||||
return (
|
||||
<>
|
||||
<button type="button" onClick={() => navigate("/healthy")}>
|
||||
<button onClick={() => navigate("/healthy")} type="button">
|
||||
go healthy
|
||||
</button>
|
||||
<RouteErrorBoundary>
|
||||
@@ -55,10 +60,10 @@ describe("RouteErrorBoundary", () => {
|
||||
it.each(["/accounts", "/login"])(
|
||||
"recovers a transient failure through the fallback on %s itself",
|
||||
async (path) => {
|
||||
authenticated = path === "/accounts";
|
||||
let failing = true;
|
||||
session.isAuthenticated = path === "/accounts";
|
||||
let isFailing = true;
|
||||
const RecoverableRoute: FC = () => {
|
||||
if (failing) throw new Error("temporary failure");
|
||||
if (isFailing) throw new Error("temporary failure");
|
||||
return <div>recovered route</div>;
|
||||
};
|
||||
render(
|
||||
@@ -71,14 +76,14 @@ describe("RouteErrorBoundary", () => {
|
||||
</MantineProvider>,
|
||||
);
|
||||
const action = screen.getByRole("link", { name: "Продолжить" });
|
||||
failing = false;
|
||||
isFailing = false;
|
||||
fireEvent.click(action);
|
||||
expect(await screen.findByText("recovered route")).toBeInTheDocument();
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps a persistent failure recoverable without an automatic reset loop", () => {
|
||||
authenticated = true;
|
||||
session.isAuthenticated = true;
|
||||
renderBoundary("/accounts");
|
||||
fireEvent.click(screen.getByRole("link", { name: "Продолжить" }));
|
||||
expect(
|
||||
@@ -91,7 +96,7 @@ describe("RouteErrorBoundary", () => {
|
||||
});
|
||||
|
||||
it("offers document navigation when React.lazy caches a rejected chunk", async () => {
|
||||
authenticated = false;
|
||||
session.isAuthenticated = false;
|
||||
const load = vi.fn(() => Promise.reject(new Error("chunk failed")));
|
||||
const Chunk = lazy(load);
|
||||
render(
|
||||
@@ -110,18 +115,18 @@ describe("RouteErrorBoundary", () => {
|
||||
const reload = screen.getByRole("link", { name: "Перезагрузить страницу" });
|
||||
expect(reload).toHaveAttribute("href", "/login");
|
||||
// Cancel browser navigation in JSDOM only after proving React Router did not intercept it.
|
||||
let intercepted = true;
|
||||
let isIntercepted = true;
|
||||
const preventNavigation = (event: MouseEvent) => {
|
||||
intercepted = event.defaultPrevented;
|
||||
isIntercepted = event.defaultPrevented;
|
||||
event.preventDefault();
|
||||
};
|
||||
document.addEventListener("click", preventNavigation, { once: true });
|
||||
fireEvent.click(reload);
|
||||
expect(intercepted).toBe(false);
|
||||
expect(isIntercepted).toBe(false);
|
||||
});
|
||||
|
||||
it("offers login after a public route error without error details", () => {
|
||||
authenticated = false;
|
||||
session.isAuthenticated = false;
|
||||
renderBoundary("/login");
|
||||
expect(document.title).toBe("Не удалось открыть страницу — WealthLedger");
|
||||
expect(screen.getByRole("link", { name: "Продолжить" })).toHaveAttribute(
|
||||
@@ -132,9 +137,9 @@ describe("RouteErrorBoundary", () => {
|
||||
});
|
||||
|
||||
it("offers accounts after a protected route error and resets on navigation", async () => {
|
||||
authenticated = true;
|
||||
session.isAuthenticated = true;
|
||||
renderBoundary("/accounts");
|
||||
screen.getByRole("button", { name: "go healthy" }).click();
|
||||
fireEvent.click(screen.getByRole("button", { name: "go healthy" }));
|
||||
expect(await screen.findByText("healthy route")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import type { FC, PropsWithChildren } from "react";
|
||||
|
||||
import { Button, Stack, Text, Title } from "@mantine/core";
|
||||
import { useDocumentTitle } from "@mantine/hooks";
|
||||
import type { FC, PropsWithChildren } from "react";
|
||||
import { Component } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { Link, useLocation } from "react-router";
|
||||
|
||||
import { useAuth } from "@/features/auth";
|
||||
|
||||
type BoundaryProps = PropsWithChildren<{
|
||||
routeKey: string;
|
||||
authenticated: boolean;
|
||||
routeKey: string;
|
||||
}>;
|
||||
|
||||
type RouteErrorFallbackProps = {
|
||||
@@ -18,12 +20,12 @@ type RouteErrorFallbackProps = {
|
||||
type State = { hasError: boolean };
|
||||
|
||||
class Boundary extends Component<BoundaryProps, State> {
|
||||
state: State = { hasError: false };
|
||||
|
||||
static getDerivedStateFromError(): State {
|
||||
return { hasError: true };
|
||||
}
|
||||
|
||||
state: State = { hasError: false };
|
||||
|
||||
componentDidUpdate(previousProps: BoundaryProps) {
|
||||
if (previousProps.routeKey !== this.props.routeKey && this.state.hasError) {
|
||||
this.setState({ hasError: false });
|
||||
@@ -31,10 +33,7 @@ class Boundary extends Component<BoundaryProps, State> {
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return <RouteErrorFallback authenticated={this.props.authenticated} />;
|
||||
}
|
||||
return this.props.children;
|
||||
return this.state.hasError ? <RouteErrorFallback authenticated={this.props.authenticated} /> : this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +59,7 @@ const RouteErrorFallback: FC<RouteErrorFallbackProps> = (props) => {
|
||||
|
||||
const RouteErrorBoundary: FC<PropsWithChildren> = (props) => {
|
||||
const { children } = props;
|
||||
const { pathname, search, key } = useLocation();
|
||||
const { key, pathname, search } = useLocation();
|
||||
const { user } = useAuth();
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user