Files
wehub-resource-sync b3a7f98e5a
CI / E2E Cloudflare (4/8) (push) Failing after 0s
CI / E2E Cloudflare (8/8) (push) Failing after 1s
CI / Lint (push) Failing after 1s
Auto Extract / Extract (push) Failing after 4s
CI / Version Check (push) Failing after 9s
CI / Integration Tests (push) Failing after 1s
CI / E2E tests (1/8) (push) Failing after 1s
CI / E2E tests (2/8) (push) Failing after 2s
CI / E2E tests (3/8) (push) Failing after 2s
CI / Browser Tests (push) Failing after 1s
CI / E2E tests (5/8) (push) Failing after 1s
CI / E2E Cloudflare (2/8) (push) Failing after 2s
CI / Typecheck (push) Failing after 1s
CI / Changeset Validation (push) Failing after 2s
CI / E2E Cloudflare (5/8) (push) Failing after 1s
CI / E2E Cloudflare (6/8) (push) Failing after 1s
CI / E2E Cloudflare (7/8) (push) Failing after 1s
CodeQL / Analyze (javascript-typescript) (push) Failing after 1s
Format / Format (push) Failing after 0s
CodeQL / Analyze (actions) (push) Failing after 4s
CI / E2E tests (4/8) (push) Failing after 1s
CI / E2E tests (6/8) (push) Failing after 1s
CI / E2E tests (7/8) (push) Failing after 2s
CI / E2E tests (8/8) (push) Failing after 1s
CI / E2E Cloudflare (1/8) (push) Failing after 1s
CI / E2E Cloudflare (3/8) (push) Failing after 2s
Preview Releases / Publish Preview (push) Failing after 0s
zizmor / Run zizmor (push) Failing after 1s
Release / Release (push) Failing after 2s
CI / Smoke Tests (push) Failing after 5m36s
CI / Tests (push) Failing after 6m36s
Release / Sync Templates (push) Has been skipped
CI / E2E Tests (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:23:53 +08:00

98 lines
2.6 KiB
TypeScript

import { i18n } from "@lingui/core";
import { I18nProvider } from "@lingui/react";
import { render, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { userEvent } from "vitest/browser";
import { LocaleDirectionProvider } from "../../src/locales/LocaleDirectionProvider.js";
import { useLocale } from "../../src/locales/useLocale.js";
const expectHTMLAttr = (attr: "lang" | "dir", expected: string | null) => {
expect(document.documentElement.getAttribute(attr)).toBe(expected);
};
describe("LocaleDirectionProvider", () => {
beforeEach(() => {
document.documentElement.removeAttribute("dir");
document.documentElement.removeAttribute("lang");
});
test("throws error when used without I18nProvider", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
try {
expect(() => {
render(
<LocaleDirectionProvider>
<div>test</div>
</LocaleDirectionProvider>,
);
}).toThrow();
} finally {
consoleErrorSpy.mockRestore();
}
});
test("works correctly when wrapped by I18nProvider", () => {
i18n.loadAndActivate({ locale: "en", messages: {} });
expect(() => {
render(
<I18nProvider i18n={i18n}>
<LocaleDirectionProvider>
<div data-testid="content">test</div>
</LocaleDirectionProvider>
</I18nProvider>,
);
}).not.toThrow();
expect(screen.getByTestId("content")).toBeInTheDocument();
});
test("updates document.documentElement.lang attribute and dir attribute for RTL locale", () => {
i18n.loadAndActivate({ locale: "ar", messages: {} });
render(
<I18nProvider i18n={i18n}>
<LocaleDirectionProvider>
<div>test</div>
</LocaleDirectionProvider>
</I18nProvider>,
);
expectHTMLAttr("lang", "ar");
expectHTMLAttr("dir", "rtl");
});
test("updates document.documentElement.lang and dir attributes when locale changes", async () => {
i18n.loadAndActivate({ locale: "en", messages: {} });
const LocaleButton = () => {
const { setLocale } = useLocale();
return (
<button type="button" data-testid="locale-button" onClick={() => setLocale("ar")}>
Dashing into Arabic
</button>
);
};
render(
<I18nProvider i18n={i18n}>
<LocaleDirectionProvider>
<LocaleButton />
</LocaleDirectionProvider>
</I18nProvider>,
);
expectHTMLAttr("dir", "ltr");
expectHTMLAttr("lang", "en");
await userEvent.click(screen.getByTestId("locale-button"));
await waitFor(() => {
expectHTMLAttr("dir", "rtl");
expectHTMLAttr("lang", "ar");
});
});
});