test(i18n): a language test unmounts its reader before switching the language back
useTags reset the language in a finally while its hook was still mounted;
the switch scheduled a render, and in a loaded gate run it landed after
the jsdom environment was torn down ('window is not defined', two
unhandled errors). auditColumns and comfortPin had the same shape.
This commit is contained in:
@@ -3,14 +3,21 @@
|
||||
// E30 (D1 A): the tag catalog as every reader sees it. A system (auto) tag is stored in English and
|
||||
// named in the reader's language; a user's own tag stays as they typed it, even when it happens to be
|
||||
// a vocabulary word.
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ReactNode } from "react";
|
||||
import { renderHook, waitFor } from "@testing-library/react";
|
||||
import { cleanup, renderHook, waitFor } from "@testing-library/react";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { setLanguage } from "../i18n";
|
||||
import { api, type Tag } from "./api";
|
||||
import { useTags } from "./useTags";
|
||||
|
||||
// Unmount BEFORE the language goes back: a mounted reader re-renders on the switch, and under a loaded
|
||||
// run that render landed after the test environment was gone ("window is not defined", E31 S5).
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
setLanguage("en");
|
||||
});
|
||||
|
||||
const tag = (id: number, name: string, system: boolean): Tag => ({
|
||||
id,
|
||||
name,
|
||||
@@ -36,19 +43,15 @@ describe("the tag catalog", () => {
|
||||
{ ...tag(5, "Hungarian", true), category: "language" },
|
||||
]);
|
||||
setLanguage("hu");
|
||||
try {
|
||||
const { result } = renderHook(() => useTags(), { wrapper });
|
||||
await waitFor(() => expect(result.current.data).toBeDefined());
|
||||
expect(result.current.data!.map((tg) => tg.name)).toEqual([
|
||||
"Oktatás",
|
||||
"Utazás",
|
||||
"Zene",
|
||||
"Angol",
|
||||
"Magyar",
|
||||
]);
|
||||
} finally {
|
||||
setLanguage("en");
|
||||
}
|
||||
const { result } = renderHook(() => useTags(), { wrapper });
|
||||
await waitFor(() => expect(result.current.data).toBeDefined());
|
||||
expect(result.current.data!.map((tg) => tg.name)).toEqual([
|
||||
"Oktatás",
|
||||
"Utazás",
|
||||
"Zene",
|
||||
"Angol",
|
||||
"Magyar",
|
||||
]);
|
||||
});
|
||||
|
||||
it("names an auto-tag in the reader's language, a user's tag as typed", async () => {
|
||||
@@ -59,17 +62,13 @@ describe("the tag catalog", () => {
|
||||
tag(4, "Somethingnew", true), // an auto-tag the catalog does not name yet
|
||||
]);
|
||||
setLanguage("hu");
|
||||
try {
|
||||
const { result } = renderHook(() => useTags(), { wrapper });
|
||||
await waitFor(() => expect(result.current.data).toBeDefined());
|
||||
expect(result.current.data!.map((tg) => tg.name)).toEqual([
|
||||
"Egészség és fitnesz",
|
||||
"Gaming",
|
||||
"Magyar",
|
||||
"Somethingnew",
|
||||
]);
|
||||
} finally {
|
||||
setLanguage("en");
|
||||
}
|
||||
const { result } = renderHook(() => useTags(), { wrapper });
|
||||
await waitFor(() => expect(result.current.data).toBeDefined());
|
||||
expect(result.current.data!.map((tg) => tg.name)).toEqual([
|
||||
"Egészség és fitnesz",
|
||||
"Gaming",
|
||||
"Magyar",
|
||||
"Somethingnew",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user