@bleedingdev/modern-js-plugin-i18n 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/index.js +0 -22
- package/dist/cjs/runtime/Link.js +19 -32
- package/dist/cjs/runtime/context.js +26 -61
- package/dist/cjs/runtime/contextHelpers.js +5 -5
- package/dist/cjs/runtime/core.js +17 -14
- package/dist/cjs/runtime/hooks.js +7 -8
- package/dist/cjs/runtime/localizedPaths.js +11 -9
- package/dist/cjs/runtime/providerComposition.js +37 -13
- package/dist/cjs/runtime/routerAdapter.js +35 -140
- package/dist/cjs/runtime/utils.js +17 -19
- package/dist/cjs/server/index.js +10 -7
- package/dist/cjs/server/redirectPolicy.js +36 -31
- package/dist/cjs/shared/urlStrategy.js +18 -0
- package/dist/esm/cli/index.mjs +0 -22
- package/dist/esm/runtime/Link.mjs +19 -32
- package/dist/esm/runtime/context.mjs +18 -62
- package/dist/esm/runtime/contextHelpers.mjs +5 -5
- package/dist/esm/runtime/core.mjs +8 -5
- package/dist/esm/runtime/hooks.mjs +7 -8
- package/dist/esm/runtime/localizedPaths.mjs +11 -9
- package/dist/esm/runtime/providerComposition.mjs +38 -14
- package/dist/esm/runtime/routerAdapter.mjs +30 -141
- package/dist/esm/runtime/utils.mjs +15 -17
- package/dist/esm/server/index.mjs +10 -7
- package/dist/esm/server/redirectPolicy.mjs +29 -24
- package/dist/esm/shared/urlStrategy.mjs +0 -0
- package/dist/esm-node/cli/index.mjs +0 -22
- package/dist/esm-node/runtime/Link.mjs +19 -32
- package/dist/esm-node/runtime/context.mjs +18 -62
- package/dist/esm-node/runtime/contextHelpers.mjs +5 -5
- package/dist/esm-node/runtime/core.mjs +8 -5
- package/dist/esm-node/runtime/hooks.mjs +7 -8
- package/dist/esm-node/runtime/localizedPaths.mjs +11 -9
- package/dist/esm-node/runtime/providerComposition.mjs +38 -14
- package/dist/esm-node/runtime/routerAdapter.mjs +30 -141
- package/dist/esm-node/runtime/utils.mjs +15 -17
- package/dist/esm-node/server/index.mjs +10 -7
- package/dist/esm-node/server/redirectPolicy.mjs +29 -24
- package/dist/esm-node/shared/urlStrategy.mjs +1 -0
- package/dist/types/runtime/context.d.ts +8 -11
- package/dist/types/runtime/contextHelpers.d.ts +2 -2
- package/dist/types/runtime/core.d.ts +11 -2
- package/dist/types/runtime/hooks.d.ts +4 -5
- package/dist/types/runtime/localizedPaths.d.ts +4 -4
- package/dist/types/runtime/providerComposition.d.ts +13 -1
- package/dist/types/runtime/routerAdapter.d.ts +23 -6
- package/dist/types/runtime/utils.d.ts +4 -12
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/redirectPolicy.d.ts +5 -13
- package/dist/types/shared/type.d.ts +0 -14
- package/dist/types/shared/urlStrategy.d.ts +7 -0
- package/package.json +21 -11
- package/rstest.config.mts +2 -8
- package/src/cli/index.ts +1 -43
- package/src/runtime/Link.tsx +20 -40
- package/src/runtime/context.tsx +34 -133
- package/src/runtime/contextHelpers.ts +7 -5
- package/src/runtime/core.tsx +26 -6
- package/src/runtime/hooks.ts +14 -9
- package/src/runtime/localizedPaths.ts +24 -19
- package/src/runtime/providerComposition.tsx +71 -23
- package/src/runtime/routerAdapter.tsx +73 -338
- package/src/runtime/utils.ts +40 -31
- package/src/server/index.ts +44 -9
- package/src/server/redirectPolicy.ts +70 -70
- package/src/shared/type.ts +0 -14
- package/src/shared/urlStrategy.ts +11 -0
- package/tests/federatedI18nBoundaryResolver.test.tsx +71 -0
- package/tests/i18nUtils.test.ts +31 -65
- package/tests/link.test.tsx +13 -511
- package/tests/linkTypes.test.ts +12 -3
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +4 -96
- package/tests/localisedUrls.test.ts +7 -216
- package/tests/reactI18nextRuntimeBoundary.test.ts +1 -82
- package/tests/redirectPolicy.test.ts +39 -30
- package/tests/routerAdapter.test.tsx +95 -1055
- package/tests/federatedI18nBoundary.client.test.tsx +0 -129
- package/tests/federatedI18nBoundary.test.tsx +0 -132
- package/tests/peerCohort.test.ts +0 -25
|
@@ -1,56 +1,34 @@
|
|
|
1
1
|
import {
|
|
2
|
-
applyRouterRuntimeState,
|
|
3
2
|
InternalRuntimeContext,
|
|
4
3
|
RuntimeContext,
|
|
5
4
|
} from '@modern-js/runtime/context';
|
|
6
|
-
import {
|
|
7
|
-
createMemoryRouter,
|
|
8
|
-
Link as ReactRouterLink,
|
|
9
|
-
RouterProvider,
|
|
10
|
-
} from '@modern-js/runtime/router';
|
|
11
|
-
import i18next from 'i18next';
|
|
12
5
|
import type React from 'react';
|
|
13
6
|
import type { ComponentType, PropsWithChildren } from 'react';
|
|
14
|
-
import { act, useState } from 'react';
|
|
7
|
+
import { act, useEffect, useState } from 'react';
|
|
15
8
|
import { createRoot, type Root } from 'react-dom/client';
|
|
16
|
-
import { useTranslation } from 'react-i18next';
|
|
17
9
|
import { i18nPlugin } from '../src/runtime';
|
|
18
10
|
import { ModernI18nProvider, useModernI18n } from '../src/runtime/context';
|
|
19
|
-
import { I18nLink } from '../src/runtime/I18nLink';
|
|
20
11
|
import type { I18nInstance } from '../src/runtime/i18n';
|
|
21
12
|
import { getReactI18nextIntegration } from '../src/runtime/i18n/react-i18next';
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
13
|
+
import { Link } from '../src/runtime/Link';
|
|
14
|
+
import {
|
|
15
|
+
createI18nRootWrapper,
|
|
16
|
+
type I18nLanguageSynchronizationProps,
|
|
17
|
+
} from '../src/runtime/providerComposition';
|
|
18
|
+
import {
|
|
19
|
+
I18nNavigationProvider,
|
|
20
|
+
useI18nRouterAdapter,
|
|
21
|
+
} from '../src/runtime/routerAdapter';
|
|
24
22
|
|
|
25
23
|
(
|
|
26
24
|
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
|
|
27
25
|
).IS_REACT_ACT_ENVIRONMENT = true;
|
|
28
26
|
|
|
29
|
-
const localisedUrls = {
|
|
30
|
-
'/terms-of-service': {
|
|
31
|
-
en: '/terms-of-service',
|
|
32
|
-
cs: '/podminky-pouzivani',
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
|
|
36
27
|
const requestContext = {
|
|
37
28
|
request: {},
|
|
38
29
|
response: {},
|
|
39
30
|
};
|
|
40
31
|
|
|
41
|
-
const capturedTanstackLinkProps: any[] = [];
|
|
42
|
-
|
|
43
|
-
const TanstackLink = ({ to, children, ...props }: any) => {
|
|
44
|
-
capturedTanstackLinkProps.push({ to, ...props });
|
|
45
|
-
const { prefetch: _prefetch, preload: _preload, ...anchorProps } = props;
|
|
46
|
-
|
|
47
|
-
return (
|
|
48
|
-
<a href={to} data-router-link="tanstack" {...anchorProps}>
|
|
49
|
-
{children}
|
|
50
|
-
</a>
|
|
51
|
-
);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
32
|
function createI18nInstance(language = 'en'): I18nInstance {
|
|
55
33
|
return {
|
|
56
34
|
language,
|
|
@@ -66,37 +44,6 @@ function createI18nInstance(language = 'en'): I18nInstance {
|
|
|
66
44
|
};
|
|
67
45
|
}
|
|
68
46
|
|
|
69
|
-
function createRuntimeContext(
|
|
70
|
-
router: unknown,
|
|
71
|
-
framework: 'tanstack' | 'react-router',
|
|
72
|
-
) {
|
|
73
|
-
const context = {
|
|
74
|
-
isBrowser: true,
|
|
75
|
-
requestContext,
|
|
76
|
-
context: requestContext,
|
|
77
|
-
router: {
|
|
78
|
-
...(framework === 'tanstack'
|
|
79
|
-
? { Link: TanstackLink, useRouter: () => router }
|
|
80
|
-
: { useLocation: () => undefined, useHref: () => undefined }),
|
|
81
|
-
},
|
|
82
|
-
} as any;
|
|
83
|
-
|
|
84
|
-
applyRouterRuntimeState(context, {
|
|
85
|
-
framework,
|
|
86
|
-
instance: router,
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
return context;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function createTanstackRuntimeContext(router: unknown) {
|
|
93
|
-
return createRuntimeContext(router, 'tanstack');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function createReactRouterRuntimeContext(router: unknown) {
|
|
97
|
-
return createRuntimeContext(router, 'react-router');
|
|
98
|
-
}
|
|
99
|
-
|
|
100
47
|
function collectI18nWrapRoot() {
|
|
101
48
|
let wrapRoot: ((App: ComponentType<any>) => ComponentType<any>) | undefined;
|
|
102
49
|
|
|
@@ -120,254 +67,6 @@ function collectI18nWrapRoot() {
|
|
|
120
67
|
return wrapRoot;
|
|
121
68
|
}
|
|
122
69
|
|
|
123
|
-
async function collectI18nRuntime(
|
|
124
|
-
i18nInstance: I18nInstance,
|
|
125
|
-
reactI18next = false,
|
|
126
|
-
) {
|
|
127
|
-
let onBeforeRender:
|
|
128
|
-
| ((
|
|
129
|
-
context: ReturnType<typeof createTanstackRuntimeContext>,
|
|
130
|
-
) => Promise<void>)
|
|
131
|
-
| undefined;
|
|
132
|
-
let wrapRoot: ((App: ComponentType<any>) => ComponentType<any>) | undefined;
|
|
133
|
-
|
|
134
|
-
i18nPlugin({
|
|
135
|
-
i18nInstance,
|
|
136
|
-
reactI18next,
|
|
137
|
-
localeDetection: {
|
|
138
|
-
fallbackLanguage: 'en',
|
|
139
|
-
i18nextDetector: false,
|
|
140
|
-
languages: ['en', 'cs'],
|
|
141
|
-
localePathRedirect: true,
|
|
142
|
-
},
|
|
143
|
-
}).setup?.({
|
|
144
|
-
getRuntimeConfig: () => ({}),
|
|
145
|
-
onBeforeRender: (
|
|
146
|
-
callback: (
|
|
147
|
-
context: ReturnType<typeof createTanstackRuntimeContext>,
|
|
148
|
-
) => Promise<void>,
|
|
149
|
-
) => {
|
|
150
|
-
onBeforeRender = callback;
|
|
151
|
-
},
|
|
152
|
-
wrapRoot: (callback: (App: ComponentType<any>) => ComponentType<any>) => {
|
|
153
|
-
wrapRoot = callback;
|
|
154
|
-
},
|
|
155
|
-
} as any);
|
|
156
|
-
|
|
157
|
-
if (!onBeforeRender || !wrapRoot) {
|
|
158
|
-
throw new Error('Expected i18n runtime plugin lifecycle registrations');
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return { onBeforeRender, wrapRoot };
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
async function createEventEmittingDeferredI18nInstance() {
|
|
165
|
-
const instance = i18next.createInstance();
|
|
166
|
-
await instance.init({
|
|
167
|
-
lng: 'en',
|
|
168
|
-
fallbackLng: 'en',
|
|
169
|
-
resources: {
|
|
170
|
-
en: { translation: { languageSwitcher: 'Language' } },
|
|
171
|
-
cs: { translation: { languageSwitcher: 'Jazyk' } },
|
|
172
|
-
},
|
|
173
|
-
});
|
|
174
|
-
const originalHasLoadedNamespace = instance.hasLoadedNamespace;
|
|
175
|
-
instance.hasLoadedNamespace = function hasLoadedNamespace(
|
|
176
|
-
namespace,
|
|
177
|
-
options,
|
|
178
|
-
) {
|
|
179
|
-
if (this !== instance) {
|
|
180
|
-
throw new Error('i18next hasLoadedNamespace receiver was not preserved');
|
|
181
|
-
}
|
|
182
|
-
return originalHasLoadedNamespace.call(this, namespace, options);
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
const pending: Array<{
|
|
186
|
-
language: string;
|
|
187
|
-
promise: Promise<unknown>;
|
|
188
|
-
resolve: () => void;
|
|
189
|
-
}> = [];
|
|
190
|
-
const translator = (instance as unknown as { translator: I18nInstance })
|
|
191
|
-
.translator;
|
|
192
|
-
|
|
193
|
-
instance.changeLanguage = rstest.fn((language = 'en') => {
|
|
194
|
-
let resolveChange!: (value: unknown) => void;
|
|
195
|
-
const promise = new Promise<unknown>(resolve => {
|
|
196
|
-
resolveChange = resolve;
|
|
197
|
-
});
|
|
198
|
-
pending.push({
|
|
199
|
-
language,
|
|
200
|
-
promise,
|
|
201
|
-
resolve: () => {
|
|
202
|
-
instance.language = language;
|
|
203
|
-
translator.changeLanguage?.(language);
|
|
204
|
-
instance.emit('languageChanged', language);
|
|
205
|
-
resolveChange(instance.t.bind(instance));
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
return promise;
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
return { instance, pending };
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function createMutableTanstackRouter(pathname = '/en') {
|
|
215
|
-
let location = {
|
|
216
|
-
pathname,
|
|
217
|
-
searchStr: '',
|
|
218
|
-
hash: '',
|
|
219
|
-
};
|
|
220
|
-
let matches = [{ params: { lang: pathname.slice(1) } }];
|
|
221
|
-
const listeners = new Set<() => void>();
|
|
222
|
-
|
|
223
|
-
return {
|
|
224
|
-
navigate: rstest.fn(async () => undefined),
|
|
225
|
-
stores: {
|
|
226
|
-
location: {
|
|
227
|
-
get: () => location,
|
|
228
|
-
subscribe: (listener: () => void) => {
|
|
229
|
-
listeners.add(listener);
|
|
230
|
-
return () => listeners.delete(listener);
|
|
231
|
-
},
|
|
232
|
-
},
|
|
233
|
-
matches: {
|
|
234
|
-
get: () => matches,
|
|
235
|
-
},
|
|
236
|
-
},
|
|
237
|
-
publishPathname(nextPathname: string) {
|
|
238
|
-
location = {
|
|
239
|
-
pathname: nextPathname,
|
|
240
|
-
searchStr: '',
|
|
241
|
-
hash: '',
|
|
242
|
-
};
|
|
243
|
-
matches = [{ params: { lang: nextPathname.slice(1) } }];
|
|
244
|
-
for (const listener of listeners) {
|
|
245
|
-
listener();
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
publishStateUpdate() {
|
|
249
|
-
for (const listener of listeners) {
|
|
250
|
-
listener();
|
|
251
|
-
}
|
|
252
|
-
},
|
|
253
|
-
};
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
function createMutableReactRouter(
|
|
257
|
-
pathname = '/en',
|
|
258
|
-
params: Record<string, string> = { lang: pathname.slice(1) },
|
|
259
|
-
) {
|
|
260
|
-
const listeners = new Set<() => void>();
|
|
261
|
-
const router = {
|
|
262
|
-
state: {
|
|
263
|
-
location: {
|
|
264
|
-
pathname,
|
|
265
|
-
search: '',
|
|
266
|
-
hash: '',
|
|
267
|
-
},
|
|
268
|
-
matches: [{ params }],
|
|
269
|
-
fetchers: new Map<string, unknown>(),
|
|
270
|
-
},
|
|
271
|
-
subscribe(listener: () => void) {
|
|
272
|
-
listeners.add(listener);
|
|
273
|
-
return () => listeners.delete(listener);
|
|
274
|
-
},
|
|
275
|
-
publishFetcherUpdate() {
|
|
276
|
-
router.state = {
|
|
277
|
-
...router.state,
|
|
278
|
-
fetchers: new Map([['fetcher', { state: 'idle' }]]),
|
|
279
|
-
};
|
|
280
|
-
for (const listener of listeners) {
|
|
281
|
-
listener();
|
|
282
|
-
}
|
|
283
|
-
},
|
|
284
|
-
publishPathname(nextPathname: string) {
|
|
285
|
-
router.state = {
|
|
286
|
-
...router.state,
|
|
287
|
-
location: {
|
|
288
|
-
...router.state.location,
|
|
289
|
-
pathname: nextPathname,
|
|
290
|
-
},
|
|
291
|
-
};
|
|
292
|
-
for (const listener of listeners) {
|
|
293
|
-
listener();
|
|
294
|
-
}
|
|
295
|
-
},
|
|
296
|
-
publishParams(nextParams: Record<string, string>) {
|
|
297
|
-
router.state = {
|
|
298
|
-
...router.state,
|
|
299
|
-
matches: [{ params: nextParams }],
|
|
300
|
-
};
|
|
301
|
-
for (const listener of listeners) {
|
|
302
|
-
listener();
|
|
303
|
-
}
|
|
304
|
-
},
|
|
305
|
-
};
|
|
306
|
-
|
|
307
|
-
return router;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function createDeferredI18nInstance() {
|
|
311
|
-
const resources = {
|
|
312
|
-
en: { languageSwitcher: 'Language' },
|
|
313
|
-
cs: { languageSwitcher: 'Jazyk' },
|
|
314
|
-
};
|
|
315
|
-
const pending: Array<{
|
|
316
|
-
language: keyof typeof resources;
|
|
317
|
-
promise: Promise<void>;
|
|
318
|
-
resolve: () => void;
|
|
319
|
-
reject: (error: Error) => void;
|
|
320
|
-
}> = [];
|
|
321
|
-
const instance = createI18nInstance('en');
|
|
322
|
-
delete instance.setLang;
|
|
323
|
-
instance.t = (key: string) =>
|
|
324
|
-
resources[instance.language as keyof typeof resources][
|
|
325
|
-
key as keyof (typeof resources)['en']
|
|
326
|
-
];
|
|
327
|
-
instance.changeLanguage = rstest.fn((language = 'en') => {
|
|
328
|
-
let resolveChange!: () => void;
|
|
329
|
-
let rejectChange!: (error: Error) => void;
|
|
330
|
-
const promise = new Promise<void>((resolve, reject) => {
|
|
331
|
-
resolveChange = resolve;
|
|
332
|
-
rejectChange = reject;
|
|
333
|
-
});
|
|
334
|
-
pending.push({
|
|
335
|
-
language: language as keyof typeof resources,
|
|
336
|
-
promise,
|
|
337
|
-
resolve: () => {
|
|
338
|
-
instance.language = language;
|
|
339
|
-
resolveChange();
|
|
340
|
-
},
|
|
341
|
-
reject: rejectChange,
|
|
342
|
-
});
|
|
343
|
-
return promise;
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
return { instance, pending };
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function createTanstackRouter(pathname = '/en/terms-of-service', lang = 'en') {
|
|
350
|
-
const url = new URL(pathname, 'https://modernjs.test');
|
|
351
|
-
|
|
352
|
-
return {
|
|
353
|
-
navigate: rstest.fn(async () => undefined),
|
|
354
|
-
state: {
|
|
355
|
-
location: {
|
|
356
|
-
pathname: url.pathname,
|
|
357
|
-
searchStr: url.search,
|
|
358
|
-
hash: url.hash,
|
|
359
|
-
},
|
|
360
|
-
matches: [
|
|
361
|
-
{
|
|
362
|
-
params: {
|
|
363
|
-
lang,
|
|
364
|
-
},
|
|
365
|
-
},
|
|
366
|
-
],
|
|
367
|
-
},
|
|
368
|
-
};
|
|
369
|
-
}
|
|
370
|
-
|
|
371
70
|
async function renderI18nRoot(node: React.ReactNode) {
|
|
372
71
|
const container = document.createElement('div');
|
|
373
72
|
document.body.appendChild(container);
|
|
@@ -393,52 +92,6 @@ async function renderI18nRoot(node: React.ReactNode) {
|
|
|
393
92
|
};
|
|
394
93
|
}
|
|
395
94
|
|
|
396
|
-
async function renderWithRuntime(
|
|
397
|
-
node: React.ReactNode,
|
|
398
|
-
runtimeContext: ReturnType<typeof createTanstackRuntimeContext>,
|
|
399
|
-
) {
|
|
400
|
-
const container = document.createElement('div');
|
|
401
|
-
document.body.appendChild(container);
|
|
402
|
-
const root = createRoot(container);
|
|
403
|
-
|
|
404
|
-
await act(async () => {
|
|
405
|
-
root.render(
|
|
406
|
-
<InternalRuntimeContext.Provider value={runtimeContext}>
|
|
407
|
-
{node}
|
|
408
|
-
</InternalRuntimeContext.Provider>,
|
|
409
|
-
);
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
return {
|
|
413
|
-
container,
|
|
414
|
-
root,
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
async function renderWithRuntimeContexts(
|
|
419
|
-
node: React.ReactNode,
|
|
420
|
-
runtimeContext: ReturnType<typeof createTanstackRuntimeContext>,
|
|
421
|
-
) {
|
|
422
|
-
const container = document.createElement('div');
|
|
423
|
-
document.body.appendChild(container);
|
|
424
|
-
const root = createRoot(container);
|
|
425
|
-
|
|
426
|
-
await act(async () => {
|
|
427
|
-
root.render(
|
|
428
|
-
<InternalRuntimeContext.Provider value={runtimeContext}>
|
|
429
|
-
<RuntimeContext.Provider value={runtimeContext}>
|
|
430
|
-
{node}
|
|
431
|
-
</RuntimeContext.Provider>
|
|
432
|
-
</InternalRuntimeContext.Provider>,
|
|
433
|
-
);
|
|
434
|
-
});
|
|
435
|
-
|
|
436
|
-
return {
|
|
437
|
-
container,
|
|
438
|
-
root,
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
|
|
442
95
|
function cleanup(rendered?: { container: HTMLElement; root: Root }) {
|
|
443
96
|
if (!rendered) {
|
|
444
97
|
return;
|
|
@@ -566,255 +219,6 @@ describe('i18n runtime wrapRoot', () => {
|
|
|
566
219
|
expect(providerInstances.length).toBeGreaterThan(1);
|
|
567
220
|
expect(providerInstances.at(-1)).toBe(initialProviderInstance);
|
|
568
221
|
});
|
|
569
|
-
|
|
570
|
-
test('commits route language and translated copy after the instance changes', async () => {
|
|
571
|
-
window.history.replaceState(null, '', '/en');
|
|
572
|
-
const router = createMutableTanstackRouter();
|
|
573
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
574
|
-
const { instance, pending } = createDeferredI18nInstance();
|
|
575
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(instance);
|
|
576
|
-
await onBeforeRender(runtimeContext);
|
|
577
|
-
|
|
578
|
-
const Translation = ({ id }: { id: string }) => {
|
|
579
|
-
const { language, t } = useModernI18n();
|
|
580
|
-
return (
|
|
581
|
-
<span data-testid={id}>
|
|
582
|
-
{language}:{t('languageSwitcher')}
|
|
583
|
-
</span>
|
|
584
|
-
);
|
|
585
|
-
};
|
|
586
|
-
const App = () => (
|
|
587
|
-
<main>
|
|
588
|
-
<Translation id="first" />
|
|
589
|
-
<Translation id="second" />
|
|
590
|
-
</main>
|
|
591
|
-
);
|
|
592
|
-
const I18nRoot = wrapRoot(App);
|
|
593
|
-
|
|
594
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
595
|
-
|
|
596
|
-
expect(rendered.container.textContent).toBe('en:Languageen:Language');
|
|
597
|
-
|
|
598
|
-
await act(async () => {
|
|
599
|
-
router.publishPathname('/cs');
|
|
600
|
-
});
|
|
601
|
-
|
|
602
|
-
expect(instance.changeLanguage).toHaveBeenCalledTimes(1);
|
|
603
|
-
expect(instance.changeLanguage).toHaveBeenCalledWith('cs');
|
|
604
|
-
expect(rendered.container.textContent).toBe('en:Languageen:Language');
|
|
605
|
-
|
|
606
|
-
await act(async () => {
|
|
607
|
-
pending[0].resolve();
|
|
608
|
-
await pending[0].promise;
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
expect(rendered.container.textContent).toBe('cs:Jazykcs:Jazyk');
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
test('uses the wrapper language operation without changing twice', async () => {
|
|
615
|
-
window.history.replaceState(null, '', '/en');
|
|
616
|
-
const router = createMutableTanstackRouter();
|
|
617
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
618
|
-
const instance = createI18nInstance();
|
|
619
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(instance);
|
|
620
|
-
await onBeforeRender(runtimeContext);
|
|
621
|
-
|
|
622
|
-
const App = () => {
|
|
623
|
-
const { language } = useModernI18n();
|
|
624
|
-
return <main>{language}</main>;
|
|
625
|
-
};
|
|
626
|
-
const I18nRoot = wrapRoot(App);
|
|
627
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
628
|
-
|
|
629
|
-
await act(async () => {
|
|
630
|
-
router.publishPathname('/cs');
|
|
631
|
-
await Promise.resolve();
|
|
632
|
-
});
|
|
633
|
-
|
|
634
|
-
expect(instance.setLang).toHaveBeenCalledTimes(1);
|
|
635
|
-
expect(instance.setLang).toHaveBeenCalledWith('cs');
|
|
636
|
-
expect(instance.changeLanguage).not.toHaveBeenCalled();
|
|
637
|
-
expect(rendered.container.textContent).toBe('cs');
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
test('keeps the latest route when language changes resolve out of order', async () => {
|
|
641
|
-
window.history.replaceState(null, '', '/en');
|
|
642
|
-
const router = createMutableTanstackRouter();
|
|
643
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
644
|
-
const { instance, pending } = createDeferredI18nInstance();
|
|
645
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(instance);
|
|
646
|
-
await onBeforeRender(runtimeContext);
|
|
647
|
-
|
|
648
|
-
const App = () => {
|
|
649
|
-
const { language, t } = useModernI18n();
|
|
650
|
-
return (
|
|
651
|
-
<main>
|
|
652
|
-
{language}:{t('languageSwitcher')}
|
|
653
|
-
</main>
|
|
654
|
-
);
|
|
655
|
-
};
|
|
656
|
-
const I18nRoot = wrapRoot(App);
|
|
657
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
658
|
-
|
|
659
|
-
await act(async () => {
|
|
660
|
-
router.publishPathname('/cs');
|
|
661
|
-
});
|
|
662
|
-
expect(pending).toHaveLength(1);
|
|
663
|
-
|
|
664
|
-
await act(async () => {
|
|
665
|
-
router.publishPathname('/en');
|
|
666
|
-
});
|
|
667
|
-
expect(rendered.container.textContent).toBe('en:Language');
|
|
668
|
-
|
|
669
|
-
await act(async () => {
|
|
670
|
-
pending[0].resolve();
|
|
671
|
-
await pending[0].promise;
|
|
672
|
-
});
|
|
673
|
-
|
|
674
|
-
expect(rendered.container.textContent).toBe('en:Language');
|
|
675
|
-
expect(pending).toHaveLength(2);
|
|
676
|
-
expect(pending[1].language).toBe('en');
|
|
677
|
-
|
|
678
|
-
await act(async () => {
|
|
679
|
-
pending[1].resolve();
|
|
680
|
-
await pending[1].promise;
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
expect(rendered.container.textContent).toBe('en:Language');
|
|
684
|
-
expect(instance.language).toBe('en');
|
|
685
|
-
});
|
|
686
|
-
|
|
687
|
-
test('does not block a replacement instance behind abandoned language work', async () => {
|
|
688
|
-
window.history.replaceState(null, '', '/en');
|
|
689
|
-
const router = createMutableTanstackRouter();
|
|
690
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
691
|
-
const oldI18n = createDeferredI18nInstance();
|
|
692
|
-
runtimeContext.i18nInstance = oldI18n.instance;
|
|
693
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(
|
|
694
|
-
oldI18n.instance,
|
|
695
|
-
);
|
|
696
|
-
await onBeforeRender(runtimeContext);
|
|
697
|
-
|
|
698
|
-
const App = () => {
|
|
699
|
-
const { language } = useModernI18n();
|
|
700
|
-
return <main>{language}</main>;
|
|
701
|
-
};
|
|
702
|
-
const I18nRoot = wrapRoot(App);
|
|
703
|
-
rendered = await renderWithRuntimeContexts(<I18nRoot />, runtimeContext);
|
|
704
|
-
|
|
705
|
-
await act(async () => {
|
|
706
|
-
router.publishPathname('/cs');
|
|
707
|
-
});
|
|
708
|
-
expect(oldI18n.pending).toHaveLength(1);
|
|
709
|
-
|
|
710
|
-
const replacementI18n = createDeferredI18nInstance();
|
|
711
|
-
const replacementContext = createTanstackRuntimeContext(router);
|
|
712
|
-
replacementContext.i18nInstance = replacementI18n.instance;
|
|
713
|
-
await act(async () => {
|
|
714
|
-
rendered?.root.render(
|
|
715
|
-
<InternalRuntimeContext.Provider value={replacementContext}>
|
|
716
|
-
<RuntimeContext.Provider value={replacementContext}>
|
|
717
|
-
<I18nRoot />
|
|
718
|
-
</RuntimeContext.Provider>
|
|
719
|
-
</InternalRuntimeContext.Provider>,
|
|
720
|
-
);
|
|
721
|
-
});
|
|
722
|
-
|
|
723
|
-
expect(replacementI18n.pending).toHaveLength(1);
|
|
724
|
-
expect(replacementI18n.pending[0].language).toBe('cs');
|
|
725
|
-
});
|
|
726
|
-
|
|
727
|
-
test('does not expose an obsolete route language through react-i18next', async () => {
|
|
728
|
-
window.history.replaceState(null, '', '/en');
|
|
729
|
-
const router = createMutableTanstackRouter();
|
|
730
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
731
|
-
const { instance, pending } =
|
|
732
|
-
await createEventEmittingDeferredI18nInstance();
|
|
733
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(
|
|
734
|
-
instance,
|
|
735
|
-
true,
|
|
736
|
-
);
|
|
737
|
-
await onBeforeRender(runtimeContext);
|
|
738
|
-
|
|
739
|
-
const App = () => {
|
|
740
|
-
const { t } = useTranslation();
|
|
741
|
-
return <main>{t('languageSwitcher')}</main>;
|
|
742
|
-
};
|
|
743
|
-
const I18nRoot = wrapRoot(App);
|
|
744
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
745
|
-
|
|
746
|
-
await act(async () => {
|
|
747
|
-
router.publishPathname('/cs');
|
|
748
|
-
});
|
|
749
|
-
expect(pending).toHaveLength(1);
|
|
750
|
-
|
|
751
|
-
await act(async () => {
|
|
752
|
-
router.publishPathname('/en');
|
|
753
|
-
});
|
|
754
|
-
|
|
755
|
-
await act(async () => {
|
|
756
|
-
pending[0].resolve();
|
|
757
|
-
await pending[0].promise;
|
|
758
|
-
});
|
|
759
|
-
|
|
760
|
-
expect(rendered.container.textContent).toBe('Language');
|
|
761
|
-
});
|
|
762
|
-
|
|
763
|
-
test('keeps committed copy after a failed change and retries automatically', async () => {
|
|
764
|
-
rstest.useFakeTimers();
|
|
765
|
-
window.history.replaceState(null, '', '/en');
|
|
766
|
-
rstest.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
767
|
-
const router = createMutableTanstackRouter();
|
|
768
|
-
const runtimeContext = createTanstackRuntimeContext(router);
|
|
769
|
-
const { instance, pending } = createDeferredI18nInstance();
|
|
770
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(instance);
|
|
771
|
-
await onBeforeRender(runtimeContext);
|
|
772
|
-
|
|
773
|
-
const RetryConsumer = () => {
|
|
774
|
-
useModernI18n();
|
|
775
|
-
return null;
|
|
776
|
-
};
|
|
777
|
-
const App = () => {
|
|
778
|
-
const [showRetryConsumer, setShowRetryConsumer] = useState(false);
|
|
779
|
-
const { language, t } = useModernI18n();
|
|
780
|
-
return (
|
|
781
|
-
<main>
|
|
782
|
-
{language}:{t('languageSwitcher')}
|
|
783
|
-
<button type="button" onClick={() => setShowRetryConsumer(true)}>
|
|
784
|
-
Retry
|
|
785
|
-
</button>
|
|
786
|
-
{showRetryConsumer && <RetryConsumer />}
|
|
787
|
-
</main>
|
|
788
|
-
);
|
|
789
|
-
};
|
|
790
|
-
const I18nRoot = wrapRoot(App);
|
|
791
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
792
|
-
|
|
793
|
-
await act(async () => {
|
|
794
|
-
router.publishPathname('/cs');
|
|
795
|
-
});
|
|
796
|
-
|
|
797
|
-
const languageError = new Error('failed to load Czech resources');
|
|
798
|
-
await act(async () => {
|
|
799
|
-
pending[0].reject(languageError);
|
|
800
|
-
await pending[0].promise.catch(() => undefined);
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
expect(rendered.container.textContent).toContain('en:Language');
|
|
804
|
-
|
|
805
|
-
await act(async () => {
|
|
806
|
-
await rstest.advanceTimersByTimeAsync(50);
|
|
807
|
-
});
|
|
808
|
-
expect(pending).toHaveLength(2);
|
|
809
|
-
|
|
810
|
-
await act(async () => {
|
|
811
|
-
pending[1].resolve();
|
|
812
|
-
await pending[1].promise;
|
|
813
|
-
});
|
|
814
|
-
|
|
815
|
-
expect(rendered.container.textContent).toContain('cs:Jazyk');
|
|
816
|
-
expect(instance.language).toBe('cs');
|
|
817
|
-
});
|
|
818
222
|
});
|
|
819
223
|
|
|
820
224
|
describe('i18n react-i18next integration', () => {
|
|
@@ -826,469 +230,105 @@ describe('i18n react-i18next integration', () => {
|
|
|
826
230
|
});
|
|
827
231
|
});
|
|
828
232
|
|
|
829
|
-
describe('
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
window.history.replaceState(null, '', '/');
|
|
837
|
-
});
|
|
838
|
-
|
|
839
|
-
test('uses the TanStack router Link for I18nLink rendering', async () => {
|
|
840
|
-
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
841
|
-
rendered = await renderWithRuntime(
|
|
842
|
-
<ModernI18nProvider
|
|
843
|
-
value={{
|
|
844
|
-
language: 'cs',
|
|
845
|
-
i18nInstance: createI18nInstance('cs'),
|
|
846
|
-
languages: ['en', 'cs'],
|
|
847
|
-
localePathRedirect: true,
|
|
848
|
-
localisedUrls,
|
|
849
|
-
}}
|
|
850
|
-
>
|
|
851
|
-
<I18nLink to="/terms-of-service" data-testid="terms-link">
|
|
852
|
-
Terms
|
|
853
|
-
</I18nLink>
|
|
854
|
-
</ModernI18nProvider>,
|
|
855
|
-
createTanstackRuntimeContext(router),
|
|
856
|
-
);
|
|
857
|
-
|
|
858
|
-
const link = rendered.container.querySelector<HTMLAnchorElement>(
|
|
859
|
-
'[data-testid="terms-link"]',
|
|
860
|
-
);
|
|
861
|
-
expect(link?.getAttribute('href')).toBe('/cs/podminky-pouzivani');
|
|
862
|
-
expect(link?.getAttribute('data-router-link')).toBe('tanstack');
|
|
863
|
-
});
|
|
864
|
-
|
|
865
|
-
test('ignores router state updates that do not change location or params', async () => {
|
|
866
|
-
const router = createMutableTanstackRouter('/en');
|
|
867
|
-
let renders = 0;
|
|
868
|
-
const LocationProbe = () => {
|
|
869
|
-
renders += 1;
|
|
870
|
-
const { location } = useI18nRouterAdapter();
|
|
871
|
-
return <output>{location?.pathname}</output>;
|
|
872
|
-
};
|
|
873
|
-
|
|
874
|
-
rendered = await renderWithRuntime(
|
|
875
|
-
<LocationProbe />,
|
|
876
|
-
createTanstackRuntimeContext(router),
|
|
877
|
-
);
|
|
878
|
-
const initialRenders = renders;
|
|
879
|
-
|
|
880
|
-
await act(async () => {
|
|
881
|
-
router.publishStateUpdate();
|
|
882
|
-
});
|
|
883
|
-
expect(renders).toBe(initialRenders);
|
|
884
|
-
expect(rendered.container.textContent).toBe('/en');
|
|
885
|
-
|
|
886
|
-
await act(async () => {
|
|
887
|
-
router.publishPathname('/cs');
|
|
888
|
-
});
|
|
889
|
-
expect(renders).toBe(initialRenders + 1);
|
|
890
|
-
expect(rendered.container.textContent).toBe('/cs');
|
|
891
|
-
});
|
|
892
|
-
|
|
893
|
-
test('ignores React Router fetcher updates while observing location changes', async () => {
|
|
894
|
-
const router = createMutableReactRouter('/en');
|
|
895
|
-
let renders = 0;
|
|
896
|
-
const LocationProbe = () => {
|
|
897
|
-
renders += 1;
|
|
898
|
-
const { location } = useI18nRouterAdapter();
|
|
899
|
-
return <output>{location?.pathname}</output>;
|
|
900
|
-
};
|
|
901
|
-
|
|
902
|
-
rendered = await renderWithRuntime(
|
|
903
|
-
<LocationProbe />,
|
|
904
|
-
createReactRouterRuntimeContext(router),
|
|
905
|
-
);
|
|
906
|
-
const initialRenders = renders;
|
|
907
|
-
|
|
908
|
-
await act(async () => {
|
|
909
|
-
router.publishFetcherUpdate();
|
|
910
|
-
});
|
|
911
|
-
expect(renders).toBe(initialRenders);
|
|
912
|
-
expect(rendered.container.textContent).toBe('/en');
|
|
913
|
-
|
|
914
|
-
await act(async () => {
|
|
915
|
-
router.publishPathname('/cs');
|
|
916
|
-
});
|
|
917
|
-
expect(renders).toBe(initialRenders + 1);
|
|
918
|
-
expect(rendered.container.textContent).toBe('/cs');
|
|
919
|
-
});
|
|
920
|
-
|
|
921
|
-
test('observes distinct route params without serialized snapshot collisions', async () => {
|
|
922
|
-
const router = createMutableReactRouter('/products', {
|
|
923
|
-
a: '1&b=2',
|
|
924
|
-
});
|
|
925
|
-
const ParamsProbe = () => {
|
|
926
|
-
const { params } = useI18nRouterAdapter();
|
|
927
|
-
return <output>{JSON.stringify(params)}</output>;
|
|
928
|
-
};
|
|
929
|
-
|
|
930
|
-
rendered = await renderWithRuntime(
|
|
931
|
-
<ParamsProbe />,
|
|
932
|
-
createReactRouterRuntimeContext(router),
|
|
933
|
-
);
|
|
934
|
-
expect(rendered.container.textContent).toBe('{"a":"1&b=2"}');
|
|
935
|
-
|
|
936
|
-
await act(async () => {
|
|
937
|
-
router.publishParams({ a: '1', b: '2' });
|
|
938
|
-
});
|
|
939
|
-
expect(rendered.container.textContent).toBe('{"a":"1","b":"2"}');
|
|
940
|
-
});
|
|
941
|
-
|
|
942
|
-
test('forwards warmup props through I18nLink with a localized string target', async () => {
|
|
943
|
-
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
944
|
-
rendered = await renderWithRuntime(
|
|
945
|
-
<ModernI18nProvider
|
|
946
|
-
value={{
|
|
947
|
-
language: 'cs',
|
|
948
|
-
i18nInstance: createI18nInstance('cs'),
|
|
949
|
-
languages: ['en', 'cs'],
|
|
950
|
-
localePathRedirect: true,
|
|
951
|
-
localisedUrls,
|
|
952
|
-
}}
|
|
953
|
-
>
|
|
954
|
-
<I18nLink
|
|
955
|
-
to="/terms-of-service"
|
|
956
|
-
data-testid="terms-link"
|
|
957
|
-
prefetch="viewport"
|
|
958
|
-
preload="intent"
|
|
959
|
-
>
|
|
960
|
-
Terms
|
|
961
|
-
</I18nLink>
|
|
962
|
-
</ModernI18nProvider>,
|
|
963
|
-
createTanstackRuntimeContext(router),
|
|
233
|
+
describe('native navigation extension contract', () => {
|
|
234
|
+
test('renders the supplied router primitive and exposes provider-scoped navigation', async () => {
|
|
235
|
+
const navigate = rstest.fn();
|
|
236
|
+
const RouterLink = ({ to, children }: any) => (
|
|
237
|
+
<a href={to} data-native="yes">
|
|
238
|
+
{children}
|
|
239
|
+
</a>
|
|
964
240
|
);
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
expect(linkProps.prefetch).toBeUndefined();
|
|
974
|
-
});
|
|
975
|
-
|
|
976
|
-
test('does not leak warmup props to fallback anchors', async () => {
|
|
977
|
-
rendered = await renderI18nRoot(
|
|
978
|
-
<ModernI18nProvider
|
|
979
|
-
value={{
|
|
980
|
-
language: 'cs',
|
|
981
|
-
i18nInstance: createI18nInstance('cs'),
|
|
982
|
-
languages: ['en', 'cs'],
|
|
983
|
-
localePathRedirect: true,
|
|
984
|
-
localisedUrls,
|
|
985
|
-
}}
|
|
986
|
-
>
|
|
987
|
-
<I18nLink
|
|
988
|
-
to="/terms-of-service"
|
|
989
|
-
data-testid="terms-link"
|
|
990
|
-
prefetch="none"
|
|
991
|
-
preload={false}
|
|
992
|
-
>
|
|
993
|
-
Terms
|
|
994
|
-
</I18nLink>
|
|
995
|
-
</ModernI18nProvider>,
|
|
996
|
-
);
|
|
997
|
-
|
|
998
|
-
const link = rendered.container.querySelector<HTMLAnchorElement>(
|
|
999
|
-
'[data-testid="terms-link"]',
|
|
1000
|
-
);
|
|
1001
|
-
expect(link?.getAttribute('href')).toBe('/cs/podminky-pouzivani');
|
|
1002
|
-
expect(link?.hasAttribute('prefetch')).toBe(false);
|
|
1003
|
-
expect(link?.hasAttribute('preload')).toBe(false);
|
|
1004
|
-
});
|
|
1005
|
-
|
|
1006
|
-
test('uses TanStack-shaped replacement when changeLanguage updates the URL', async () => {
|
|
1007
|
-
window.history.replaceState(
|
|
1008
|
-
null,
|
|
1009
|
-
'',
|
|
1010
|
-
'/en/terms-of-service?from=test#section',
|
|
1011
|
-
);
|
|
1012
|
-
|
|
1013
|
-
const router = createTanstackRouter(
|
|
1014
|
-
'/en/terms-of-service?from=test#section',
|
|
1015
|
-
);
|
|
1016
|
-
let changeLanguagePromise: Promise<void> | undefined;
|
|
1017
|
-
|
|
1018
|
-
const Harness = () => {
|
|
1019
|
-
const { changeLanguage } = useModernI18n();
|
|
1020
|
-
return (
|
|
1021
|
-
<button
|
|
1022
|
-
type="button"
|
|
1023
|
-
onClick={() => {
|
|
1024
|
-
changeLanguagePromise = changeLanguage('cs');
|
|
1025
|
-
}}
|
|
1026
|
-
>
|
|
1027
|
-
Change language
|
|
1028
|
-
</button>
|
|
1029
|
-
);
|
|
1030
|
-
};
|
|
1031
|
-
|
|
1032
|
-
rendered = await renderWithRuntime(
|
|
1033
|
-
<ModernI18nProvider
|
|
1034
|
-
value={{
|
|
1035
|
-
language: 'en',
|
|
1036
|
-
i18nInstance: createI18nInstance('en'),
|
|
1037
|
-
languages: ['en', 'cs'],
|
|
1038
|
-
localePathRedirect: true,
|
|
1039
|
-
localisedUrls,
|
|
1040
|
-
}}
|
|
1041
|
-
>
|
|
1042
|
-
<Harness />
|
|
1043
|
-
</ModernI18nProvider>,
|
|
1044
|
-
createTanstackRuntimeContext(router),
|
|
1045
|
-
);
|
|
1046
|
-
|
|
1047
|
-
const button = rendered.container.querySelector('button');
|
|
1048
|
-
|
|
1049
|
-
await act(async () => {
|
|
1050
|
-
button?.dispatchEvent(
|
|
1051
|
-
new MouseEvent('click', {
|
|
1052
|
-
bubbles: true,
|
|
1053
|
-
cancelable: true,
|
|
1054
|
-
button: 0,
|
|
1055
|
-
}),
|
|
1056
|
-
);
|
|
1057
|
-
await changeLanguagePromise;
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1060
|
-
expect(router.navigate).toHaveBeenCalledWith({
|
|
1061
|
-
to: '/cs/podminky-pouzivani?from=test#section',
|
|
1062
|
-
replace: true,
|
|
1063
|
-
});
|
|
1064
|
-
});
|
|
1065
|
-
|
|
1066
|
-
test('updates provider language when target locale is already in URL', async () => {
|
|
1067
|
-
window.history.replaceState(null, '', '/cs/podminky-pouzivani');
|
|
1068
|
-
|
|
1069
|
-
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
1070
|
-
const i18nInstance = createI18nInstance('en');
|
|
1071
|
-
const updateLanguage = rstest.fn();
|
|
1072
|
-
let changeLanguagePromise: Promise<void> | undefined;
|
|
1073
|
-
|
|
1074
|
-
const Harness = () => {
|
|
1075
|
-
const { changeLanguage } = useModernI18n();
|
|
1076
|
-
return (
|
|
1077
|
-
<button
|
|
1078
|
-
type="button"
|
|
1079
|
-
onClick={() => {
|
|
1080
|
-
changeLanguagePromise = changeLanguage('cs');
|
|
1081
|
-
}}
|
|
1082
|
-
>
|
|
1083
|
-
Change language
|
|
1084
|
-
</button>
|
|
1085
|
-
);
|
|
1086
|
-
};
|
|
1087
|
-
|
|
1088
|
-
rendered = await renderWithRuntime(
|
|
1089
|
-
<ModernI18nProvider
|
|
1090
|
-
value={{
|
|
1091
|
-
language: 'en',
|
|
1092
|
-
i18nInstance,
|
|
1093
|
-
languages: ['en', 'cs'],
|
|
1094
|
-
localePathRedirect: true,
|
|
1095
|
-
localisedUrls,
|
|
1096
|
-
updateLanguage,
|
|
1097
|
-
}}
|
|
1098
|
-
>
|
|
1099
|
-
<Harness />
|
|
1100
|
-
</ModernI18nProvider>,
|
|
1101
|
-
createTanstackRuntimeContext(router),
|
|
1102
|
-
);
|
|
1103
|
-
|
|
1104
|
-
const button = rendered.container.querySelector('button');
|
|
1105
|
-
updateLanguage.mockClear();
|
|
1106
|
-
|
|
1107
|
-
await act(async () => {
|
|
1108
|
-
button?.dispatchEvent(
|
|
1109
|
-
new MouseEvent('click', {
|
|
1110
|
-
bubbles: true,
|
|
1111
|
-
cancelable: true,
|
|
1112
|
-
button: 0,
|
|
1113
|
-
}),
|
|
1114
|
-
);
|
|
1115
|
-
await changeLanguagePromise;
|
|
1116
|
-
});
|
|
1117
|
-
|
|
1118
|
-
expect(updateLanguage).toHaveBeenCalledWith('cs');
|
|
1119
|
-
expect(router.navigate).not.toHaveBeenCalled();
|
|
1120
|
-
});
|
|
1121
|
-
|
|
1122
|
-
test('keeps React Router positional replacement when changeLanguage updates the URL', async () => {
|
|
1123
|
-
window.history.replaceState(null, '', '/en/terms-of-service');
|
|
1124
|
-
|
|
1125
|
-
const router = {
|
|
1126
|
-
navigate: rstest.fn(async () => undefined),
|
|
1127
|
-
};
|
|
1128
|
-
let changeLanguagePromise: Promise<void> | undefined;
|
|
1129
|
-
|
|
1130
|
-
const Harness = () => {
|
|
1131
|
-
const { changeLanguage } = useModernI18n();
|
|
1132
|
-
return (
|
|
1133
|
-
<button
|
|
1134
|
-
type="button"
|
|
1135
|
-
onClick={() => {
|
|
1136
|
-
changeLanguagePromise = changeLanguage('cs');
|
|
1137
|
-
}}
|
|
1138
|
-
>
|
|
1139
|
-
Change language
|
|
1140
|
-
</button>
|
|
1141
|
-
);
|
|
1142
|
-
};
|
|
1143
|
-
|
|
1144
|
-
rendered = await renderWithRuntime(
|
|
1145
|
-
<ModernI18nProvider
|
|
241
|
+
function Consumer() {
|
|
242
|
+
const adapter = useI18nRouterAdapter();
|
|
243
|
+
expect(adapter.navigate).toBe(navigate);
|
|
244
|
+
expect(adapter.params).toEqual({ lang: 'cs' });
|
|
245
|
+
return <Link to="/products?tag=x#detail">Products</Link>;
|
|
246
|
+
}
|
|
247
|
+
const rendered = await renderI18nRoot(
|
|
248
|
+
<I18nNavigationProvider
|
|
1146
249
|
value={{
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
250
|
+
hasRouter: true,
|
|
251
|
+
navigate,
|
|
252
|
+
params: { lang: 'cs' },
|
|
253
|
+
location: { pathname: '/cs/products', search: '', hash: '' },
|
|
254
|
+
Link: RouterLink,
|
|
1152
255
|
}}
|
|
1153
256
|
>
|
|
1154
|
-
<Harness />
|
|
1155
|
-
</ModernI18nProvider>,
|
|
1156
|
-
createReactRouterRuntimeContext(router),
|
|
1157
|
-
);
|
|
1158
|
-
|
|
1159
|
-
const button = rendered.container.querySelector('button');
|
|
1160
|
-
|
|
1161
|
-
await act(async () => {
|
|
1162
|
-
button?.dispatchEvent(
|
|
1163
|
-
new MouseEvent('click', {
|
|
1164
|
-
bubbles: true,
|
|
1165
|
-
cancelable: true,
|
|
1166
|
-
button: 0,
|
|
1167
|
-
}),
|
|
1168
|
-
);
|
|
1169
|
-
await changeLanguagePromise;
|
|
1170
|
-
});
|
|
1171
|
-
|
|
1172
|
-
expect(router.navigate).toHaveBeenCalledWith('/cs/podminky-pouzivani', {
|
|
1173
|
-
replace: true,
|
|
1174
|
-
});
|
|
1175
|
-
});
|
|
1176
|
-
|
|
1177
|
-
test('tracks React Router link and history navigation without Modern i18n consumers', async () => {
|
|
1178
|
-
window.history.replaceState(null, '', '/en');
|
|
1179
|
-
const i18nInstance = i18next.createInstance();
|
|
1180
|
-
await i18nInstance.init({
|
|
1181
|
-
lng: 'en',
|
|
1182
|
-
fallbackLng: 'en',
|
|
1183
|
-
resources: {
|
|
1184
|
-
en: { translation: { languageSwitcher: 'Language' } },
|
|
1185
|
-
cs: { translation: { languageSwitcher: 'Jazyk' } },
|
|
1186
|
-
},
|
|
1187
|
-
});
|
|
1188
|
-
|
|
1189
|
-
const RouteContent = () => {
|
|
1190
|
-
const { t } = useTranslation();
|
|
1191
|
-
return (
|
|
1192
|
-
<main>
|
|
1193
|
-
<span data-testid="translation">{t('languageSwitcher')}</span>
|
|
1194
|
-
<ReactRouterLink to="/cs">Czech</ReactRouterLink>
|
|
1195
|
-
</main>
|
|
1196
|
-
);
|
|
1197
|
-
};
|
|
1198
|
-
const router = createMemoryRouter(
|
|
1199
|
-
[{ path: '/:lang', element: <RouteContent /> }],
|
|
1200
|
-
{ initialEntries: ['/en'] },
|
|
1201
|
-
);
|
|
1202
|
-
const originalSubscribe = router.subscribe;
|
|
1203
|
-
router.subscribe = function subscribe(listener) {
|
|
1204
|
-
if (this !== router) {
|
|
1205
|
-
throw new Error('React Router subscribe receiver was not preserved');
|
|
1206
|
-
}
|
|
1207
|
-
return originalSubscribe.call(this, listener);
|
|
1208
|
-
};
|
|
1209
|
-
const runtimeContext = createReactRouterRuntimeContext(router);
|
|
1210
|
-
const { onBeforeRender, wrapRoot } = await collectI18nRuntime(
|
|
1211
|
-
i18nInstance,
|
|
1212
|
-
true,
|
|
1213
|
-
);
|
|
1214
|
-
await onBeforeRender(runtimeContext);
|
|
1215
|
-
const I18nRoot = wrapRoot(() => <RouterProvider router={router} />);
|
|
1216
|
-
rendered = await renderWithRuntime(<I18nRoot />, runtimeContext);
|
|
1217
|
-
|
|
1218
|
-
await act(async () => {
|
|
1219
|
-
rendered?.container
|
|
1220
|
-
.querySelector('a')
|
|
1221
|
-
?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
|
|
1222
|
-
await Promise.resolve();
|
|
1223
|
-
});
|
|
1224
|
-
expect(
|
|
1225
|
-
rendered.container.querySelector('[data-testid="translation"]')
|
|
1226
|
-
?.textContent,
|
|
1227
|
-
).toBe('Jazyk');
|
|
1228
|
-
|
|
1229
|
-
await act(async () => {
|
|
1230
|
-
await router.navigate(-1);
|
|
1231
|
-
});
|
|
1232
|
-
expect(
|
|
1233
|
-
rendered.container.querySelector('[data-testid="translation"]')
|
|
1234
|
-
?.textContent,
|
|
1235
|
-
).toBe('Language');
|
|
1236
|
-
});
|
|
1237
|
-
|
|
1238
|
-
test('exposes a language-scoped t function for rendered copy', async () => {
|
|
1239
|
-
const i18nInstance = createI18nInstance('en');
|
|
1240
|
-
i18nInstance.t = (key: string) => `${i18nInstance.language}:${key}`;
|
|
1241
|
-
const renderTranslations: Array<(key: string) => string> = [];
|
|
1242
|
-
let setProviderLanguage: ((language: string) => void) | undefined;
|
|
1243
|
-
|
|
1244
|
-
const StatefulI18nProvider = ({ children }: PropsWithChildren) => {
|
|
1245
|
-
const [language, setLanguage] = useState('en');
|
|
1246
|
-
setProviderLanguage = setLanguage;
|
|
1247
|
-
|
|
1248
|
-
return (
|
|
1249
257
|
<ModernI18nProvider
|
|
1250
258
|
value={{
|
|
1251
|
-
language,
|
|
1252
|
-
i18nInstance,
|
|
259
|
+
language: 'cs',
|
|
260
|
+
i18nInstance: createI18nInstance('cs'),
|
|
1253
261
|
languages: ['en', 'cs'],
|
|
1254
262
|
localePathRedirect: true,
|
|
1255
|
-
localisedUrls,
|
|
1256
|
-
updateLanguage: setLanguage,
|
|
1257
263
|
}}
|
|
1258
264
|
>
|
|
1259
|
-
|
|
265
|
+
<Consumer />
|
|
1260
266
|
</ModernI18nProvider>
|
|
267
|
+
</I18nNavigationProvider>,
|
|
268
|
+
);
|
|
269
|
+
try {
|
|
270
|
+
expect(rendered.container.querySelector('a')?.getAttribute('href')).toBe(
|
|
271
|
+
'/cs/products?tag=x#detail',
|
|
1261
272
|
);
|
|
1262
|
-
|
|
273
|
+
expect(
|
|
274
|
+
rendered.container.querySelector('a')?.getAttribute('data-native'),
|
|
275
|
+
).toBe('yes');
|
|
276
|
+
expect(navigate).not.toHaveBeenCalled();
|
|
277
|
+
} finally {
|
|
278
|
+
cleanup(rendered);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
1263
282
|
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
283
|
+
describe('native synchronization extension contract', () => {
|
|
284
|
+
test('uses the supplied component callback and preserves its state across parent renders', async () => {
|
|
285
|
+
const instance = createI18nInstance('en');
|
|
286
|
+
const synchronize = rstest.fn();
|
|
287
|
+
const mounts = rstest.fn();
|
|
288
|
+
const observed: I18nLanguageSynchronizationProps[] = [];
|
|
289
|
+
const Synchronization = (props: I18nLanguageSynchronizationProps) => {
|
|
290
|
+
observed.push(props);
|
|
291
|
+
useEffect(() => {
|
|
292
|
+
mounts();
|
|
293
|
+
props.setLang('cs');
|
|
294
|
+
}, []);
|
|
295
|
+
return props.children(synchronize);
|
|
1268
296
|
};
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
).toBe('en:key');
|
|
1281
|
-
const initialT = renderTranslations.at(-1);
|
|
1282
|
-
|
|
1283
|
-
await act(async () => {
|
|
1284
|
-
i18nInstance.language = 'cs';
|
|
1285
|
-
setProviderLanguage?.('cs');
|
|
297
|
+
const Root = createI18nRootWrapper({
|
|
298
|
+
htmlLangAttr: false,
|
|
299
|
+
localePathRedirect: true,
|
|
300
|
+
languages: ['en', 'cs'],
|
|
301
|
+
fallbackLanguage: 'en',
|
|
302
|
+
getLatestI18nInstance: () => instance,
|
|
303
|
+
getI18nextProvider: () => null,
|
|
304
|
+
LanguageSynchronization: Synchronization,
|
|
305
|
+
})(() => {
|
|
306
|
+
const { language } = useModernI18n();
|
|
307
|
+
return <main>{language}</main>;
|
|
1286
308
|
});
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
309
|
+
window.history.replaceState(null, '', '/cs/products');
|
|
310
|
+
const rendered = await renderI18nRoot(<Root />);
|
|
311
|
+
try {
|
|
312
|
+
expect(rendered.container.textContent).toBe('cs');
|
|
313
|
+
expect(synchronize).toHaveBeenCalledWith('cs');
|
|
314
|
+
expect(observed.at(-1)?.i18nInstance).toBe(instance);
|
|
315
|
+
expect(observed.at(-1)?.pathname).toBe('/cs/products');
|
|
316
|
+
expect(observed.at(-1)?.prevLangRef.current).toBe('cs');
|
|
317
|
+
await act(async () => {
|
|
318
|
+
rendered.root.render(
|
|
319
|
+
<RuntimeContext.Provider
|
|
320
|
+
value={{ isBrowser: true, requestContext, context: requestContext }}
|
|
321
|
+
>
|
|
322
|
+
<Root unrelated="updated" />
|
|
323
|
+
</RuntimeContext.Provider>,
|
|
324
|
+
);
|
|
325
|
+
});
|
|
326
|
+
expect(mounts).toHaveBeenCalledTimes(1);
|
|
327
|
+
expect(rendered.container.textContent).toBe('cs');
|
|
328
|
+
expect(instance.changeLanguage).not.toHaveBeenCalled();
|
|
329
|
+
} finally {
|
|
330
|
+
cleanup(rendered);
|
|
331
|
+
window.history.replaceState(null, '', '/');
|
|
332
|
+
}
|
|
1293
333
|
});
|
|
1294
334
|
});
|