|
| 1 | +import { test, expect, describe } from 'vitest' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | +import { setup, url } from '../utils' |
| 4 | +import { getText, getData, waitForMs, renderPage, waitForURL } from '../helper' |
| 5 | + |
| 6 | +describe('basic lazy loading', async () => { |
| 7 | + await setup({ |
| 8 | + rootDir: fileURLToPath(new URL(`../fixtures/lazy-without-server`, import.meta.url)), |
| 9 | + browser: true, |
| 10 | + prerender: true, |
| 11 | + nuxtConfig: { |
| 12 | + i18n: { |
| 13 | + debug: true |
| 14 | + } |
| 15 | + } |
| 16 | + }) |
| 17 | + |
| 18 | + test('dynamic locale files are not cached', async () => { |
| 19 | + const { page } = await renderPage('/nl') |
| 20 | + |
| 21 | + page.on('domcontentloaded', () => { |
| 22 | + console.log('domcontentload triggered!') |
| 23 | + }) |
| 24 | + |
| 25 | + // capture dynamicTime - simulates changing api response |
| 26 | + const dynamicTime = await getText(page, '#dynamic-time') |
| 27 | + |
| 28 | + await page.click('#lang-switcher-with-nuxt-link-fr') |
| 29 | + await waitForURL(page, '/fr') |
| 30 | + expect(await getText(page, '#dynamic-time')).toEqual('Not dynamic') |
| 31 | + |
| 32 | + // dynamicTime depends on passage of some time |
| 33 | + await waitForMs(100) |
| 34 | + |
| 35 | + // dynamicTime does not match captured dynamicTime |
| 36 | + await page.click('#lang-switcher-with-nuxt-link-nl') |
| 37 | + await waitForURL(page, '/nl') |
| 38 | + expect(await getText(page, '#dynamic-time')).to.not.equal(dynamicTime) |
| 39 | + }) |
| 40 | + |
| 41 | + test('locales are fetched on demand', async () => { |
| 42 | + const home = url('/') |
| 43 | + const { page, requests } = await renderPage(home) |
| 44 | + |
| 45 | + const setFromRequests = () => [...new Set(requests)].filter(x => x.includes('lazy-locale-')) |
| 46 | + |
| 47 | + // only default locales are fetched (en) |
| 48 | + await page.goto(home) |
| 49 | + console.log(setFromRequests()) |
| 50 | + expect(setFromRequests().filter(locale => locale.includes('fr') || locale.includes('nl'))).toHaveLength(0) |
| 51 | + |
| 52 | + // wait for request after navigation |
| 53 | + const localeRequestFr = page.waitForRequest(/lazy-locale-fr/) |
| 54 | + await page.click('#lang-switcher-with-nuxt-link-fr') |
| 55 | + await localeRequestFr |
| 56 | + |
| 57 | + // `fr` locale has been fetched |
| 58 | + expect(setFromRequests().filter(locale => locale.includes('fr'))).toHaveLength(1) |
| 59 | + |
| 60 | + // wait for request after navigation |
| 61 | + const localeRequestNl = page.waitForRequest(/lazy-locale-module-nl/) |
| 62 | + await page.click('#lang-switcher-with-nuxt-link-nl') |
| 63 | + await localeRequestNl |
| 64 | + |
| 65 | + // `nl` (module) locale has been fetched |
| 66 | + expect(setFromRequests().filter(locale => locale.includes('nl'))).toHaveLength(1) |
| 67 | + }) |
| 68 | + |
| 69 | + test('can access to no prefix locale (en): /', async () => { |
| 70 | + const { page } = await renderPage('/') |
| 71 | + |
| 72 | + // `en` rendering |
| 73 | + expect(await getText(page, '#home-header')).toEqual('Homepage') |
| 74 | + expect(await getText(page, 'title')).toEqual('Homepage') |
| 75 | + expect(await getText(page, '#link-about')).toEqual('About us') |
| 76 | + |
| 77 | + // lang switcher rendering |
| 78 | + expect(await getText(page, '#set-locale-link-fr')).toEqual('Français') |
| 79 | + |
| 80 | + // page path |
| 81 | + expect(await getData(page, '#home-use-async-data')).toMatchObject({ aboutPath: '/about' }) |
| 82 | + |
| 83 | + // current locale |
| 84 | + expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('en') |
| 85 | + |
| 86 | + // html tag `lang` attribute with language code |
| 87 | + expect(await page.getAttribute('html', 'lang')).toEqual('en-US') |
| 88 | + }) |
| 89 | + |
| 90 | + test('can access to prefix locale: /fr', async () => { |
| 91 | + const { page } = await renderPage('/fr') |
| 92 | + |
| 93 | + console.log(page.url()) |
| 94 | + |
| 95 | + // `fr` rendering |
| 96 | + expect(await getText(page, '#home-header')).toEqual('Accueil') |
| 97 | + expect(await getText(page, 'title')).toEqual('Accueil') |
| 98 | + expect(await getText(page, '#link-about')).toEqual('À propos') |
| 99 | + |
| 100 | + // lang switcher rendering |
| 101 | + expect(await getText(page, '#set-locale-link-en')).toEqual('English') |
| 102 | + |
| 103 | + // page path |
| 104 | + expect(await getData(page, '#home-use-async-data')).toMatchObject({ aboutPath: '/fr/about' }) |
| 105 | + |
| 106 | + // current locale |
| 107 | + expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr') |
| 108 | + |
| 109 | + // html tag `lang` attribute with language code |
| 110 | + expect(await page.getAttribute('html', 'lang')).toEqual('fr-FR') |
| 111 | + }) |
| 112 | + |
| 113 | + test('mutiple lazy loading', async () => { |
| 114 | + const { page } = await renderPage('/en-GB') |
| 115 | + |
| 116 | + // `en` base rendering |
| 117 | + expect(await getText(page, '#home-header')).toEqual('Homepage') |
| 118 | + expect(await getText(page, 'title')).toEqual('Homepage') |
| 119 | + expect(await getText(page, '#link-about')).toEqual('About us') |
| 120 | + |
| 121 | + expect(await getText(page, '#profile-js')).toEqual('Profile1') |
| 122 | + expect(await getText(page, '#profile-ts')).toEqual('Profile2') |
| 123 | + }) |
| 124 | + |
| 125 | + test('files with cache disabled bypass caching', async () => { |
| 126 | + const { page, consoleLogs } = await renderPage('/') |
| 127 | + |
| 128 | + await page.click('#lang-switcher-with-nuxt-link-en-GB') |
| 129 | + expect([...consoleLogs].filter(log => log.text.includes('lazy-locale-en-GB.js bypassing cache!'))).toHaveLength(1) |
| 130 | + |
| 131 | + await page.click('#lang-switcher-with-nuxt-link-fr') |
| 132 | + expect([...consoleLogs].filter(log => log.text.includes('lazy-locale-fr.json5 bypassing cache!'))).toHaveLength(1) |
| 133 | + |
| 134 | + await page.click('#lang-switcher-with-nuxt-link-en-GB') |
| 135 | + expect([...consoleLogs].filter(log => log.text.includes('lazy-locale-en-GB.js bypassing cache!'))).toHaveLength(2) |
| 136 | + |
| 137 | + await page.click('#lang-switcher-with-nuxt-link-fr') |
| 138 | + expect([...consoleLogs].filter(log => log.text.includes('lazy-locale-fr.json5 bypassing cache!'))).toHaveLength(2) |
| 139 | + }) |
| 140 | + |
| 141 | + test('manually loaded messages can be used in translations', async () => { |
| 142 | + const { page } = await renderPage('/manual-load') |
| 143 | + |
| 144 | + expect(await getText(page, '#welcome-english')).toEqual('Welcome!') |
| 145 | + expect(await getText(page, '#welcome-dutch')).toEqual('Welkom!') |
| 146 | + }) |
| 147 | +}) |
0 commit comments