Files
carmanagement/apps/homepage/tests/unit/localization.test.ts
T
root 781512399b
Build & Deploy / Build & Push Docker Image (push) Successful in 2m51s
Test / Type Check (all packages) (push) Successful in 52s
Build & Deploy / Deploy to VPS (push) Successful in 4s
Test / API Unit Tests (push) Successful in 49s
Test / Homepage Unit Tests (push) Successful in 44s
Test / Storefront Unit Tests (push) Successful in 39s
Test / Admin Unit Tests (push) Successful in 40s
Test / Dashboard Unit Tests (push) Successful in 40s
Test / API Integration Tests (push) Successful in 1m0s
Update homepage routes to include language and mode
2026-07-02 14:10:17 -04:00

64 lines
2.4 KiB
TypeScript

import {
getDirection,
hreflangMap,
isLocale,
localeFromAcceptLanguage,
localeSwitchUrl,
localizedPath,
routeById,
routeIdFromAnyLocaleSlug,
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/system');
expect(localizedPath('privacy', 'fr')).toBe('/fr/system/confidentialite');
expect(localizedPath('sign-in', 'ar')).toBe('/ar/system/تسجيل-الدخول');
expect(routeIdFromSlug('fr', 'confidentialite')).toBe('privacy');
expect(routeIdFromSlug('fr', 'privacy')).toBeNull();
expect(routeIdFromAnyLocaleSlug('privacy')).toBe('privacy');
expect(routeIdFromAnyLocaleSlug('confidentialite')).toBe('privacy');
expect(routeIdFromAnyLocaleSlug('unknown')).toBeNull();
expect(
routeIdFromPathname('/ar/system/%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/system');
expect(links.ar).toBe('https://approved.test/ar/system');
expect(links['x-default']).toBe('https://approved.test/en/system');
});
it('preserves only approved campaign query values and valid hashes', () => {
const current = new URL(
'https://approved.test/en/system?utm_source=campaign&email=private%40approved.test#gibberish',
);
const switched = localeSwitchUrl(current, 'fr', 'home');
expect(switched.pathname).toBe('/fr/system');
expect(switched.search).toBe('?utm_source=campaign');
expect(switched.hash).toBe('');
current.hash = '#workflow';
expect(localeSwitchUrl(current, 'ar', 'home').hash).toBe('#workflow');
});
});