forked from nuxt-modules/i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime_hooks.spec.ts
64 lines (53 loc) · 2.25 KB
/
runtime_hooks.spec.ts
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
import { test, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup, url, createPage } from './utils'
import { getText } from './helper'
await setup({
rootDir: fileURLToPath(new URL(`./fixtures/runtime_hook`, import.meta.url)),
browser: true,
// overrides
nuxtConfig: {
plugins: [fileURLToPath(new URL(`./fixtures/plugins/i18nHooks.ts`, import.meta.url))],
i18n: { defaultLocale: 'en' }
}
})
describe('beforeLocaleSwitch / localeSwitched', () => {
test('<NuxtLink>', async () => {
const home = url('/')
const page = await createPage()
const messages: string[] = []
page.on('console', msg => messages.push(msg.text()))
await page.goto(home)
// click `fr` lang switch link
await page.locator('#lang-switcher-with-nuxt-link a').click()
// click `en` lang switch link
await page.locator('#lang-switcher-with-nuxt-link a').click()
expect(messages.find(m => m.includes('onBeforeLanguageSwitch en fr true'))).toBeTruthy()
expect(messages.find(m => m.includes('onLanguageSwitched en fr'))).toBeTruthy()
expect(messages.find(m => m.includes('onBeforeLanguageSwitch fr en false'))).toBeTruthy()
// current locale
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
// navigate to about page
await page.locator('#link-about').click()
await page.waitForTimeout(1000)
// navigate to home page
await page.locator('#link-home').click()
await page.waitForTimeout(1000)
})
test('setLocale', async () => {
const home = url('/')
const page = await createPage()
const messages: string[] = []
page.on('console', msg => messages.push(msg.text()))
await page.goto(home)
// click `fr` lang switch link
await page.locator('#set-locale-link-fr').click()
// click `en` lang switch link
await page.locator('#set-locale-link-en').click()
expect(messages.find(m => m.includes('onBeforeLanguageSwitch en fr true'))).toBeTruthy()
expect(messages.find(m => m.includes('onLanguageSwitched en fr'))).toBeTruthy()
expect(messages.find(m => m.includes('onBeforeLanguageSwitch fr en false'))).toBeTruthy()
// current locale
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
})
})