|
| 1 | +import { act, render, screen, waitFor } from "@testing-library/react" |
| 2 | +import React from "react" |
| 3 | + |
| 4 | +import i18next, { loadLanguage } from "../setup" |
| 5 | +import TranslationProvider, { useAppTranslation } from "../TranslationContext" |
| 6 | + |
| 7 | +// The provider reads the active language from extension state. Tests drive it |
| 8 | +// through a mutable holder so a rerender can simulate a settings change. |
| 9 | +const extensionState: { language: string } = { language: "en" } |
| 10 | + |
| 11 | +vi.mock("@/context/ExtensionStateContext", () => ({ |
| 12 | + useExtensionState: () => extensionState, |
| 13 | +})) |
| 14 | + |
| 15 | +const Probe = () => { |
| 16 | + const { t, i18n } = useAppTranslation() |
| 17 | + |
| 18 | + return ( |
| 19 | + <div> |
| 20 | + <span data-testid="error-title">{t("common:errorBoundary.title")}</span> |
| 21 | + <span data-testid="active-language">{i18n.language}</span> |
| 22 | + </div> |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +const renderProbe = (language: string) => { |
| 27 | + extensionState.language = language |
| 28 | + |
| 29 | + return render( |
| 30 | + <TranslationProvider> |
| 31 | + <Probe /> |
| 32 | + </TranslationProvider>, |
| 33 | + ) |
| 34 | +} |
| 35 | + |
| 36 | +describe("TranslationProvider locale loading", () => { |
| 37 | + beforeEach(async () => { |
| 38 | + // i18next is a module-level singleton, so a language left active by an |
| 39 | + // earlier test would mask the fallback path this suite is checking. |
| 40 | + await i18next.changeLanguage("en") |
| 41 | + }) |
| 42 | + |
| 43 | + it("renders translated strings for a non-English initial language", async () => { |
| 44 | + renderProbe("ko") |
| 45 | + |
| 46 | + // Korean bundles are not in the entry chunk, so the first paint resolves |
| 47 | + // through the English fallback and the switch lands after the fetch. |
| 48 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Something went wrong") |
| 49 | + |
| 50 | + await waitFor(() => { |
| 51 | + expect(screen.getByTestId("error-title")).toHaveTextContent("문제가 발생했습니다") |
| 52 | + }) |
| 53 | + expect(screen.getByTestId("active-language")).toHaveTextContent("ko") |
| 54 | + }) |
| 55 | + |
| 56 | + it("loads a non-eager locale when the language changes at runtime", async () => { |
| 57 | + const { rerender } = renderProbe("en") |
| 58 | + |
| 59 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Something went wrong") |
| 60 | + |
| 61 | + extensionState.language = "ja" |
| 62 | + await act(async () => { |
| 63 | + rerender( |
| 64 | + <TranslationProvider> |
| 65 | + <Probe /> |
| 66 | + </TranslationProvider>, |
| 67 | + ) |
| 68 | + }) |
| 69 | + |
| 70 | + await waitFor(() => { |
| 71 | + expect(screen.getByTestId("error-title")).toHaveTextContent("問題が発生しました") |
| 72 | + }) |
| 73 | + expect(screen.getByTestId("active-language")).toHaveTextContent("ja") |
| 74 | + }) |
| 75 | + |
| 76 | + it("falls back to English when the requested locale has no resources", async () => { |
| 77 | + renderProbe("xx-unsupported") |
| 78 | + |
| 79 | + await waitFor(() => { |
| 80 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Something went wrong") |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it("keeps English available after switching away and back", async () => { |
| 85 | + const { rerender } = renderProbe("de") |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Etwas ist schiefgelaufen") |
| 89 | + }) |
| 90 | + |
| 91 | + extensionState.language = "en" |
| 92 | + await act(async () => { |
| 93 | + rerender( |
| 94 | + <TranslationProvider> |
| 95 | + <Probe /> |
| 96 | + </TranslationProvider>, |
| 97 | + ) |
| 98 | + }) |
| 99 | + |
| 100 | + await waitFor(() => { |
| 101 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Something went wrong") |
| 102 | + }) |
| 103 | + }) |
| 104 | + |
| 105 | + it("keeps the previous locale rendered while a warm switch is still loading", async () => { |
| 106 | + const { rerender } = renderProbe("de") |
| 107 | + |
| 108 | + await waitFor(() => { |
| 109 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Etwas ist schiefgelaufen") |
| 110 | + }) |
| 111 | + |
| 112 | + extensionState.language = "fr" |
| 113 | + await act(async () => { |
| 114 | + rerender( |
| 115 | + <TranslationProvider> |
| 116 | + <Probe /> |
| 117 | + </TranslationProvider>, |
| 118 | + ) |
| 119 | + }) |
| 120 | + |
| 121 | + // French is not in the entry chunk and no other case loads it, so the |
| 122 | + // switch cannot have landed yet. A `changeLanguage` fired before the fetch |
| 123 | + // resolves would paint the English fallback here instead of German. |
| 124 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Etwas ist schiefgelaufen") |
| 125 | + |
| 126 | + await waitFor(() => { |
| 127 | + expect(screen.getByTestId("error-title")).toHaveTextContent("Une erreur s'est produite") |
| 128 | + }) |
| 129 | + expect(screen.getByTestId("active-language")).toHaveTextContent("fr") |
| 130 | + }) |
| 131 | + |
| 132 | + it("skips the language switch when the provider unmounts before the fetch resolves", async () => { |
| 133 | + const changeLanguage = vi.spyOn(i18next, "changeLanguage") |
| 134 | + |
| 135 | + const { unmount } = renderProbe("vi") |
| 136 | + // The locale chunk is still in flight here, so the effect cleanup runs |
| 137 | + // before the fetch settles and `cancelled` decides the outcome. |
| 138 | + unmount() |
| 139 | + |
| 140 | + // `loadLanguage` memoizes the in-flight promise, so awaiting the same |
| 141 | + // locale here waits for the exact resolution the guard is deciding on. |
| 142 | + // The provider attached its `.then` first, so its callback has already run |
| 143 | + // by the time this returns: the switch was suppressed, not left pending. |
| 144 | + await loadLanguage("vi") |
| 145 | + |
| 146 | + expect(i18next.hasResourceBundle("vi", "common")).toBe(true) |
| 147 | + expect(changeLanguage).not.toHaveBeenCalledWith("vi") |
| 148 | + expect(i18next.language).not.toBe("vi") |
| 149 | + }) |
| 150 | +}) |
0 commit comments