|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import localePlugin from "~/plugins/locale.client"; |
| 3 | +import { |
| 4 | + resetBrowserLanguages, |
| 5 | + setBrowserLanguages, |
| 6 | +} from "../helpers/browserLocale"; |
| 7 | + |
| 8 | +const PICKED_LOCALE_KEY = "5stack:locale"; |
| 9 | + |
| 10 | +const app = () => |
| 11 | + useNuxtApp() as unknown as { |
| 12 | + $i18n: { |
| 13 | + locale: { value: string }; |
| 14 | + setLocale: (code: string) => Promise<void>; |
| 15 | + }; |
| 16 | + _vueI18n: { __firstAccess: boolean }; |
| 17 | + }; |
| 18 | + |
| 19 | +const locale = () => app().$i18n.locale.value; |
| 20 | + |
| 21 | +async function boot() { |
| 22 | + app()._vueI18n.__firstAccess = true; |
| 23 | + await (localePlugin as unknown as () => Promise<void>)(); |
| 24 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 25 | +} |
| 26 | + |
| 27 | +beforeEach(() => { |
| 28 | + localStorage.clear(); |
| 29 | + document.cookie = "i18n_redirected=; max-age=0; path=/"; |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(async () => { |
| 33 | + resetBrowserLanguages(); |
| 34 | + localStorage.clear(); |
| 35 | + document.cookie = "i18n_redirected=; max-age=0; path=/"; |
| 36 | + await app().$i18n.setLocale("en"); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("the language a visitor first sees", () => { |
| 40 | + it("is Traditional Chinese for a zh-TW browser, and stays that way across pages", async () => { |
| 41 | + setBrowserLanguages(["zh-TW", "zh", "en-US"]); |
| 42 | + |
| 43 | + await boot(); |
| 44 | + expect(locale()).toBe("zh-Hant"); |
| 45 | + |
| 46 | + await useRouter().push("/faq"); |
| 47 | + await useRouter().push("/"); |
| 48 | + expect(locale()).toBe("zh-Hant"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("is the visitor's own pick over their browser", async () => { |
| 52 | + setBrowserLanguages(["zh-TW"]); |
| 53 | + localStorage.setItem(PICKED_LOCALE_KEY, "ja"); |
| 54 | + |
| 55 | + await boot(); |
| 56 | + |
| 57 | + expect(locale()).toBe("ja"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("is not written to a cookie for the next visit", async () => { |
| 61 | + setBrowserLanguages(["de-DE"]); |
| 62 | + |
| 63 | + await boot(); |
| 64 | + await useRouter().push("/faq"); |
| 65 | + await new Promise((resolve) => setTimeout(resolve, 10)); |
| 66 | + |
| 67 | + expect(document.cookie).not.toContain("i18n_redirected"); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe("the old i18n_redirected cookie", () => { |
| 72 | + it("no longer holds a zh-TW browser on Simplified Chinese", async () => { |
| 73 | + setBrowserLanguages(["zh-TW"]); |
| 74 | + document.cookie = "i18n_redirected=zh-Hans; path=/"; |
| 75 | + |
| 76 | + await boot(); |
| 77 | + |
| 78 | + expect(locale()).toBe("zh-Hant"); |
| 79 | + expect(localStorage.getItem(PICKED_LOCALE_KEY)).toBeNull(); |
| 80 | + expect(document.cookie).not.toContain("i18n_redirected"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("carries over a language the visitor picked", async () => { |
| 84 | + setBrowserLanguages(["en-US"]); |
| 85 | + document.cookie = "i18n_redirected=de; path=/"; |
| 86 | + |
| 87 | + await boot(); |
| 88 | + |
| 89 | + expect(locale()).toBe("de"); |
| 90 | + expect(localStorage.getItem(PICKED_LOCALE_KEY)).toBe("de"); |
| 91 | + expect(document.cookie).not.toContain("i18n_redirected"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments