@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.98 → 3.4.0-ultramodern.0
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/README.md +221 -11
- package/dist/cjs/cli/index.js +17 -64
- package/dist/cjs/cli/locales.js +132 -0
- package/dist/cjs/runtime/I18nLink.js +17 -20
- package/dist/cjs/runtime/Link.js +264 -0
- package/dist/cjs/runtime/canonicalRoutes.js +18 -0
- package/dist/cjs/runtime/context.js +9 -5
- package/dist/cjs/runtime/hooks.js +9 -5
- package/dist/cjs/runtime/i18n/backend/config.js +9 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +20 -11
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +79 -10
- package/dist/cjs/runtime/i18n/backend/index.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +9 -5
- package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
- package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
- package/dist/cjs/runtime/i18n/detection/config.js +9 -5
- package/dist/cjs/runtime/i18n/detection/index.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
- package/dist/cjs/runtime/i18n/index.js +9 -5
- package/dist/cjs/runtime/i18n/instance.js +17 -13
- package/dist/cjs/runtime/i18n/react-i18next.js +12 -8
- package/dist/cjs/runtime/i18n/utils.js +9 -5
- package/dist/cjs/runtime/index.js +32 -5
- package/dist/cjs/runtime/localizedPaths.js +102 -0
- package/dist/cjs/runtime/routerAdapter.js +11 -7
- package/dist/cjs/runtime/utils.js +31 -17
- package/dist/cjs/server/index.js +10 -14
- package/dist/cjs/shared/deepMerge.js +12 -8
- package/dist/cjs/shared/detection.js +9 -5
- package/dist/cjs/shared/localisedUrls.js +148 -34
- package/dist/cjs/shared/utils.js +15 -11
- package/dist/esm/cli/index.mjs +8 -48
- package/dist/esm/cli/locales.mjs +80 -0
- package/dist/esm/runtime/I18nLink.mjs +7 -14
- package/dist/esm/runtime/Link.mjs +221 -0
- package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
- package/dist/esm/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +56 -5
- package/dist/esm/runtime/index.mjs +4 -2
- package/dist/esm/runtime/localizedPaths.mjs +55 -0
- package/dist/esm/runtime/routerAdapter.mjs +3 -3
- package/dist/esm/runtime/utils.mjs +19 -12
- package/dist/esm/server/index.mjs +2 -10
- package/dist/esm/shared/localisedUrls.mjs +115 -23
- package/dist/esm-node/cli/index.mjs +8 -48
- package/dist/esm-node/cli/locales.mjs +81 -0
- package/dist/esm-node/runtime/I18nLink.mjs +7 -14
- package/dist/esm-node/runtime/Link.mjs +222 -0
- package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +56 -5
- package/dist/esm-node/runtime/index.mjs +4 -2
- package/dist/esm-node/runtime/localizedPaths.mjs +56 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +3 -3
- package/dist/esm-node/runtime/utils.mjs +19 -12
- package/dist/esm-node/server/index.mjs +2 -10
- package/dist/esm-node/shared/localisedUrls.mjs +115 -23
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/cli/locales.d.ts +17 -0
- package/dist/types/runtime/I18nLink.d.ts +4 -13
- package/dist/types/runtime/Link.d.ts +66 -0
- package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
- package/dist/types/runtime/i18n/backend/defaults.d.ts +10 -7
- package/dist/types/runtime/i18n/backend/defaults.node.d.ts +13 -4
- package/dist/types/runtime/index.d.ts +5 -1
- package/dist/types/runtime/localizedPaths.d.ts +39 -0
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +13 -4
- package/dist/types/shared/localisedUrls.d.ts +23 -0
- package/dist/types/shared/type.d.ts +27 -5
- package/package.json +28 -25
- package/rstest.config.mts +7 -2
- package/src/cli/index.ts +25 -98
- package/src/cli/locales.ts +186 -0
- package/src/runtime/I18nLink.tsx +13 -44
- package/src/runtime/Link.tsx +430 -0
- package/src/runtime/canonicalRoutes.ts +93 -0
- package/src/runtime/i18n/backend/defaults.node.ts +112 -7
- package/src/runtime/i18n/backend/defaults.ts +20 -18
- package/src/runtime/index.tsx +24 -2
- package/src/runtime/localizedPaths.ts +107 -0
- package/src/runtime/routerAdapter.tsx +4 -5
- package/src/runtime/types.ts +1 -1
- package/src/runtime/utils.ts +33 -26
- package/src/server/index.ts +7 -17
- package/src/shared/localisedUrls.ts +256 -26
- package/src/shared/type.ts +27 -5
- package/tests/backendDefaults.test.ts +51 -0
- package/tests/i18nUtils.test.ts +10 -3
- package/tests/link.test.tsx +525 -0
- package/tests/linkTypes.test.ts +28 -0
- package/tests/localisedUrls.test.ts +224 -0
- package/tests/routerAdapter.test.tsx +86 -12
- package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
- package/tests/type-fixture/tsconfig.json +15 -0
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
import { InternalRuntimeContext } from '@modern-js/runtime/context';
|
|
2
|
+
import type React from 'react';
|
|
3
|
+
import { act } from 'react';
|
|
4
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
5
|
+
import { ModernI18nProvider } from '../src/runtime/context';
|
|
6
|
+
import type { I18nInstance } from '../src/runtime/i18n';
|
|
7
|
+
import { interpolateRouteParams, Link } from '../src/runtime/Link';
|
|
8
|
+
import {
|
|
9
|
+
canonicalPath,
|
|
10
|
+
localizePath,
|
|
11
|
+
useLocalizedLocation,
|
|
12
|
+
} from '../src/runtime/localizedPaths';
|
|
13
|
+
import { buildLocalizedUrl, splitUrlTarget } from '../src/runtime/utils';
|
|
14
|
+
|
|
15
|
+
(
|
|
16
|
+
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
|
|
17
|
+
).IS_REACT_ACT_ENVIRONMENT = true;
|
|
18
|
+
|
|
19
|
+
const localisedUrls = {
|
|
20
|
+
'/terms-of-service': {
|
|
21
|
+
en: '/terms-of-service',
|
|
22
|
+
cs: '/podminky-pouzivani',
|
|
23
|
+
},
|
|
24
|
+
'/products': {
|
|
25
|
+
en: '/products',
|
|
26
|
+
cs: '/produkty',
|
|
27
|
+
},
|
|
28
|
+
'/products/:slug': {
|
|
29
|
+
en: '/products/:slug',
|
|
30
|
+
cs: '/produkty/:slug',
|
|
31
|
+
},
|
|
32
|
+
// Canonical key that matches no language pattern.
|
|
33
|
+
'/talks/:slug': {
|
|
34
|
+
en: '/lectures/:slug',
|
|
35
|
+
cs: '/prednasky/:slug',
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const languages = ['en', 'cs'];
|
|
40
|
+
const pathsConfig = { languages, localisedUrls };
|
|
41
|
+
|
|
42
|
+
const requestContext = {
|
|
43
|
+
request: {},
|
|
44
|
+
response: {},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const capturedLinkProps: any[] = [];
|
|
48
|
+
|
|
49
|
+
// Mirrors the real TanStack Link contract: it consumes its own props
|
|
50
|
+
// (`preload`, `search`, `hash`, ...) and spreads everything else onto the
|
|
51
|
+
// anchor. Deliberately does NOT strip `prefetch` — TanStack has no such prop,
|
|
52
|
+
// so a forwarded `prefetch` would leak into the DOM and fail assertions.
|
|
53
|
+
const TanstackLink = ({ to, children, ...props }: any) => {
|
|
54
|
+
capturedLinkProps.push({ to, ...props });
|
|
55
|
+
const {
|
|
56
|
+
preload: _preload,
|
|
57
|
+
search: _search,
|
|
58
|
+
hash: _hash,
|
|
59
|
+
hashScrollIntoView: _hashScrollIntoView,
|
|
60
|
+
replace: _replace,
|
|
61
|
+
...anchorProps
|
|
62
|
+
} = props;
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<a href={to} data-router-link="tanstack" {...anchorProps}>
|
|
66
|
+
{children}
|
|
67
|
+
</a>
|
|
68
|
+
);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
function createI18nInstance(language = 'en'): I18nInstance {
|
|
72
|
+
return {
|
|
73
|
+
language,
|
|
74
|
+
isInitialized: true,
|
|
75
|
+
init: () => Promise.resolve(undefined),
|
|
76
|
+
use: () => {},
|
|
77
|
+
createInstance: () => createI18nInstance(language),
|
|
78
|
+
services: {},
|
|
79
|
+
options: {},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function createTanstackRouter(target = '/en/terms-of-service', lang = 'en') {
|
|
84
|
+
const url = new URL(target, 'https://modernjs.test');
|
|
85
|
+
|
|
86
|
+
return {
|
|
87
|
+
navigate: rstest.fn(async () => undefined),
|
|
88
|
+
state: {
|
|
89
|
+
location: {
|
|
90
|
+
pathname: url.pathname,
|
|
91
|
+
searchStr: url.search,
|
|
92
|
+
hash: url.hash,
|
|
93
|
+
},
|
|
94
|
+
matches: [{ params: { lang } }],
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function createTanstackRuntimeContext(router: unknown) {
|
|
100
|
+
return {
|
|
101
|
+
isBrowser: true,
|
|
102
|
+
requestContext,
|
|
103
|
+
context: requestContext,
|
|
104
|
+
routerFramework: 'tanstack',
|
|
105
|
+
routerInstance: router,
|
|
106
|
+
routerRuntime: {
|
|
107
|
+
framework: 'tanstack',
|
|
108
|
+
instance: router,
|
|
109
|
+
},
|
|
110
|
+
router: {
|
|
111
|
+
Link: TanstackLink,
|
|
112
|
+
useRouter: () => router,
|
|
113
|
+
},
|
|
114
|
+
} as any;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function providerValue(language: string) {
|
|
118
|
+
return {
|
|
119
|
+
language,
|
|
120
|
+
i18nInstance: createI18nInstance(language),
|
|
121
|
+
languages,
|
|
122
|
+
localePathRedirect: true,
|
|
123
|
+
localisedUrls,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function renderWithRuntime(node: React.ReactNode, runtimeContext: any) {
|
|
128
|
+
const container = document.createElement('div');
|
|
129
|
+
document.body.appendChild(container);
|
|
130
|
+
const root = createRoot(container);
|
|
131
|
+
|
|
132
|
+
await act(async () => {
|
|
133
|
+
root.render(
|
|
134
|
+
<InternalRuntimeContext.Provider value={runtimeContext}>
|
|
135
|
+
{node}
|
|
136
|
+
</InternalRuntimeContext.Provider>,
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
return { container, root };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function cleanup(rendered?: { container: HTMLElement; root: Root }) {
|
|
144
|
+
if (!rendered) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
act(() => {
|
|
148
|
+
rendered.root.unmount();
|
|
149
|
+
});
|
|
150
|
+
rendered.container.remove();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
describe('splitUrlTarget', () => {
|
|
154
|
+
test('splits pathname, search and hash', () => {
|
|
155
|
+
expect(splitUrlTarget('/talks?tag=x#abstract')).toEqual({
|
|
156
|
+
pathname: '/talks',
|
|
157
|
+
search: '?tag=x',
|
|
158
|
+
hash: '#abstract',
|
|
159
|
+
});
|
|
160
|
+
expect(splitUrlTarget('/#work-with-me')).toEqual({
|
|
161
|
+
pathname: '/',
|
|
162
|
+
search: '',
|
|
163
|
+
hash: '#work-with-me',
|
|
164
|
+
});
|
|
165
|
+
expect(splitUrlTarget('/talks')).toEqual({
|
|
166
|
+
pathname: '/talks',
|
|
167
|
+
search: '',
|
|
168
|
+
hash: '#'.slice(1) === '' ? '' : '',
|
|
169
|
+
});
|
|
170
|
+
expect(splitUrlTarget('?q=1#x')).toEqual({
|
|
171
|
+
pathname: '',
|
|
172
|
+
search: '?q=1',
|
|
173
|
+
hash: '#x',
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
describe('buildLocalizedUrl suffix handling', () => {
|
|
179
|
+
test('hash-only target keeps the hash and drops the trailing slash', () => {
|
|
180
|
+
expect(buildLocalizedUrl('/#work-with-me', 'en', languages)).toBe(
|
|
181
|
+
'/en#work-with-me',
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('query-only target keeps the query', () => {
|
|
186
|
+
expect(buildLocalizedUrl('/products?tag=x', 'en', languages)).toBe(
|
|
187
|
+
'/en/products?tag=x',
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('query and hash survive localized slug mapping', () => {
|
|
192
|
+
expect(
|
|
193
|
+
buildLocalizedUrl(
|
|
194
|
+
'/products/bota?tag=x#detail',
|
|
195
|
+
'cs',
|
|
196
|
+
languages,
|
|
197
|
+
localisedUrls,
|
|
198
|
+
),
|
|
199
|
+
).toBe('/cs/produkty/bota?tag=x#detail');
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test('root path', () => {
|
|
203
|
+
expect(buildLocalizedUrl('/', 'cs', languages, localisedUrls)).toBe('/cs');
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test('localizes canonical keys that match no language pattern', () => {
|
|
207
|
+
expect(
|
|
208
|
+
buildLocalizedUrl(
|
|
209
|
+
'/talks/ai-slop#abstract',
|
|
210
|
+
'cs',
|
|
211
|
+
languages,
|
|
212
|
+
localisedUrls,
|
|
213
|
+
),
|
|
214
|
+
).toBe('/cs/prednasky/ai-slop#abstract');
|
|
215
|
+
expect(
|
|
216
|
+
buildLocalizedUrl('/talks/ai-slop', 'en', languages, localisedUrls),
|
|
217
|
+
).toBe('/en/lectures/ai-slop');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
test('re-localizes already-localized paths', () => {
|
|
221
|
+
expect(
|
|
222
|
+
buildLocalizedUrl('/cs/produkty/bota#x', 'en', languages, localisedUrls),
|
|
223
|
+
).toBe('/en/products/bota#x');
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe('interpolateRouteParams', () => {
|
|
228
|
+
test('interpolates $param and :param segments', () => {
|
|
229
|
+
expect(interpolateRouteParams('/talks/$slug', { slug: 'ai slop' })).toBe(
|
|
230
|
+
'/talks/ai%20slop',
|
|
231
|
+
);
|
|
232
|
+
expect(interpolateRouteParams('/talks/:slug', { slug: 'x' })).toBe(
|
|
233
|
+
'/talks/x',
|
|
234
|
+
);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('drops missing optional segments', () => {
|
|
238
|
+
expect(interpolateRouteParams('/opt/{-$slug}', {})).toBe('/opt');
|
|
239
|
+
expect(interpolateRouteParams('/opt/:slug?', {})).toBe('/opt');
|
|
240
|
+
expect(interpolateRouteParams('/opt/{-$slug}', { slug: 'v' })).toBe(
|
|
241
|
+
'/opt/v',
|
|
242
|
+
);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
test('expands splat params', () => {
|
|
246
|
+
expect(interpolateRouteParams('/files/$', { _splat: 'a/b' })).toBe(
|
|
247
|
+
'/files/a/b',
|
|
248
|
+
);
|
|
249
|
+
expect(interpolateRouteParams('/files/*', { '*': 'a' })).toBe('/files/a');
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('localization utilities', () => {
|
|
254
|
+
test('localizePath maps canonical paths per language', () => {
|
|
255
|
+
expect(localizePath('/products/bota', 'cs', pathsConfig)).toBe(
|
|
256
|
+
'/cs/produkty/bota',
|
|
257
|
+
);
|
|
258
|
+
expect(localizePath('/talks/x', 'en', pathsConfig)).toBe('/en/lectures/x');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test('canonicalPath strips language and reverse-maps localized slugs', () => {
|
|
262
|
+
expect(canonicalPath('/cs/produkty/bota', pathsConfig)).toBe(
|
|
263
|
+
'/products/bota',
|
|
264
|
+
);
|
|
265
|
+
expect(canonicalPath('/en/lectures/x?q=1#h', pathsConfig)).toBe(
|
|
266
|
+
'/talks/x?q=1#h',
|
|
267
|
+
);
|
|
268
|
+
expect(canonicalPath('/cs', pathsConfig)).toBe('/');
|
|
269
|
+
expect(canonicalPath('/en/products', pathsConfig)).toBe('/products');
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe('framework Link', () => {
|
|
274
|
+
let rendered: { container: HTMLElement; root: Root } | undefined;
|
|
275
|
+
|
|
276
|
+
afterEach(() => {
|
|
277
|
+
cleanup(rendered);
|
|
278
|
+
rendered = undefined;
|
|
279
|
+
capturedLinkProps.length = 0;
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test('localizes canonical paths through the TanStack Link', async () => {
|
|
283
|
+
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
284
|
+
rendered = await renderWithRuntime(
|
|
285
|
+
<ModernI18nProvider value={providerValue('cs')}>
|
|
286
|
+
<Link to="/products/$slug" params={{ slug: 'bota' }} data-testid="p">
|
|
287
|
+
Product
|
|
288
|
+
</Link>
|
|
289
|
+
</ModernI18nProvider>,
|
|
290
|
+
createTanstackRuntimeContext(router),
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
const link = rendered.container.querySelector('[data-testid="p"]');
|
|
294
|
+
expect(link?.getAttribute('href')).toBe('/cs/produkty/bota');
|
|
295
|
+
expect(link?.getAttribute('data-router-link')).toBe('tanstack');
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test('passes hash natively for cross-page hash targets', async () => {
|
|
299
|
+
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
300
|
+
rendered = await renderWithRuntime(
|
|
301
|
+
<ModernI18nProvider value={providerValue('cs')}>
|
|
302
|
+
<Link to="/#work-with-me" data-testid="cta">
|
|
303
|
+
CTA
|
|
304
|
+
</Link>
|
|
305
|
+
</ModernI18nProvider>,
|
|
306
|
+
createTanstackRuntimeContext(router),
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
const props = capturedLinkProps.at(-1);
|
|
310
|
+
expect(props.to).toBe('/cs');
|
|
311
|
+
expect(props.hash).toBe('work-with-me');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('passes query and hash from the target natively', async () => {
|
|
315
|
+
const router = createTanstackRouter('/en/products', 'en');
|
|
316
|
+
rendered = await renderWithRuntime(
|
|
317
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
318
|
+
<Link to="/products?tag=x#list" data-testid="q">
|
|
319
|
+
Products
|
|
320
|
+
</Link>
|
|
321
|
+
</ModernI18nProvider>,
|
|
322
|
+
createTanstackRuntimeContext(router),
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
const props = capturedLinkProps.at(-1);
|
|
326
|
+
expect(props.to).toBe('/en/products');
|
|
327
|
+
expect(props.search).toEqual({ tag: 'x' });
|
|
328
|
+
expect(props.hash).toBe('list');
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('renders a plain anchor for external targets', async () => {
|
|
332
|
+
const router = createTanstackRouter('/en', 'en');
|
|
333
|
+
rendered = await renderWithRuntime(
|
|
334
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
335
|
+
<Link to="https://ai.bleeding.dev" data-testid="ext" prefetch="none">
|
|
336
|
+
AI
|
|
337
|
+
</Link>
|
|
338
|
+
</ModernI18nProvider>,
|
|
339
|
+
createTanstackRuntimeContext(router),
|
|
340
|
+
);
|
|
341
|
+
|
|
342
|
+
const link = rendered.container.querySelector('[data-testid="ext"]');
|
|
343
|
+
expect(link?.getAttribute('href')).toBe('https://ai.bleeding.dev');
|
|
344
|
+
expect(link?.getAttribute('data-router-link')).toBeNull();
|
|
345
|
+
expect(link?.hasAttribute('prefetch')).toBe(false);
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
test('renders a plain anchor for same-page hash targets', async () => {
|
|
349
|
+
const router = createTanstackRouter('/en', 'en');
|
|
350
|
+
rendered = await renderWithRuntime(
|
|
351
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
352
|
+
<Link to="#work-with-me" data-testid="anchor">
|
|
353
|
+
Jump
|
|
354
|
+
</Link>
|
|
355
|
+
</ModernI18nProvider>,
|
|
356
|
+
createTanstackRuntimeContext(router),
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
const link = rendered.container.querySelector('[data-testid="anchor"]');
|
|
360
|
+
expect(link?.getAttribute('href')).toBe('#work-with-me');
|
|
361
|
+
expect(link?.getAttribute('data-router-link')).toBeNull();
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
test('falls back to a localized anchor without a router', async () => {
|
|
365
|
+
rendered = await renderWithRuntime(
|
|
366
|
+
<ModernI18nProvider value={providerValue('cs')}>
|
|
367
|
+
<Link
|
|
368
|
+
to="/products/$slug?tag=x#detail"
|
|
369
|
+
params={{ slug: 'bota' }}
|
|
370
|
+
data-testid="f"
|
|
371
|
+
prefetch="viewport"
|
|
372
|
+
>
|
|
373
|
+
Product
|
|
374
|
+
</Link>
|
|
375
|
+
</ModernI18nProvider>,
|
|
376
|
+
{ isBrowser: true, requestContext, context: requestContext } as any,
|
|
377
|
+
);
|
|
378
|
+
|
|
379
|
+
const link = rendered.container.querySelector('[data-testid="f"]');
|
|
380
|
+
expect(link?.getAttribute('href')).toBe('/cs/produkty/bota?tag=x#detail');
|
|
381
|
+
expect(link?.hasAttribute('prefetch')).toBe(false);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
test('maps prefetch to the TanStack preload prop', async () => {
|
|
385
|
+
const router = createTanstackRouter('/en/products', 'en');
|
|
386
|
+
rendered = await renderWithRuntime(
|
|
387
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
388
|
+
<Link to="/products" data-testid="pf" prefetch="intent">
|
|
389
|
+
Products
|
|
390
|
+
</Link>
|
|
391
|
+
</ModernI18nProvider>,
|
|
392
|
+
createTanstackRuntimeContext(router),
|
|
393
|
+
);
|
|
394
|
+
|
|
395
|
+
const props = capturedLinkProps.at(-1);
|
|
396
|
+
expect(props.preload).toBe('intent');
|
|
397
|
+
expect(props.prefetch).toBeUndefined();
|
|
398
|
+
|
|
399
|
+
const link = rendered.container.querySelector('[data-testid="pf"]');
|
|
400
|
+
expect(link?.hasAttribute('prefetch')).toBe(false);
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
test('maps prefetch="none" to preload={false}; explicit preload wins', async () => {
|
|
404
|
+
const router = createTanstackRouter('/en/products', 'en');
|
|
405
|
+
rendered = await renderWithRuntime(
|
|
406
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
407
|
+
<Link to="/products" data-testid="none" prefetch="none">
|
|
408
|
+
Products
|
|
409
|
+
</Link>
|
|
410
|
+
<Link
|
|
411
|
+
to="/products"
|
|
412
|
+
data-testid="explicit"
|
|
413
|
+
prefetch="intent"
|
|
414
|
+
preload="viewport"
|
|
415
|
+
>
|
|
416
|
+
Products
|
|
417
|
+
</Link>
|
|
418
|
+
</ModernI18nProvider>,
|
|
419
|
+
createTanstackRuntimeContext(router),
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
const noneProps = capturedLinkProps[capturedLinkProps.length - 2];
|
|
423
|
+
expect(noneProps.preload).toBe(false);
|
|
424
|
+
expect(noneProps.prefetch).toBeUndefined();
|
|
425
|
+
|
|
426
|
+
const explicitProps = capturedLinkProps.at(-1);
|
|
427
|
+
expect(explicitProps.preload).toBe('viewport');
|
|
428
|
+
expect(explicitProps.prefetch).toBeUndefined();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
test('marks the canonical target active on any localized variant', async () => {
|
|
432
|
+
const router = createTanstackRouter('/cs/podminky-pouzivani', 'cs');
|
|
433
|
+
rendered = await renderWithRuntime(
|
|
434
|
+
<ModernI18nProvider value={providerValue('cs')}>
|
|
435
|
+
<Link
|
|
436
|
+
to="/terms-of-service"
|
|
437
|
+
data-testid="active-link"
|
|
438
|
+
activeProps={{ className: 'is-active' }}
|
|
439
|
+
className="nav"
|
|
440
|
+
>
|
|
441
|
+
Terms
|
|
442
|
+
</Link>
|
|
443
|
+
<Link to="/products" data-testid="inactive-link">
|
|
444
|
+
Products
|
|
445
|
+
</Link>
|
|
446
|
+
</ModernI18nProvider>,
|
|
447
|
+
createTanstackRuntimeContext(router),
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
const active = rendered.container.querySelector(
|
|
451
|
+
'[data-testid="active-link"]',
|
|
452
|
+
);
|
|
453
|
+
expect(active?.getAttribute('data-status')).toBe('active');
|
|
454
|
+
expect(active?.getAttribute('aria-current')).toBe('page');
|
|
455
|
+
expect(active?.getAttribute('class')).toBe('nav is-active');
|
|
456
|
+
|
|
457
|
+
const inactive = rendered.container.querySelector(
|
|
458
|
+
'[data-testid="inactive-link"]',
|
|
459
|
+
);
|
|
460
|
+
expect(inactive?.getAttribute('data-status')).toBeNull();
|
|
461
|
+
expect(inactive?.getAttribute('aria-current')).toBeNull();
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
test('prefix-matches nested locations unless exact is requested', async () => {
|
|
465
|
+
const router = createTanstackRouter('/en/products/shoe', 'en');
|
|
466
|
+
rendered = await renderWithRuntime(
|
|
467
|
+
<ModernI18nProvider value={providerValue('en')}>
|
|
468
|
+
<Link to="/products" data-testid="prefix">
|
|
469
|
+
Products
|
|
470
|
+
</Link>
|
|
471
|
+
<Link
|
|
472
|
+
to="/products"
|
|
473
|
+
activeOptions={{ exact: true }}
|
|
474
|
+
data-testid="exact"
|
|
475
|
+
>
|
|
476
|
+
Products
|
|
477
|
+
</Link>
|
|
478
|
+
<Link to="/" data-testid="root">
|
|
479
|
+
Home
|
|
480
|
+
</Link>
|
|
481
|
+
</ModernI18nProvider>,
|
|
482
|
+
createTanstackRuntimeContext(router),
|
|
483
|
+
);
|
|
484
|
+
|
|
485
|
+
expect(
|
|
486
|
+
rendered.container
|
|
487
|
+
.querySelector('[data-testid="prefix"]')
|
|
488
|
+
?.getAttribute('data-status'),
|
|
489
|
+
).toBe('active');
|
|
490
|
+
expect(
|
|
491
|
+
rendered.container
|
|
492
|
+
.querySelector('[data-testid="exact"]')
|
|
493
|
+
?.getAttribute('data-status'),
|
|
494
|
+
).toBeNull();
|
|
495
|
+
expect(
|
|
496
|
+
rendered.container
|
|
497
|
+
.querySelector('[data-testid="root"]')
|
|
498
|
+
?.getAttribute('data-status'),
|
|
499
|
+
).toBeNull();
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
test('useLocalizedLocation exposes per-language alternates', async () => {
|
|
503
|
+
const router = createTanstackRouter('/cs/podminky-pouzivani?q=1#top', 'cs');
|
|
504
|
+
let snapshot: ReturnType<typeof useLocalizedLocation> | undefined;
|
|
505
|
+
|
|
506
|
+
const Probe = () => {
|
|
507
|
+
snapshot = useLocalizedLocation();
|
|
508
|
+
return null;
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
rendered = await renderWithRuntime(
|
|
512
|
+
<ModernI18nProvider value={providerValue('cs')}>
|
|
513
|
+
<Probe />
|
|
514
|
+
</ModernI18nProvider>,
|
|
515
|
+
createTanstackRuntimeContext(router),
|
|
516
|
+
);
|
|
517
|
+
|
|
518
|
+
expect(snapshot?.language).toBe('cs');
|
|
519
|
+
expect(snapshot?.canonical).toBe('/terms-of-service');
|
|
520
|
+
expect(snapshot?.alternates).toEqual({
|
|
521
|
+
en: '/en/terms-of-service?q=1#top',
|
|
522
|
+
cs: '/cs/podminky-pouzivani?q=1#top',
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
const fixtureDir = path.resolve(__dirname, 'type-fixture');
|
|
5
|
+
|
|
6
|
+
const tsgoBin = path.join(
|
|
7
|
+
path.dirname(require.resolve('@typescript/native-preview/package.json')),
|
|
8
|
+
'bin/tsgo.js',
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
describe('Link type-level tests', () => {
|
|
12
|
+
test('fixture type-checks correctly: valid uses compile, invalid uses are rejected', () => {
|
|
13
|
+
try {
|
|
14
|
+
execFileSync(
|
|
15
|
+
process.execPath,
|
|
16
|
+
[tsgoBin, '--noEmit', '-p', 'tsconfig.json'],
|
|
17
|
+
{
|
|
18
|
+
cwd: fixtureDir,
|
|
19
|
+
stdio: 'pipe',
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
} catch (e: any) {
|
|
23
|
+
const stdout = e?.stdout ? String(e.stdout) : '';
|
|
24
|
+
const stderr = e?.stderr ? String(e.stderr) : '';
|
|
25
|
+
throw new Error(`TypeScript type-check failed:\n${stdout}\n${stderr}`);
|
|
26
|
+
}
|
|
27
|
+
}, 60_000);
|
|
28
|
+
});
|