forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.test.js
2470 lines (2143 loc) · 71.4 KB
/
module.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { join, resolve } from 'path'
import { readFileSync } from 'fs'
import { generate, setup, loadConfig, get, url } from '@nuxtjs/module-test-utils'
import { JSDOM } from 'jsdom'
import { withoutTrailingSlash, withTrailingSlash } from 'ufo'
import { adjustRouteDefinitionForTrailingSlash } from '../src/helpers/utils'
import { getSeoTags } from './utils'
/**
* @typedef {any} Nuxt
* @typedef {import('@nuxt/types').NuxtConfig} NuxtConfig
*/
/** @param {string} html */
const getDom = html => (new JSDOM(html)).window.document
describe('locales as string array', () => {
/** @type {Nuxt} */
let nuxt
beforeAll(async () => {
const testConfig = loadConfig(__dirname, 'no-lang-switcher')
// Override those after merging to overwrite original values.
testConfig.i18n.locales = ['en', 'fr']
nuxt = (await setup(testConfig)).nuxt
})
afterAll(async () => {
await nuxt.close()
})
test('renders default locale', async () => {
const html = await get('/about')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: About us')
})
test('renders non-default locale', async () => {
const html = await get('/fr/about')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: À propos')
})
test('dir attribute will not be added to the html element', async () => {
const html = await get('/about')
const dom = getDom(html)
expect(dom.documentElement.getAttribute('dir')).toBeNull()
})
test('nuxtI18nHead does not set SEO Meta', async () => {
const html = await get('/about')
const dom = getDom(html)
const seoTags = getSeoTags(dom)
expect(seoTags).toEqual([])
})
})
describe('differentDomains enabled', () => {
/** @type {Nuxt} */
let nuxt
beforeAll(async () => {
const override = {
i18n: {
differentDomains: true,
defaultDirection: 'auto'
}
}
const localConfig = loadConfig(__dirname, 'basic', override, { merge: true })
// Override after merging options to avoid arrays being merged.
localConfig.i18n.locales = [
{
code: 'en',
iso: 'en-US',
name: 'English',
domain: 'en.nuxt-app.localhost',
dir: 'ltr'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français',
domain: 'fr.nuxt-app.localhost'
},
{
code: 'ru',
iso: 'ru-RU',
name: 'Русский',
domain: 'https://ru.nuxt-app.localhost'
},
{
code: 'ua',
iso: 'uk-UA',
name: 'Українська'
}
]
nuxt = (await setup(localConfig)).nuxt
})
afterAll(async () => {
await nuxt.close()
})
test('host matches locale\'s domain (en)', async () => {
const requestOptions = {
headers: {
Host: 'en.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('body')?.textContent).toContain('page: Homepage')
expect(dom.querySelector('head meta[property="og-locale"]')).toBe(null)
})
test('host matches locale\'s domain (fr)', async () => {
const requestOptions = {
headers: {
Host: 'fr.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('body')?.textContent).toContain('page: Accueil')
})
test('host matches locale\'s runtime-set domain (ua)', async () => {
const requestOptions = {
headers: {
Host: 'ua-runtime.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('body')?.textContent).toContain('locale: ua')
})
test('x-forwarded-host does not match locale\'s domain', async () => {
const requestOptions = {
headers: {
'X-Forwarded-Host': 'xx.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
// Falls back to english.
expect(dom.querySelector('body')?.textContent).toContain('page: Homepage')
})
test('x-forwarded-host does match locale\'s domain (fr)', async () => {
const requestOptions = {
headers: {
'X-Forwarded-Host': 'fr.nuxt-app.localhost'
}
}
const html = await get('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('body')?.textContent).toContain('page: Accueil')
})
test('dir attribute exists and is set to the default direction', async () => {
const requestOptions = {
headers: {
'X-Forwarded-Host': 'fr.nuxt-app.localhost'
}
}
const html = await get('/locale', requestOptions)
const dom = getDom(html)
expect(dom.documentElement.getAttribute('dir')).toEqual('auto')
})
test('dir and SEO attributes exists', async () => {
const requestOptions = {
headers: {
Host: 'en.nuxt-app.localhost'
}
}
const html = await get('/locale', requestOptions)
const dom = getDom(html)
expect(dom.documentElement.getAttribute('dir')).toEqual('ltr')
const seoTags = getSeoTags(dom)
const expectedSeoTags = [
{
tagName: 'meta',
property: 'og:locale',
content: 'en_US'
},
{
tagName: 'meta',
property: 'og:locale:alternate',
content: 'fr_FR'
},
{
tagName: 'meta',
property: 'og:locale:alternate',
content: 'ru_RU'
},
{
tagName: 'meta',
property: 'og:locale:alternate',
content: 'uk_UA'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://en.nuxt-app.localhost/locale',
hreflang: 'en'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://en.nuxt-app.localhost/locale',
hreflang: 'en-US'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://fr.nuxt-app.localhost/locale',
hreflang: 'fr'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://fr.nuxt-app.localhost/locale',
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: 'https://ru.nuxt-app.localhost/locale',
hreflang: 'ru'
},
{
tagName: 'link',
rel: 'alternate',
href: 'https://ru.nuxt-app.localhost/locale',
hreflang: 'ru-RU'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://ua-runtime.nuxt-app.localhost/locale',
hreflang: 'uk'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://ua-runtime.nuxt-app.localhost/locale',
hreflang: 'uk-UA'
},
{
tagName: 'link',
rel: 'alternate',
href: 'http://en.nuxt-app.localhost/locale',
hreflang: 'x-default'
},
{
tagName: 'link',
rel: 'canonical',
href: 'http://en.nuxt-app.localhost/locale'
}
]
expect(seoTags).toEqual(expectedSeoTags)
})
})
const TRAILING_SLASHES = [undefined, false, true]
for (const trailingSlash of TRAILING_SLASHES) {
describe(`basic (trailingSlash is "${trailingSlash}")`, () => {
/** @type {Nuxt} */
let nuxt
/** @param {string} path */
const pathRespectingTrailingSlash = path => {
return (trailingSlash ? withTrailingSlash(path, true) : withoutTrailingSlash(path, true) || withTrailingSlash(path, true))
}
/** @type {get} */
const getRespectingTrailingSlash = async (path, options) => await get(pathRespectingTrailingSlash(path), options)
beforeAll(async () => {
/** @type {import('@nuxt/types').NuxtConfig} */
const overrides = {
router: {
trailingSlash,
// Redirects are not processed by the module.
extendRoutes (routes) {
routes.push({
path: adjustRouteDefinitionForTrailingSlash('/about-redirect', trailingSlash),
name: 'about-redirect___en',
redirect: { name: 'about___en' }
})
}
}
}
/** @type {import('@nuxt/types').NuxtConfig} */
const testConfig = loadConfig(__dirname, 'basic', overrides, { merge: true })
// Extend routes before the module so that the module processes them.
testConfig.modules?.unshift(join(__dirname, 'fixture', 'basic', 'extend-routes'))
nuxt = (await setup(testConfig)).nuxt
})
afterAll(async () => {
await nuxt.close()
})
test('sets SEO metadata properly', async () => {
const html = await getRespectingTrailingSlash('/')
const dom = getDom(html)
const seoTags = getSeoTags(dom)
const expectedSeoTags = [
{
tagName: 'meta',
property: 'og:locale',
content: 'en'
},
{
tagName: 'meta',
property: 'og:locale:alternate',
content: 'fr_FR'
},
{
tagName: 'link',
rel: 'alternate',
href: 'nuxt-app.localhost/',
hreflang: 'en'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr'),
hreflang: 'fr'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr'),
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: 'nuxt-app.localhost/',
hreflang: 'x-default'
},
{
tagName: 'link',
rel: 'canonical',
href: 'nuxt-app.localhost/'
}
]
expect(seoTags).toEqual(expectedSeoTags)
})
test('/ contains EN text, link to /fr/ & link /about-us', async () => {
const html = await getRespectingTrailingSlash('/')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: Homepage')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr'))
expect(langSwitcher?.children[0].textContent).toBe('Français')
const aboutLink = dom.querySelector('#link-about')
expect(aboutLink).not.toBeNull()
expect(aboutLink?.getAttribute('href')).toBe(pathRespectingTrailingSlash('/about-us'))
expect(aboutLink?.textContent).toBe('About us')
})
test('/fr contains FR text, link to / & link to /fr/a-propos', async () => {
const html = await getRespectingTrailingSlash('/fr')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: Accueil')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe('/')
expect(langSwitcher?.children[0].textContent).toBe('English')
const aboutLink = dom.querySelector('#link-about')
expect(aboutLink).not.toBeNull()
expect(aboutLink?.getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr/a-propos'))
expect(aboutLink?.textContent).toBe('À propos')
})
test('/about-us contains EN text, link to /fr/a-propos & link /', async () => {
const html = await getRespectingTrailingSlash('/about-us')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: About us')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr/a-propos'))
expect(langSwitcher?.children[0].textContent).toBe('Français')
const homeLink = dom.querySelector('#link-home')
expect(homeLink).not.toBeNull()
expect(homeLink?.getAttribute('href')).toBe('/')
expect(homeLink?.textContent).toBe('Homepage')
})
test('/fr/a-propos contains FR text, link to /about-us & link to /fr/', async () => {
const html = await getRespectingTrailingSlash('/fr/a-propos')
const dom = getDom(html)
expect(dom.querySelector('#current-page')?.textContent).toBe('page: À propos')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe(pathRespectingTrailingSlash('/about-us'))
expect(langSwitcher?.children[0].textContent).toBe('English')
const homeLink = dom.querySelector('#link-home')
expect(homeLink).not.toBeNull()
expect(homeLink?.getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr'))
expect(homeLink?.textContent).toBe('Accueil')
})
test('/fr/notlocalized contains FR text', async () => {
const html = await getRespectingTrailingSlash('/fr/notlocalized')
const dom = getDom(html)
expect(dom.querySelector('main')?.textContent).toBe('FR only')
})
test('/notlocalized & /fr/fr/notlocalized return 404', async () => {
expect.assertions(2)
await getRespectingTrailingSlash('/notlocalized').catch(error => expect(error.statusCode).toBe(404))
await getRespectingTrailingSlash('/fr/fr/notlocalized').catch(error => expect(error.statusCode).toBe(404))
})
test('route specifies options with non-supported locale', async () => {
await expect(getRespectingTrailingSlash('/simple')).resolves.toBeDefined()
await expect(getRespectingTrailingSlash('/fr/simple')).resolves.toBeDefined()
await expect(getRespectingTrailingSlash('/es/simple')).rejects.toBeDefined()
})
test('navigates to route with optional param without the param specified', async () => {
await expect(getRespectingTrailingSlash('/custom-route/')).resolves.toBeDefined()
})
describe('posts', () => {
/** @type {string} */
let html
/** @type {HTMLHeadingElement | null} */
let title
/** @type {HTMLAnchorElement | null} */
let langSwitcherLink
/** @type {HTMLAnchorElement | null} */
let link
const getElements = () => {
const dom = getDom(html)
title = dom.querySelector('h1')
langSwitcherLink = dom.querySelector('#lang-switcher a')
link = dom.querySelector('#post-link')
}
// TODO: Broken in Nuxt +2.14.0
if (trailingSlash !== false) {
test('/posts contains EN text, link to /fr/articles/ & link to /posts/my-post', async () => {
html = await getRespectingTrailingSlash('/posts')
getElements()
expect(title?.textContent).toBe('Posts')
expect(langSwitcherLink?.href).toBe('/fr/articles/')
// if (trailingSlash === false) {
// expect(link).toBeNull()
// } else {
expect(link?.href).toBe(pathRespectingTrailingSlash('/posts/my-post'))
// }
})
}
test('/posts/my-post contains EN text, link to /fr/articles/mon-article & link to /posts/', async () => {
html = await getRespectingTrailingSlash('/posts/my-post')
getElements()
expect(title?.textContent).toBe('Posts')
expect(langSwitcherLink?.href).toBe(pathRespectingTrailingSlash('/fr/articles/mon-article'))
expect(link?.href).toBe('/posts/')
})
// TODO: Broken in Nuxt +2.14.0
if (trailingSlash !== false) {
test('/fr/articles contains FR text, link to /posts/ & link to /fr/articles/mon-article', async () => {
html = await getRespectingTrailingSlash('/fr/articles')
getElements()
expect(title?.textContent).toBe('Articles')
expect(langSwitcherLink?.href).toBe('/posts/')
// if (trailingSlash === false) {
// expect(link).toBeNull()
// } else {
expect(link?.href).toBe(pathRespectingTrailingSlash('/fr/articles/mon-article'))
// }
})
test('/fr/articles/mon-article contains FR text, link to /posts/my-post & link to /fr/articles/', async () => {
html = await getRespectingTrailingSlash('/fr/articles/mon-article')
getElements()
expect(title?.textContent).toBe('Articles')
expect(langSwitcherLink?.href).toBe(pathRespectingTrailingSlash('/posts/my-post'))
expect(link?.href).toBe('/fr/articles/')
})
}
})
describe('store', () => {
test('injects $i18n in store', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$store.$i18n).toBeDefined()
})
})
// TODO: Broken in Nuxt +2.14.0
if (trailingSlash !== false) {
test('navigates to child route with nameless parent and checks path to other locale', async () => {
const window = await nuxt.renderAndGetWindow(url(pathRespectingTrailingSlash('/posts')))
const langSwitcherLink = window.document.querySelector('#lang-switcher a')
expect(langSwitcherLink.getAttribute('href')).toBe('/fr/articles/')
const link = window.document.querySelector('#post-link')
// if (trailingSlash === false) {
// expect(link).toBeNull()
// } else {
expect(link.getAttribute('href')).toBe(pathRespectingTrailingSlash('/posts/my-post'))
// }
})
}
test('localePath with route-less params navigates to same locale route', async () => {
const window = await nuxt.renderAndGetWindow(url(pathRespectingTrailingSlash('/posts/my-post')))
const link = window.document.querySelector('#post-link-no-route')
expect(link.getAttribute('href')).toBe(pathRespectingTrailingSlash('/posts/look-ma-no-route'))
})
test('localePath with route-less params navigates to different locale route', async () => {
const window = await nuxt.renderAndGetWindow(url(pathRespectingTrailingSlash('/posts/my-post')))
const link = window.document.querySelector('#post-link-no-route-fr')
expect(link.getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr/articles/look-ma-no-route'))
})
test('navigates to dynamic child route and checks path to other locale', async () => {
const window = await nuxt.renderAndGetWindow(url(pathRespectingTrailingSlash('/dynamicNested/1')))
const body = window.document.querySelector('body')
expect(body.textContent).toContain('Category')
if (trailingSlash === true) {
expect(body.textContent).toContain('Subcategory')
} else {
expect(body.textContent).not.toContain('Subcategory')
}
// Will only work if navigated-to route has a name.
expect(window.$nuxt.switchLocalePath('fr')).toBe(pathRespectingTrailingSlash('/fr/imbrication-dynamique/1'))
})
test('/dynamicNested/1/2/3 contains link to /fr/imbrication-dynamique/1/2/3', async () => {
const html = await getRespectingTrailingSlash('/dynamicNested/1/2/3')
const dom = getDom(html)
expect(dom.querySelector('h1')?.textContent).toBe('Category 1')
expect(dom.querySelector('h2')?.textContent).toBe('Subcategory 2')
expect(dom.querySelector('h3')?.textContent).toBe('Post 3')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe(pathRespectingTrailingSlash('/fr/imbrication-dynamique/1/2/3'))
expect(langSwitcher?.children[0].textContent).toBe('Français')
})
test('/fr/imbrication-dynamique/1/2/3 contains link to /dynamicNested/1/2/3', async () => {
const html = await getRespectingTrailingSlash('/fr/imbrication-dynamique/1/2/3')
const dom = getDom(html)
expect(dom.querySelector('h1')?.textContent).toBe('Category 1')
expect(dom.querySelector('h2')?.textContent).toBe('Subcategory 2')
expect(dom.querySelector('h3')?.textContent).toBe('Post 3')
const langSwitcher = dom.querySelector('#lang-switcher')
expect(langSwitcher).not.toBeNull()
expect(langSwitcher?.children.length).toBe(1)
expect(langSwitcher?.children[0].getAttribute('href')).toBe(pathRespectingTrailingSlash('/dynamicNested/1/2/3'))
expect(langSwitcher?.children[0].textContent).toBe('English')
})
test('localePath resolves correct path', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localePath('about')).toBe(pathRespectingTrailingSlash('/about-us'))
expect(window.$nuxt.localePath('about', 'fr')).toBe(pathRespectingTrailingSlash('/fr/a-propos'))
expect(window.$nuxt.localePath('/about-us')).toBe(pathRespectingTrailingSlash('/about-us'))
})
test('localePath resolves route name to non-redirected path', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localePath('about-redirect')).toBe('/about-redirect')
})
test('localePath resolves route path to redirected path', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localePath('/about-redirect')).toBe(pathRespectingTrailingSlash('/about-us'))
expect(window.$nuxt.localePath({ path: '/about-redirect' })).toBe(pathRespectingTrailingSlash('/about-us'))
})
test('switchLocalePath returns correct path', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.switchLocalePath('fr')).toBe(pathRespectingTrailingSlash('/fr'))
})
test('getRouteBaseName returns correct name', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.getRouteBaseName()).toBe('index')
})
test('getRouteBaseName returns name of passed in route', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
/** @type {import('vue-router').RouterOptions} */
const routerOptions = window.$nuxt.$router.options
const aboutRoute = routerOptions.routes?.find(route => route.path === pathRespectingTrailingSlash('/about-us'))
expect(aboutRoute).toBeDefined()
expect(aboutRoute?.name).toBeDefined()
expect(window.$nuxt.getRouteBaseName(aboutRoute)).toBe('about')
})
test('localeRoute returns localized route', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localeRoute('about', 'en')).toMatchObject({
name: 'about___en',
fullPath: pathRespectingTrailingSlash('/about-us')
})
})
test('localeRoute with custom location object retains params', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.localeRoute({ name: 'about', params: { foo: '1' } }, 'en')).toMatchObject({
name: 'about___en',
fullPath: pathRespectingTrailingSlash('/about-us'),
params: {
foo: '1'
}
})
})
test('localePath, switchLocalePath, getRouteBaseName, localeRoute works from a middleware', async () => {
const html = await getRespectingTrailingSlash('/middleware')
const dom = getDom(html)
expect(dom.querySelector('#paths')?.textContent).toBe(
[pathRespectingTrailingSlash('/middleware'), pathRespectingTrailingSlash('/fr/middleware-fr')].join(',')
)
expect(dom.querySelector('#name')?.textContent).toBe('middleware')
const routeObject = dom.querySelector('#localizedRoute')?.textContent || '{}'
expect(JSON.parse(routeObject)).toMatchObject({
name: 'middleware___fr'
})
})
test('redirects to existing route', async () => {
const window = await nuxt.renderAndGetWindow(url(pathRespectingTrailingSlash('/about-redirect')))
const newRoute = window.$nuxt.switchLocalePath()
expect(newRoute).toBe(pathRespectingTrailingSlash('/about-us'))
})
test('fallbacks to default locale with invalid locale cookie', async () => {
const requestOptions = {
headers: {
Cookie: 'i18n_redirected=invalid'
}
}
const html = await getRespectingTrailingSlash('/', requestOptions)
const dom = getDom(html)
expect(dom.querySelector('#current-locale')?.textContent).toBe('locale: en')
})
test('registers message using vueI18nLoader', async () => {
const html = await getRespectingTrailingSlash('/loader')
const dom = getDom(html)
expect(dom.querySelector('#container')?.textContent).toBe('string from loader EN')
})
test('registers message using vueI18nLoader from yaml block', async () => {
let html = await getRespectingTrailingSlash('/loader-yaml')
let dom = getDom(html)
let title = dom.querySelector('p')
expect(title?.textContent).toBe('hello world!')
html = await getRespectingTrailingSlash('/fr/loader-yaml')
dom = getDom(html)
title = dom.querySelector('p')
expect(title?.textContent).toBe('Bonjour le monde!')
})
test('can use @nuxtjs/i18n extensions from component local i18n instance', async () => {
const html = await getRespectingTrailingSlash('/loader-yaml')
const dom = getDom(html)
const title = dom.querySelector('p#title')
expect(title?.textContent).toBe('hello world!')
const locales = dom.querySelector('p#locales')
const localesContent = locales?.textContent || '{}'
expect(JSON.parse(localesContent)).toMatchObject([
{
code: 'en',
iso: 'en',
name: 'English'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français'
}
])
})
test('baseUrl is set correctly in component local i18n instance', async () => {
const html = await getRespectingTrailingSlash('/loader-yaml')
const dom = getDom(html)
const seoTags = getSeoTags(dom)
const expectedSeoTags = [
{
tagName: 'meta',
property: 'og:locale',
content: 'en'
},
{
tagName: 'meta',
property: 'og:locale:alternate',
content: 'fr_FR'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/loader-yaml'),
hreflang: 'en'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr/loader-yaml'),
hreflang: 'fr'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr/loader-yaml'),
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/loader-yaml'),
hreflang: 'x-default'
},
{
tagName: 'link',
rel: 'canonical',
href: pathRespectingTrailingSlash('nuxt-app.localhost/loader-yaml')
}
]
expect(seoTags).toEqual(expectedSeoTags)
})
})
}
describe('hreflang', () => {
/** @type {Nuxt} */
let nuxt
beforeAll(async () => {
const testConfig = loadConfig(__dirname, 'basic')
// Override those after merging to overwrite original values.
testConfig.i18n.locales = [
{
code: 'en',
iso: 'en',
name: 'English',
dir: 'auto'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français'
},
{
code: 'es',
iso: 'es-ES',
name: 'Spanish (Spain)'
},
{
code: 'esVe',
iso: 'es-VE',
name: 'Spanish (Venezuela)',
isCatchallLocale: true
}
]
nuxt = (await setup(testConfig)).nuxt
})
test('sets SEO metadata and dir attribute properly', async () => {
const html = await get('/locale')
const dom = getDom(html)
const seoTags = getSeoTags(dom)
const expectedSeoTags = [
{
content: 'en',
property: 'og:locale',
tagName: 'meta'
},
{
content: 'fr_FR',
property: 'og:locale:alternate',
tagName: 'meta'
},
{
content: 'es_ES',
property: 'og:locale:alternate',
tagName: 'meta'
},
{
content: 'es_VE',
property: 'og:locale:alternate',
tagName: 'meta'
},
{
href: 'nuxt-app.localhost/locale',
hreflang: 'en',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/fr/locale',
hreflang: 'fr',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/fr/locale',
hreflang: 'fr-FR',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/esVe/locale',
hreflang: 'es',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/es/locale',
hreflang: 'es-ES',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/esVe/locale',
hreflang: 'es-VE',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/locale',
hreflang: 'x-default',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/locale',
rel: 'canonical',
tagName: 'link'
}
]
expect(dom.documentElement.getAttribute('dir')).toEqual('auto')
expect(seoTags).toEqual(expectedSeoTags)
})
test('localeProperties object exists and is set to the correct object', async () => {
const html = await get('/loader-yaml')
const dom = getDom(html)
const localeProperties = dom.querySelector('p#localeProperties')
const localePropertiesContent = localeProperties?.textContent || '{}'
expect(JSON.parse(localePropertiesContent)).toMatchObject(
{
code: 'en',
iso: 'en',
name: 'English'
})
})
test('dir attribute will not be added to the html element', async () => {
const html = await get('/fr')
const dom = getDom(html)
expect(dom.documentElement.getAttribute('dir')).toBeNull()
})
afterAll(async () => {
await nuxt.close()
})
})
describe('lazy loading', () => {
/** @type {Nuxt} */
let nuxt
beforeAll(async () => {
const override = {
i18n: {
lazy: true,
langDir: 'lang/',
vueI18n: {
fallbackLocale: 'en'
}
}
}
const testConfig = loadConfig(__dirname, 'basic', override, { merge: true })
// Override those after merging to overwrite original values.
testConfig.i18n.locales = [
{
code: 'en',
iso: 'en-US',
name: 'English',
file: 'en-US.js'
},
{
code: 'fr',
iso: 'fr-FR',
name: 'Français',
file: 'fr-FR.js'
}
]
testConfig.i18n.vueI18n.messages = null
nuxt = (await setup(testConfig)).nuxt
})
afterAll(async () => {
await nuxt.close()
})
test('shows fallback string', async () => {
const html = await get('/fr/fallback')
const dom = getDom(html)
const title = dom.querySelector('h1')
expect(title?.textContent).toBe('in english')
})
test('loads strings from file exporting a function', async () => {
const html = await get('/fr/simple')
const dom = getDom(html)
const container = dom.querySelector('#container')
expect(container?.textContent).toBe('Accueil')
})
test('exported function gets passed locale to load', async () => {
const html = await get('/fr/locale')
const dom = getDom(html)
const container = dom.querySelector('#t')
expect(container?.textContent).toBe('fr')
})
})
describe('with empty configuration', () => {
/** @type {Nuxt} */
let nuxt
beforeAll(async () => {
nuxt = (await setup(loadConfig(__dirname, 'no-i18n', { i18n: {} }))).nuxt
})
afterAll(async () => {
await nuxt.close()
})
test('does not remove all routes', async () => {
await nuxt.renderAndGetWindow(url('/about'))
})
test('localeProperties object exists and is set to an object with no code', async () => {
const window = await nuxt.renderAndGetWindow(url('/'))
expect(window.$nuxt.$i18n.localeProperties).toEqual({ code: '' })
})
})