返回 CodeWhale
fallback-region.test.tsx
根目录 / web / components / fallback-region.test.tsx
1 import { createElement } from "react";
2 import { renderToStaticMarkup } from "react-dom/server";
3 import { describe, expect, it, vi } from "vitest";
4 import DocsLayout from "@/app/[locale]/docs/layout";
5 import { EnglishText, FallbackRegion } from "./fallback-region";
6
7 const pathname = vi.hoisted(() => ({ value: "/ar/docs/modes" }));
8
9 vi.mock("next/navigation", () => ({
10 usePathname: () => pathname.value,
11 useRouter: () => ({ push: () => {}, replace: () => {}, prefetch: () => {} }),
12 useSearchParams: () => new URLSearchParams(),
13 }));
14
15 const marker = "body text";
16 const attrs = 'lang="en" dir="ltr"';
17
18 async function renderDocs(locale: string, path: string): Promise<string> {
19 pathname.value = path;
20 const element = await DocsLayout({
21 children: createElement("div", { id: "body" }, "body"),
22 params: Promise.resolve({ locale }),
23 });
24 return renderToStaticMarkup(element);
25 }
26
27 describe("FallbackRegion", () => {
28 it("labels an English body on a partial locale and leaves translated bodies alone", () => {
29 const ar = renderToStaticMarkup(<FallbackRegion path="/changelog" locale="ar">{marker}</FallbackRegion>);
30 expect(ar).toContain(attrs);
31 for (const locale of ["en", "zh"]) {
32 const html = renderToStaticMarkup(<FallbackRegion path="/changelog" locale={locale}>{marker}</FallbackRegion>);
33 expect(html).toBe(marker);
34 }
35 });
36
37 it("wraps one inline English string in its own language and direction", () => {
38 expect(renderToStaticMarkup(createElement(EnglishText, null, "mode work/"))).toBe(
39 `<bdi ${attrs}>mode work/</bdi>`,
40 );
41 });
42 });
43
44 describe("docs layout", () => {
45 it("labels the English body and chrome for Arabic but not a translated guide body", async () => {
46 const modes = await renderDocs("ar", "/ar/docs/modes");
47 expect(modes).toContain(`<div class="locale-fallback" ${attrs}><div id="body">body</div></div>`);
48 expect(modes.split(attrs).length - 1).toBeGreaterThanOrEqual(4);
49
50 const guide = await renderDocs("ar", "/ar/docs/guide");
51 expect(guide).toContain('<div id="body">body</div>');
52 expect(guide).not.toContain(`${attrs}><div id="body">`);
53 expect(guide.split(attrs).length - 1).toBeGreaterThanOrEqual(3);
54 });
55
56 it("adds no attributes for English or Chinese", async () => {
57 for (const locale of ["en", "zh"]) {
58 const html = await renderDocs(locale, `/${locale}/docs/modes`);
59 expect(html).not.toContain(attrs);
60 expect(html).not.toContain("locale-fallback");
61 }
62 });
63 });
64
64 lines Plain Text