Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixes/16059-locale-refresh-regression.md
Original file line number Diff line number Diff line change
@@ -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).
21 changes: 19 additions & 2 deletions tests/unit/ui/LocaleAutoDetect-refresh.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand All @@ -42,7 +44,7 @@ describe("LocaleAutoDetect refresh gating", () => {
root.render(React.createElement(LocaleAutoDetect));
});
cleanups.push(() => {
root.unmount();
act(() => root.unmount());
container.remove();
});
}
Expand All @@ -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`
Expand Down
Loading