Files
AndreaSlopFotografie/web/src/i18n/i18n.test.ts
T
SluijsensandClaude Sonnet 5.5 ad0b114828 Hero from the CMS: markers HERO-HTML and HERO-PRELOAD, hero key in the data block, picture per ratio, baked fallback
The prerendered hero is plain HTML between the markers (first photo, h1, buttons), the slider takes over after hydration with the hero of the host block, the API or the build. The head preload is added to the finished HTML. Hero texts move into nl.json. The stand-in host and the e2e tests cover the hero.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 16:48:00 +01:00

162 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { checkFilled, compareMessages, leaves, placeholders } from './compare';
import { fill, locale, locales, messages, nl, t, translator } from './index';
describe('t', () => {
it('gives the text of a key', () => {
expect(t('common.aria.backToTop')).toBe('Terug naar boven');
});
it('is the language of this build', () => {
expect(locale).toBe('nl');
expect(messages).toBe(nl);
});
it('fails for a key that does not exist, with the key in the message', () => {
expect(() => t('common.aria.bestaatNiet' as never)).toThrow(
'Missing text: common.aria.bestaatNiet',
);
expect(() => t('common.aria.backToTop.x' as never)).toThrow(
'Missing text',
);
expect(() => t('common' as never)).toThrow('Not a text: common');
});
});
// A tree of its own, to try parameters, plurals, lists and arrays
const tree = {
greeting: 'Hallo {name}, je hebt {count} berichten',
photos: { one: '{count} foto', other: '{count} foto’s' },
steps: ['Eerste', 'Tweede'],
faq: [{ question: 'Hoe?' }, { question: 'Waarom?' }],
mixed: ['Eerste', 3],
};
const own = translator(tree, 'nl');
describe('translator', () => {
it('fills {parameters}', () => {
expect(own.t('greeting', { name: 'Eva', count: 2 })).toBe(
'Hallo Eva, je hebt 2 berichten',
);
});
it('fails for a parameter that is not given', () => {
expect(() => own.t('greeting', { name: 'Eva' })).toThrow(
'Missing parameter {count}',
);
expect(() => own.t('greeting')).toThrow('Missing parameter {name}');
});
it('reaches texts inside lists by position', () => {
expect(own.t('faq.1.question')).toBe('Waarom?');
expect(own.t('steps.0')).toBe('Eerste');
});
it('gives a list of texts', () => {
expect(own.tList('steps')).toEqual(['Eerste', 'Tweede']);
expect(() => own.tList('mixed' as never)).toThrow(
'Not a list of texts',
);
expect(() => own.tList('greeting' as never)).toThrow(
'Not a list of texts',
);
});
it('chooses the singular or the plural form for the language', () => {
expect(own.tPlural('photos', 1)).toBe('1 foto');
expect(own.tPlural('photos', 0)).toBe('0 foto’s');
expect(own.tPlural('photos', 2)).toBe('2 foto’s');
expect(own.tPlural('photos', 21, { name: 'x' })).toBe('21 foto’s');
});
it('follows the plural rules of another language', () => {
// Polish: one, few and many forms; only "one" has its own text here
const pl = translator(tree, 'pl');
expect(pl.tPlural('photos', 1)).toBe('1 foto');
expect(pl.tPlural('photos', 5)).toBe('5 foto’s');
});
});
describe('fill', () => {
it('leaves a text without parameters as it is', () => {
expect(fill('Gewoon {{ tekst')).toBe('Gewoon {{ tekst');
});
});
describe('nl.json', () => {
it('has one block per area, in the order of the site', () => {
expect(Object.keys(nl)).toEqual([
'common',
'site',
'hero',
'home',
'about',
'contact',
'shoots',
'portfolio',
'instagram',
'testimonials',
'seo',
'cmsClient',
]);
});
});
describe('the texts of the site', () => {
it('are all filled, without TODO and with valid {parameters}', () => {
expect(checkFilled(nl)).toEqual([]);
expect(leaves(nl).size).toBeGreaterThan(0);
});
it.each(Object.keys(locales))(
'language %s has the same keys and parameters as Dutch',
(name) => {
expect(
compareMessages(nl, locales[name as keyof typeof locales]),
).toEqual([]);
},
);
});
describe('compareMessages and checkFilled', () => {
const base = { a: 'Hallo {name}', b: ['een', 'twee'], c: { d: 'x' } };
it('finds missing keys, extra keys and different parameters', () => {
const other = { a: 'Hello', b: ['one'], c: { d: 'x', e: 'extra' } };
expect(compareMessages(base, other)).toEqual([
{ key: 'a', problem: 'different {parameters}' },
{ key: 'b.1', problem: 'missing' },
{ key: 'c.e', problem: 'not in the base' },
]);
});
it('accepts parameters in another order', () => {
expect(
compareMessages({ a: '{x} en {y}' }, { a: '{y} and {x}' }),
).toEqual([]);
});
it('finds empty texts, TODO and broken braces', () => {
expect(
checkFilled({
a: ' ',
b: 'TODO vertalen',
c: 'kapot {naam',
d: 'ok',
}),
).toEqual([
{ key: 'a', problem: 'empty' },
{ key: 'b', problem: 'TODO' },
{ key: 'c', problem: 'invalid {parameter}' },
]);
});
it('lists the parameters of a text', () => {
expect(placeholders('{b} {a} {b}')).toEqual(['a', 'b', 'b']);
});
it('collects texts of numbers, booleans and null as nothing', () => {
expect(leaves({ a: 1, b: true, c: null, d: 'x' }).size).toBe(1);
});
});