diff --git a/changelog.d/fixes/16059-locale-refresh-regression.md b/changelog.d/fixes/16059-locale-refresh-regression.md new file mode 100644 index 000000000000..3bebe52b81ea --- /dev/null +++ b/changelog.d/fixes/16059-locale-refresh-regression.md @@ -0,0 +1 @@ +- **Tests:** preserve the first-visit locale refresh ordering regression, checking that cookie, local storage, language and text direction are persisted before refreshing server content; isolate mock and React-root cleanup between cases (#16059). diff --git a/tests/unit/ui/LocaleAutoDetect-refresh.test.tsx b/tests/unit/ui/LocaleAutoDetect-refresh.test.tsx index 576edf84d4df..bf5223ea9a40 100644 --- a/tests/unit/ui/LocaleAutoDetect-refresh.test.tsx +++ b/tests/unit/ui/LocaleAutoDetect-refresh.test.tsx @@ -23,9 +23,11 @@ describe("LocaleAutoDetect refresh gating", () => { ( globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean } ).IS_REACT_ACT_ENVIRONMENT = true; - refresh.mockClear(); + refresh.mockReset(); // Fresh visit: no locale cookie. document.cookie = "NEXT_LOCALE=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT"; + localStorage.removeItem("NEXT_LOCALE"); + document.documentElement.dir = "ltr"; Object.defineProperty(navigator, "languages", { value: ["en-US"], configurable: true }); }); @@ -42,7 +44,7 @@ describe("LocaleAutoDetect refresh gating", () => { root.render(React.createElement(LocaleAutoDetect)); }); cleanups.push(() => { - root.unmount(); + act(() => root.unmount()); container.remove(); }); } @@ -59,6 +61,21 @@ describe("LocaleAutoDetect refresh gating", () => { expect(refresh).toHaveBeenCalledTimes(1); }); + it("persists the detected locale before refreshing server content on a first visit", async () => { + document.documentElement.lang = "en"; + Object.defineProperty(navigator, "languages", { value: ["ar"], configurable: true }); + refresh.mockImplementation(() => { + expect(document.cookie).toContain("NEXT_LOCALE=ar"); + expect(localStorage.getItem("NEXT_LOCALE")).toBe("ar"); + expect(document.documentElement.lang).toBe("ar"); + expect(document.documentElement.dir).toBe("rtl"); + }); + + await mount(); + + expect(refresh).toHaveBeenCalledTimes(1); + }); + // Alias wiring guard: the component must hand `LOCALE_ALIASES` to // `detectBrowserLocale`. `fil` (Filipino) is the discriminating input — no // locale code starts with `fil`, so with the real config it resolves to `phi`