Skip to content

Commit

Permalink
test: merge test files (nuxt-modules#2570)
Browse files Browse the repository at this point in the history
* refactor: merge tests

* refactor: merge tests (nuxt-modules#24)
  • Loading branch information
BobbieGoede authored Nov 25, 2023
1 parent 8e3fab2 commit 167a2c2
Show file tree
Hide file tree
Showing 59 changed files with 302 additions and 711 deletions.
25 changes: 17 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 112 additions & 14 deletions specs/basic_usage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { test, expect } from 'vitest'
import { fileURLToPath } from 'node:url'
import { setup } from './utils'
import { getData, getText, gotoPath, renderPage, waitForURL } from './helper'
import { assetLocaleHead, getData, getText, gotoPath, renderPage, waitForURL } from './helper'

await setup({
rootDir: fileURLToPath(new URL(`./fixtures/basic_usage`, import.meta.url)),
browser: true,
// prerender: true,
// overrides
nuxtConfig: {
extends: [
fileURLToPath(new URL(`./fixtures/layers/layer-lazy`, import.meta.url)),
fileURLToPath(new URL(`./fixtures/layers/layer-vueI18n-options/layer-simple`, import.meta.url)),
fileURLToPath(new URL(`./fixtures/layers/layer-vueI18n-options/layer-simple-secondary`, import.meta.url))
],
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en'
}
}
nuxtConfig: {}
})

test('basic usage', async () => {
Expand Down Expand Up @@ -138,7 +128,7 @@ test('fallback to target lang', async () => {

// `en` rendering
expect(await getText(page, '#locale-path-usages .name a')).toEqual('Homepage')
expect(await getText(page, 'title')).toEqual('Homepage')
expect(await getText(page, 'title')).toEqual('Page - Homepage')
expect(await getText(page, '#fallback-key')).toEqual('This is the fallback message!')

// click `nl` lang switch with `<NuxtLink>`
Expand All @@ -147,7 +137,7 @@ test('fallback to target lang', async () => {

// fallback to en content translation
expect(await getText(page, '#locale-path-usages .name a')).toEqual('Homepage')
expect(await getText(page, 'title')).toEqual('Homepage')
expect(await getText(page, 'title')).toEqual('Page - Homepage')
expect(await getText(page, '#fallback-key')).toEqual('This is the fallback message!')

// page path
Expand Down Expand Up @@ -198,3 +188,111 @@ test('(#2476) Parametrized messages can be overwritten', async () => {
expect(await getText(page, '#module-layer-base-key')).toEqual('Layer base key overwritten!')
expect(await getText(page, '#module-layer-base-key-named')).toEqual('Layer base key overwritten, greetings bar!')
})

test('(#2338) should be extended API', async () => {
const { page } = await renderPage('/')

const globalData = await getData(page, '#global-scope-properties')
expect(globalData.code).toEqual('en')
const localeData = await getData(page, '#local-scope-properties')
expect(localeData.code).toEqual('en')
})

test('<NuxtLink> triggers runtime hooks', async () => {
const { page, consoleLogs } = await renderPage('/kr')

// click `fr` lang switch with `<NuxtLink>`
await page.locator('#nuxt-locale-link-fr').click()
await waitForURL(page, '/fr')

// click `kr` lang switch with `<NuxtLink>`
await page.locator('#nuxt-locale-link-kr').click()
await waitForURL(page, '/kr')

expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch kr fr true'))).toBeTruthy()
expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch fr kr false'))).toBeTruthy()
expect(consoleLogs.find(log => log.text.includes('onLanguageSwitched kr fr'))).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 waitForURL(page, '/fr/about')

// navigate to home page
await page.locator('#link-home').click()
await waitForURL(page, '/fr')
})

test('setLocale triggers runtime hooks', async () => {
const { page, consoleLogs } = await renderPage('/kr')

// click `fr` lang switch link
await page.locator('#set-locale-link-fr').click()

// click `kr` lang switch link
// Hook prevents locale change to `kr`, stays `fr`
await page.locator('#set-locale-link-kr').click()
expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch kr fr true'))).toBeTruthy()
expect(consoleLogs.find(log => log.text.includes('onLanguageSwitched kr fr'))).toBeTruthy()
expect(consoleLogs.find(log => log.text.includes('onBeforeLanguageSwitch fr kr false'))).toBeTruthy()

// current locale
expect(await getText(page, '#lang-switcher-current-locale code')).toEqual('fr')
})

test('render with meta components', async () => {
const { page } = await renderPage('/')

/**
* default locale
*/

// title tag
expect(await getText(page, 'title')).toMatch('Page - Homepage')
await waitForURL(page, '/')

// html tag `lang` attribute
expect(await page.getAttribute('html', 'lang')).toMatch('en')

// html tag `dir` attribute
expect(await page.getAttribute('html', 'dir')).toMatch('ltr')

// rendering link tag and meta tag in head tag
await assetLocaleHead(page, '#layout-use-locale-head')

/**
* change locale
*/

// click `fr` lang switch link
await page.locator('#nuxt-locale-link-fr').click()
await waitForURL(page, '/fr')

// title tag
expect(await getText(page, 'title')).toMatch('Page - Accueil')

// html tag `lang` attribute
expect(await page.getAttribute('html', 'lang')).toMatch('fr')

// rendering link tag and meta tag in head tag
await assetLocaleHead(page, '#layout-use-locale-head')

/**
* access to other page
*/

// click about page
await page.locator('#link-about').click()
await waitForURL(page, '/fr/about')

// title tag
expect(await getText(page, 'title')).toMatch('Page - À propos')

// html tag `lang` attribute
expect(await page.getAttribute('html', 'lang')).toMatch('fr')

// rendering link tag and meta tag in head tag
await assetLocaleHead(page, '#layout-use-locale-head')
})
60 changes: 0 additions & 60 deletions specs/different_domains.runtimeConfig.spec.ts

This file was deleted.

Loading

0 comments on commit 167a2c2

Please sign in to comment.