Skip to content

Commit

Permalink
test(pages): add tests for no_prefix strategy and localized paths
Browse files Browse the repository at this point in the history
Add tests to verify the functionality of the `no_prefix` strategy
within the `PageManager`, ensuring correct route handling for
localized and unlocalized pages. Also include tests for extracting
localized paths to validate that the application correctly handles
localization for multiple languages.
  • Loading branch information
s00d committed Jan 16, 2025
1 parent 153a9be commit c8cf997
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/fixtures/strategy/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
</p>

<i18n-link
id="activity"
id="contact"
:to="{ name: 'contact' }"
>
Contact
</i18n-link>

<i18n-link
id="activity"
id="about"
:to="{ name: 'about' }"
>
About
Expand Down
12 changes: 12 additions & 0 deletions test/no-prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ test.describe('no_prefix', () => {
// Check the initial text for the default locale
await expect(page.locator('#content')).toHaveText('en')

await page.click('#contact') // Assuming the link to the Contact page has id 'contact'

// Check the URL for the Contact page in German
await expect(page).toHaveURL('/contact')

await page.goBack()

// Click on the language switcher to show language options
await page.click('.language-switcher') // Assuming the switcher has a class 'language-switcher'

Expand All @@ -42,6 +49,11 @@ test.describe('no_prefix', () => {

await expect(page.locator('#content')).toHaveText('de')

await page.click('#contact')

// Check the URL for the Contact page in English
await expect(page).toHaveURL('/kontakt')

await goto('/', { waitUntil: 'hydration' })

await expect(page).toHaveURL('/')
Expand Down
72 changes: 72 additions & 0 deletions test/pages-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { fileURLToPath } from 'node:url'
import path from 'node:path'
import { expect, test } from '@nuxt/test-utils/playwright'
import type { NuxtPage } from '@nuxt/schema'
import type { Locale } from '~/src/types'
import { PageManager } from '~/src/page-manager'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

test.use({
nuxt: {
rootDir: fileURLToPath(new URL('./fixtures/undefault', import.meta.url)),
Expand Down Expand Up @@ -274,4 +277,73 @@ test.describe('PageManager', () => {
]),
)
})

test('should handle no_prefix strategy correctly', async () => {
const globalLocaleRoutes = {
activity: {
en: '/custom-activity-en',
de: '/custom-activity-de',
ru: '/custom-activity-ru',
},
unlocalized: false, // Unlocalized page should not be localized
}
const pageManagerPrefixAndDefault = new PageManager(locales, defaultLocaleCode, 'no_prefix', globalLocaleRoutes)

const pages: NuxtPage[] = [
{
path: '/activity',
name: 'activity',
children: [{ path: 'skiing', name: 'Skiing' }],
},
]

// Extend pages
pageManagerPrefixAndDefault.extendPages(pages)

expect(pages).toHaveLength(4) // Routes for default and non-default locales

// Check default locale route
expect(pages[0].path).toBe('/activity')
expect(pages[1].path).toBe('/custom-activity-en')
expect(pages[2].path).toBe('/custom-activity-de')
expect(pages[3].path).toBe('/custom-activity-ru')
})

test('extractLocalizedPaths should extract localized paths correctly', async () => {
const fixturesDir = path.join(__dirname, 'fixtures/strategy/pages')

const mockPages: NuxtPage[] = [
{
path: '/about',
file: path.join(fixturesDir, 'about.vue'),
children: [],
},
{
path: '/contact',
file: path.join(fixturesDir, 'contact.vue'),
children: [],
},
]

const locales = [
{ code: 'en' },
{ code: 'fr' },
]
const pageManager = new PageManager(locales, 'en', 'no_prefix', {})

// Вызываем метод extractLocalizedPaths
const localizedPaths = pageManager.extractLocalizedPaths(mockPages)

// Проверяем результат
expect(localizedPaths).toEqual({
'/about': {
en: '/about',
de: '/a-propos',
},
'/contact': {
en: '/contact',
de: '/kontakt',
},
})
})
})
12 changes: 12 additions & 0 deletions test/prefix-and-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ test.describe('prefix_and_default', () => {
// Check the initial text for the default locale
await expect(page.locator('#content')).toHaveText('en')

await page.click('#contact') // Assuming the link to the Contact page has id 'contact'

// Check the URL for the Contact page in German
await expect(page).toHaveURL('/en/contact')

await page.goBack()

await goto('/en', { waitUntil: 'hydration' })

// Ensure the URL contains the default locale
Expand All @@ -46,6 +53,11 @@ test.describe('prefix_and_default', () => {

await expect(page.locator('#content')).toHaveText('de')

await page.click('#contact')

// Check the URL for the Contact page in English
await expect(page).toHaveURL('/de/kontakt')

await goto('/de', { waitUntil: 'hydration' })
await expect(page).toHaveURL('/de')
})
Expand Down
12 changes: 12 additions & 0 deletions test/prefix-except-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ test.describe('prefix_except_default', () => {
// Check the initial text for the default locale
await expect(page.locator('#content')).toHaveText('en')

await page.click('#contact') // Assuming the link to the Contact page has id 'contact'

// Check the URL for the Contact page in German
await expect(page).toHaveURL('/contact')

await page.goBack()

// Click on the language switcher to show language options
await page.click('.language-switcher') // Assuming the switcher has a class 'language-switcher'

Expand All @@ -38,6 +45,11 @@ test.describe('prefix_except_default', () => {

await expect(page.locator('#content')).toHaveText('de')

await page.click('#contact')

// Check the URL for the Contact page in English
await expect(page).toHaveURL('/de/kontakt')

const response = await goto('/en', { waitUntil: 'networkidle' })
expect(response?.status()).toBe(404)

Expand Down
12 changes: 12 additions & 0 deletions test/prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ test.describe('prefix_and_default', () => {
// Check the initial text for the default locale
await expect(page.locator('#content')).toHaveText('en')

await page.click('#contact') // Assuming the link to the Contact page has id 'contact'

// Check the URL for the Contact page in German
await expect(page).toHaveURL('/en/contact')

await page.goBack()

// Click on the language switcher to show language options
await page.click('.language-switcher') // Assuming the switcher has a class 'language-switcher'

Expand All @@ -38,6 +45,11 @@ test.describe('prefix_and_default', () => {

await expect(page.locator('#content')).toHaveText('de')

await page.click('#contact')

// Check the URL for the Contact page in English
await expect(page).toHaveURL('/de/kontakt')

await goto('/en', { waitUntil: 'hydration' })
await expect(page).toHaveURL('/en')

Expand Down

0 comments on commit c8cf997

Please sign in to comment.