@@ -3,11 +3,24 @@ import i18next, { loadLanguage } from "../setup"
33// A dynamic import that rejects is what a 404 or a chunk missing from the
44// deployed manifest looks like at runtime. One namespace is enough: `loadLanguage`
55// registers a language only after every namespace resolves.
6+ const loader = vi . hoisted ( ( ) => ( { calls : 0 , fail : true } ) )
7+
68vi . mock ( "../locales/ja/common.json" , ( ) => {
7- throw new Error ( "Failed to fetch dynamically imported module" )
9+ loader . calls ++
10+
11+ if ( loader . fail ) {
12+ throw new Error ( "Failed to fetch dynamically imported module" )
13+ }
14+
15+ return { default : { errorBoundary : { title : "問題が発生しました" } } }
816} )
917
1018describe ( "i18n setup locale load failure" , ( ) => {
19+ beforeEach ( ( ) => {
20+ loader . calls = 0
21+ loader . fail = true
22+ } )
23+
1124 afterEach ( ( ) => {
1225 vi . restoreAllMocks ( )
1326 } )
@@ -29,7 +42,10 @@ describe("i18n setup locale load failure", () => {
2942 const warn = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => undefined )
3043
3144 await loadLanguage ( "ja" )
45+ expect ( loader . calls ) . toBe ( 1 )
46+
3247 await loadLanguage ( "ja" )
48+ expect ( loader . calls ) . toBe ( 2 )
3349
3450 // `loadedLanguages` is only updated on success and `pendingLanguages` is
3551 // cleared in `finally`, so a transient failure must not be memoized.
@@ -39,4 +55,16 @@ describe("i18n setup locale load failure", () => {
3955 // without re-requesting the chunk.
4056 expect ( warn ) . toHaveBeenCalledTimes ( 2 )
4157 } )
58+
59+ it ( "registers the bundles when a later attempt succeeds" , async ( ) => {
60+ vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => undefined )
61+
62+ await loadLanguage ( "ja" )
63+ expect ( i18next . hasResourceBundle ( "ja" , "common" ) ) . toBe ( false )
64+
65+ loader . fail = false
66+ await loadLanguage ( "ja" )
67+
68+ expect ( i18next . t ( "common:errorBoundary.title" , { lng : "ja" } ) ) . toBe ( "問題が発生しました" )
69+ } )
4270} )
0 commit comments