Files
vue-seo-layer/tests/labels.test.ts
Jakub Zych aa7480e003 feat: SEO labels, tokens and base styles
- useSeoLabels(): the L8 dictionary (en, pl) of the page's content locale
  (resolve locale, then html_lang, then en), overridable and extendable in
  app.config.ts under seo.labels; no i18n dependency
- seo-tokens.css (spacing, colour, small type, breadcrumb separator) and
  seo-base.css (.seo-visually-hidden, the focus ring of layer links), loaded
  before the app's CSS and styling only .seo-* classes
2026-11-04 09:27:57 +01:00

58 lines
2.6 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { SEO_LABELS, formatSeoLabel, pickLabels } from '../app/utils/labels.ts'
test('labels: en and pl carry every L8 key', () => {
const keys = (o: object, prefix = ''): string[] =>
Object.entries(o).flatMap(([k, v]) => (v && typeof v === 'object' ? keys(v, `${prefix}${k}.`) : [`${prefix}${k}`]))
const want = [
'breadcrumbs.label',
'pagination.label',
'pagination.previous',
'pagination.next',
'pagination.page',
'sitemap.title',
'sitemap.groupTitle',
'sitemap.pageSuffix',
'sitemap.all',
'sitemap.empty',
'sitemapLink',
'links.newTab',
].sort()
assert.deepEqual(keys(SEO_LABELS.en).sort(), want)
assert.deepEqual(keys(SEO_LABELS.pl).sort(), want)
assert.equal(SEO_LABELS.pl.breadcrumbs.label, 'Ścieżka nawigacji')
assert.equal(SEO_LABELS.en.links.newTab, '(opens in a new tab)')
})
test('pickLabels: the resolve locale first, then html_lang, then en', () => {
assert.equal(pickLabels('pl', 'en').pagination.next, 'Następna strona')
assert.equal(pickLabels('pl-PL', '').pagination.next, 'Następna strona')
assert.equal(pickLabels('', 'pl-PL').pagination.next, 'Następna strona')
assert.equal(pickLabels('de', 'pl').pagination.next, 'Następna strona')
assert.equal(pickLabels('de', 'fr').pagination.next, 'Next page')
assert.equal(pickLabels(undefined, undefined).sitemap.title, 'Sitemap')
})
test('pickLabels: app overrides change a key, add a locale and fill gaps from en', () => {
const overrides = { pl: { sitemap: { title: 'Spis stron' } }, de: { pagination: { next: 'Nächste Seite' } } }
const pl = pickLabels('pl', '', overrides)
assert.equal(pl.sitemap.title, 'Spis stron')
assert.equal(pl.sitemap.empty, 'Nie ma jeszcze stron do wyświetlenia.')
const de = pickLabels('de', '', overrides)
assert.equal(de.pagination.next, 'Nächste Seite')
assert.equal(de.pagination.previous, 'Previous page')
assert.equal(SEO_LABELS.pl.sitemap.title, 'Mapa strony', 'the built-in dictionary is not mutated')
})
test('pickLabels: a non-string override value is ignored', () => {
const l = pickLabels('en', '', { en: { sitemapLink: 42 as unknown as string, pagination: 'x' as unknown as object } })
assert.equal(l.sitemapLink, 'Sitemap')
assert.equal(l.pagination.label, 'Pagination')
})
test('formatSeoLabel: fills {name} placeholders and leaves unknown ones', () => {
assert.equal(formatSeoLabel(SEO_LABELS.pl.sitemap.all, { group: 'Wpisy', count: 12 }), 'Wpisy: wszystkie (12)')
assert.equal(formatSeoLabel('Page {page} of {pages}', { page: 2 }), 'Page 2 of {pages}')
})