From 9e66cbed4217c5f3e7381220ff715ed60ddf3678 Mon Sep 17 00:00:00 2001 From: Jakub Zych Date: Wed, 4 Nov 2026 19:48:57 +0100 Subject: [PATCH] test: the post title element for every postTitleTag and H1 setting auto follows content_h1_to_h2 (p only when it is false, h1 when the key is absent), h1 and p force the element, any other option value is auto. --- tests/postTitle.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 tests/postTitle.test.ts diff --git a/tests/postTitle.test.ts b/tests/postTitle.test.ts new file mode 100644 index 0000000..f34285d --- /dev/null +++ b/tests/postTitle.test.ts @@ -0,0 +1,38 @@ +// The post title's element (UI-SPEC L6, D-50): journal.postTitleTag x the +// Journal H1 setting (content_h1_to_h2 on the post answer), so a post page +// keeps exactly one H1. 'auto' (and any other value, as the module reads +// the option) follows the setting: h1 while body H1s become H2s or when +// the answer does not carry the key (a backend before the setting), p +// while the body keeps its own H1; 'h1' and 'p' force the element. +import assert from 'node:assert/strict' +import test from 'node:test' +import { journalPostTitleTag } from '../app/utils/seo.ts' +import type { JournalPost } from '../app/utils/types.ts' + +const settings = [true, false, undefined, null] as const + +test('auto follows content_h1_to_h2: p only when it is false', () => { + const want = { true: 'h1', false: 'p', undefined: 'h1', null: 'h1' } as const + for (const s of settings) assert.equal(journalPostTitleTag('auto', s), want[String(s) as keyof typeof want], `auto x ${s}`) +}) + +test('h1 and p force the element whatever the setting', () => { + for (const s of settings) { + assert.equal(journalPostTitleTag('h1', s), 'h1', `h1 x ${s}`) + assert.equal(journalPostTitleTag('p', s), 'p', `p x ${s}`) + } +}) + +test('any other option value is auto', () => { + for (const option of [undefined, null, '', 'H1', 'h2', 'span', 1, true, {}]) { + assert.equal(journalPostTitleTag(option, true), 'h1', `${JSON.stringify(option)} x true`) + assert.equal(journalPostTitleTag(option, false), 'p', `${JSON.stringify(option)} x false`) + } +}) + +test('an answer without the key renders the title as the H1', () => { + const post = { title: 'Chaga', slug: 'chaga' } as Partial + assert.equal(journalPostTitleTag('auto', (post as { content_h1_to_h2?: boolean }).content_h1_to_h2), 'h1') + const off = { ...post, content_h1_to_h2: false } as Partial + assert.equal(journalPostTitleTag('auto', (off as { content_h1_to_h2?: boolean }).content_h1_to_h2), 'p') +})