import { describe, expect, it } from 'vitest'; import { DEFAULT_LOCALE, SUPPORTED_LOCALES } from './api'; import { homeHref, listingAlternates, listingPath, resolveRoute, SECTIONS, technologyAlternates, technologyPath, } from './routes'; describe('resolveRoute', () => { it('reads a page at the root as the default language', () => { expect(resolveRoute('/sobre-nosotros')).toEqual({ type: 'page', locale: DEFAULT_LOCALE, slug: 'sobre-nosotros', }); }); it('reads a prefixed page in the language it names', () => { expect(resolveRoute('/en/about-us')).toEqual({ type: 'page', locale: 'en', slug: 'about-us', }); expect(resolveRoute('/ca/qui-som/')).toEqual({ type: 'page', locale: 'ca', slug: 'qui-som', }); }); it('reads a record under its section, in each language', () => { expect(resolveRoute('/proyectos/catalogo')).toEqual({ type: 'project', locale: 'es', slug: 'catalogo', }); expect(resolveRoute('/en/projects/catalogo')).toEqual({ type: 'project', locale: 'en', slug: 'catalogo', }); expect(resolveRoute('/ca/equip/ada')).toEqual({ type: 'member', locale: 'ca', slug: 'ada', }); expect(resolveRoute('/ca/col-labora/frontend')).toEqual({ type: 'call', locale: 'ca', slug: 'frontend', }); }); // A section only means something in its own language: `/projects/x` is not // an address the API hands out for Spanish. it('refuses a section spelled in another language', () => { expect(resolveRoute('/projects/catalogo')).toBeNull(); expect(resolveRoute('/en/proyectos/catalogo')).toBeNull(); }); // The API never spells the default language with a prefix, so one record // has one address rather than two. it('refuses the default language as a prefix', () => { expect(resolveRoute('/es/sobre-nosotros')).toBeNull(); expect(resolveRoute('/es/proyectos/catalogo')).toBeNull(); }); it('refuses what is not shaped like a record', () => { expect(resolveRoute('/')).toBeNull(); expect(resolveRoute('/fr/a-propos')).toBeNull(); expect(resolveRoute('/proyectos/catalogo/extra')).toBeNull(); }); it('knows every section in every language', () => { for (const sections of Object.values(SECTIONS)) { expect(Object.keys(sections).sort()).toEqual( [...SUPPORTED_LOCALES].sort(), ); } }); }); describe('listings', () => { it('reads the radar in every language, before any page of that slug', () => { expect(resolveRoute('/radar')).toEqual({ type: 'radar', locale: 'es', slug: '', }); expect(resolveRoute('/ca/radar')).toEqual({ type: 'radar', locale: 'ca', slug: '', }); }); it('builds the address resolveRoute reads back', () => { for (const locale of SUPPORTED_LOCALES) { expect(resolveRoute(listingPath('radar', locale))?.type).toBe('radar'); } expect(listingAlternates('radar')).toEqual({ es: '/radar', en: '/en/radar', ca: '/ca/radar', }); }); // The calls' section with nothing after it is the list of ways to help. it('reads a call section on its own as the contribute listing', () => { expect(resolveRoute('/colabora')).toEqual({ type: 'contribute', locale: 'es', slug: '', }); expect(resolveRoute('/en/contribute')?.type).toBe('contribute'); expect(resolveRoute('/en/contribute/frontend')?.type).toBe('call'); }); }); describe('technologies', () => { it('reads one under its section, in each language', () => { expect(resolveRoute('/tecnologias/angular')).toEqual({ type: 'technology', locale: 'es', slug: 'angular', }); expect(resolveRoute('/en/technologies/angular')).toEqual({ type: 'technology', locale: 'en', slug: 'angular', }); expect(resolveRoute('/ca/tecnologies/angular')).toEqual({ type: 'technology', locale: 'ca', slug: 'angular', }); }); it('builds the address resolveRoute reads back', () => { for (const locale of SUPPORTED_LOCALES) { const path = technologyPath('angular', locale); expect(resolveRoute(path)).toEqual({ type: 'technology', locale, slug: 'angular', }); } }); // The slug is a proper noun the API does not translate, so the three // addresses differ only by the prefix. it('keeps the same slug in every language', () => { expect(technologyAlternates('angular')).toEqual({ es: '/tecnologias/angular', en: '/en/technologies/angular', ca: '/ca/tecnologies/angular', }); }); }); describe('homeHref', () => { it('answers on ?lang= for every language but the default', () => { expect(homeHref(DEFAULT_LOCALE)).toBe('/'); expect(homeHref('ca')).toBe('/?lang=ca'); }); });