56 行
2.5 KiB
TypeScript
56 行
2.5 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { isLocaleCode,
|
|
legacyLocalePath,
|
|
localeFromPath,
|
|
localiseTo,
|
|
switchLocalePath,
|
|
unlocalisedPath } from '@/lib/locales'
|
|
|
|
|
|
describe ('locale URL generation', () => {
|
|
it.each (['ja', 'ain-Kana']) ('recognises the supported locale %s', locale => {
|
|
expect (isLocaleCode (locale)).toBe (true)
|
|
expect (localeFromPath (`/${ locale }/posts`)).toBe (locale)
|
|
})
|
|
|
|
it.each (['en', 'ain', 'JA', '', undefined]) ('rejects %s', locale => {
|
|
expect (isLocaleCode (locale)).toBe (false)
|
|
})
|
|
|
|
it.each ([
|
|
'/posts', '/posts/new', '/posts/search', '/posts/42', '/posts/changes',
|
|
'/tags', '/tags/42', '/tags/42/deerjikists', '/tags/nico', '/nico/tags', '/tags/changes',
|
|
'/materials', '/materials/new', '/materials/42', '/materials/changes',
|
|
'/materials/suppressions', '/theatres/42', '/wiki', '/wiki/new', '/wiki/title',
|
|
'/wiki/42/edit', '/wiki/42/diff', '/wiki/changes', '/help', '/help/changes',
|
|
'/help/article', '/help/article/diff', '/users/settings', '/settings',
|
|
'/tos', '/gekanator', '/more']) ('localises and redirects %s', pathname => {
|
|
const location = { pathname, search: '?page=2&tag=a%23b', hash: '#section' }
|
|
expect (localiseTo (pathname, 'ain-Kana')).toBe (`/ain-Kana${ pathname }`)
|
|
expect (legacyLocalePath (location, () => true)).toBe (`/ja${ pathname }${ location.search }#section`)
|
|
expect (unlocalisedPath (`/ain-Kana${ pathname }`)).toBe (pathname)
|
|
})
|
|
|
|
it ('preserves the resource, query and fragment on locale changes', () => {
|
|
expect (switchLocalePath ({ pathname: '/ja/tags/42', search: '?x=1&x=2', hash: '#history' },
|
|
'ain-Kana')).toBe ('/ain-Kana/tags/42?x=1&x=2#history')
|
|
})
|
|
|
|
it ('preserves explicit locales and relative, fragment and external targets', () => {
|
|
for (const target of ['/ja/posts', '/ain-Kana/posts', '?page=2', '#body',
|
|
'../changes', 'https://example.com/posts', '//example.com/posts'])
|
|
expect (localiseTo (target, 'ain-Kana')).toBe (target)
|
|
})
|
|
|
|
it ('preserves structured targets', () => {
|
|
expect (localiseTo ({ pathname: '/tags/42', search: '?q=one', hash: '#two' }, 'ain-Kana'))
|
|
.toEqual ({ pathname: '/ain-Kana/tags/42', search: '?q=one', hash: '#two' })
|
|
})
|
|
|
|
it.each (['/en/posts', '/ain/posts', '/unknown', '/api/tags', '/assets/image.png']) (
|
|
'does not redirect invalid or non-page paths: %s', pathname => {
|
|
expect (legacyLocalePath ({ pathname, search: '', hash: '' }, () => false)).toBeNull ()
|
|
})
|
|
})
|