|
1 | 1 | import { test, expect } from 'vitest' |
2 | | -import { $fetch, url } from './utils' |
| 2 | +import { $fetch, url, useTestContext } from './utils' |
3 | 3 | import { |
4 | 4 | assertLocaleHeadWithDom, |
5 | 5 | assetLocaleHead, |
@@ -317,28 +317,40 @@ export function basicUsageTests() { |
317 | 317 | test('<NuxtLink> triggers runtime hooks', async () => { |
318 | 318 | const { page, consoleLogs } = await renderPage('/kr') |
319 | 319 |
|
320 | | - expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch kr fr true'))).toBeTruthy() |
321 | | - expect(consoleLogs.find(log => log.text.includes('onLanguageSwitched kr fr'))).toBeTruthy() |
322 | | - expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch fr fr false'))).toBeTruthy() |
323 | | - |
324 | | - // current locale |
325 | | - expect(await page.locator('#lang-switcher-current-locale code').innerText()).toEqual('fr') |
| 320 | + await page.waitForFunction(() => !window.useNuxtApp?.().isHydrating) |
| 321 | + consoleLogs.length = 0 |
326 | 322 |
|
327 | 323 | // navigate to about page |
328 | 324 | await page.locator('#link-about').clickNavigate() |
329 | 325 | await page.waitForURL(url('/fr/about')) |
330 | 326 |
|
331 | | - // navigate to home page |
332 | | - await page.locator('#link-home').clickNavigate() |
333 | | - await page.waitForURL(url('/fr')) |
| 327 | + expect(consoleLogs[0].text).toEqual('i18n:beforeLocaleSwitch fr fr false') |
334 | 328 | }) |
335 | 329 |
|
336 | 330 | test('setLocale triggers runtime hooks', async () => { |
| 331 | + let output: string[] = [] |
| 332 | + const ctx = useTestContext() |
| 333 | + ctx.serverProcess?.process?.on( |
| 334 | + 'message', |
| 335 | + (msg: any) => msg.type === 'i18n:test-log' && msg.id === ctx.url?.split(':')[2]! && output.push(msg.data) |
| 336 | + ) |
| 337 | + await new Promise(resolve => setTimeout(resolve, 1)) // wait for process to be ready |
| 338 | + output.length = 0 |
| 339 | + |
337 | 340 | const { page, consoleLogs } = await renderPage('/kr') |
338 | 341 |
|
339 | | - expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch kr fr true'))).toBeTruthy() |
340 | | - expect(consoleLogs.find(log => log.text.includes('onLanguageSwitched kr fr'))).toBeTruthy() |
341 | | - expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch fr fr false'))).toBeTruthy() |
| 342 | + // overrides and redirects to `fr` |
| 343 | + expect(output).toMatchInlineSnapshot(` |
| 344 | + [ |
| 345 | + "i18n:beforeLocaleSwitch kr fr true", |
| 346 | + "i18n:localeSwitched kr fr", |
| 347 | + "i18n:beforeLocaleSwitch fr fr false", |
| 348 | + "i18n:beforeLocaleSwitch fr fr true", |
| 349 | + ] |
| 350 | + `) |
| 351 | + |
| 352 | + // client-side enters on `fr` locale |
| 353 | + expect(consoleLogs.find(log => log.text.includes('i18n:beforeLocaleSwitch fr fr true'))).toBeTruthy() |
342 | 354 |
|
343 | 355 | // current locale |
344 | 356 | expect(await page.locator('#lang-switcher-current-locale code').innerText()).toEqual('fr') |
|
0 commit comments