59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import {
|
|
getDirection,
|
|
hreflangMap,
|
|
isLocale,
|
|
localeFromAcceptLanguage,
|
|
localeSwitchUrl,
|
|
localizedPath,
|
|
routeById,
|
|
routeIdFromPathname,
|
|
routeIdFromSlug,
|
|
} from '@/lib/localization/config';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
describe('localization foundations', () => {
|
|
it('selects a supported locale by quality', () => {
|
|
expect(localeFromAcceptLanguage('de-DE;q=0.9, ar;q=0.8, fr;q=0.7')).toBe('ar');
|
|
expect(localeFromAcceptLanguage('fr-CA, en;q=0.8')).toBe('fr');
|
|
expect(localeFromAcceptLanguage(null)).toBe('en');
|
|
});
|
|
|
|
it('validates locale direction and route lookup', () => {
|
|
expect(isLocale('fr')).toBe(true);
|
|
expect(isLocale('de')).toBe(false);
|
|
expect(getDirection('ar')).toBe('rtl');
|
|
expect(routeById('home').id).toBe('home');
|
|
});
|
|
|
|
it('generates and resolves localized stable route paths', () => {
|
|
expect(localizedPath('home', 'ar')).toBe('/ar');
|
|
expect(localizedPath('privacy', 'fr')).toBe('/fr/confidentialite');
|
|
expect(localizedPath('login', 'ar')).toBe('/ar/تسجيل-الدخول');
|
|
expect(routeIdFromSlug('fr', 'confidentialite')).toBe('privacy');
|
|
expect(routeIdFromPathname('/ar/%D8%A7%D9%84%D8%AE%D8%B5%D9%88%D8%B5%D9%8A%D8%A9')).toBe(
|
|
'privacy',
|
|
);
|
|
expect(routeIdFromPathname('/de')).toBeNull();
|
|
});
|
|
|
|
it('generates reciprocal hreflang values', () => {
|
|
const links = hreflangMap(new URL('https://approved.test'), 'home');
|
|
expect(links.en).toBe('https://approved.test/en');
|
|
expect(links.ar).toBe('https://approved.test/ar');
|
|
expect(links['x-default']).toBe('https://approved.test/en');
|
|
});
|
|
|
|
it('preserves only approved campaign query values and valid hashes', () => {
|
|
const current = new URL(
|
|
'https://approved.test/en?utm_source=campaign&email=private%40approved.test#gibberish',
|
|
);
|
|
const switched = localeSwitchUrl(current, 'fr', 'home');
|
|
expect(switched.pathname).toBe('/fr');
|
|
expect(switched.search).toBe('?utm_source=campaign');
|
|
expect(switched.hash).toBe('');
|
|
|
|
current.hash = '#workflow';
|
|
expect(localeSwitchUrl(current, 'ar', 'home').hash).toBe('#workflow');
|
|
});
|
|
});
|