Files
OmniRoute/tests/unit/ui/LocaleAutoDetect-refresh.test.tsx
Diego Rodrigues de Sa e Souza 103236db7c fix(release): round-2 CI findings — LocaleAutoDetect refresh gating + ratchet tighten
- fix(i18n): LocaleAutoDetect (#5979) refreshed the router on EVERY cookie-less
  first visit, even when the detected locale matched the server-rendered
  <html lang> — re-navigating mid-interaction (flaky e2e 'execution context
  destroyed' + visible flash for new visitors). Refresh now only fires when
  the locale actually differs; regression test added.
- quality: tighten openapiCoverage.pct 36.9->39.3 (require-tighten gate on the
  release PR; value measured by the CI Quality Ratchet on 00c55afcb)
- quality(file-size): shrink the ProxyRegistryManager TDZ note to fit the
  1117-line freeze (prettier reflow added a line at commit time)
- changelog bullet + 42 i18n mirrors re-synced
2026-07-04 11:29:15 -03:00

62 lines
2.1 KiB
TypeScript

// @vitest-environment jsdom
import React from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// Regression guard (release v3.8.44 e2e finding): LocaleAutoDetect called
// router.refresh() on EVERY first visit — even when the detected browser
// locale was exactly the one the server had just rendered — re-navigating the
// page mid-interaction (Playwright: "Execution context was destroyed") and
// flashing for every new visitor. It must refresh ONLY when the detected
// locale differs from <html lang>.
const refresh = vi.fn();
vi.mock("next/navigation", () => ({
useRouter: () => ({ refresh }),
}));
describe("LocaleAutoDetect refresh gating", () => {
const cleanups: Array<() => void> = [];
beforeEach(() => {
(
globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
refresh.mockClear();
// Fresh visit: no locale cookie.
document.cookie = "NEXT_LOCALE=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
Object.defineProperty(navigator, "languages", { value: ["en-US"], configurable: true });
});
afterEach(() => {
while (cleanups.length) cleanups.pop()?.();
});
async function mount() {
const { LocaleAutoDetect } = await import("@/shared/components/LocaleAutoDetect");
const container = document.createElement("div");
document.body.appendChild(container);
const root = createRoot(container);
await act(async () => {
root.render(React.createElement(LocaleAutoDetect));
});
cleanups.push(() => {
root.unmount();
container.remove();
});
}
it("does NOT refresh when the detected locale equals the server-rendered <html lang>", async () => {
document.documentElement.lang = "en";
await mount();
expect(refresh).not.toHaveBeenCalled();
});
it("refreshes when the detected locale differs from the server-rendered <html lang>", async () => {
document.documentElement.lang = "fr";
await mount();
expect(refresh).toHaveBeenCalledTimes(1);
});
});