Files
vue-faq-layer/tests/faqSchema.test.ts
Jakub Zych f7ec4fe56f feat: FAQPage node and widget headings
- FaqBlock prop schema (default true) adds a FAQPage node (@id
  <page URL>#faq, one Question with its acceptedAnswer as plain text per
  item) to the page's one JSON-LD graph through vue-seo-layer's
  useSchemaNode (faqSchema, faqAnswerText)
- headingLevel 0 renders the title as <p class="faq-block__title"> with
  the same id, still labelling the section, for sidebar and footer
  widgets
- smoke: count= and repeatable options; expectations for the widget
  title, one FAQPage node and the answers in the HTML inside details
2026-11-04 14:02:05 +01:00

44 lines
2.1 KiB
TypeScript

// The FAQPage node of a FAQ block (D-43): one Question per item with the
// answer as plain text, @id <page URL>#faq.
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { faqAnswerText, faqSchema } from '../app/utils/faqSchema.ts'
const FAQ = {
name: 'Acme questions',
slug: 'acme',
locale: 'en',
description: '<p>About Acme.</p>',
content: [
{ question: 'How long does shipping take?', answer: '<p>Two days.</p><ul><li>Poland</li><li>Europe</li></ul>' },
{ question: 'Can I return it?', answer: '<p>Yes &amp; <a href="/returns">free</a>&nbsp;of charge &lt;30 days&gt;.</p>' },
],
}
test('faqSchema: a FAQPage with a Question and acceptedAnswer per item', () => {
assert.deepEqual(faqSchema(FAQ, 'https://acme.test/help'), {
'@type': 'FAQPage',
'@id': 'https://acme.test/help#faq',
url: 'https://acme.test/help',
mainEntity: [
{ '@type': 'Question', name: 'How long does shipping take?', acceptedAnswer: { '@type': 'Answer', text: 'Two days. Poland Europe' } },
{ '@type': 'Question', name: 'Can I return it?', acceptedAnswer: { '@type': 'Answer', text: 'Yes & free of charge <30 days>.' } },
],
})
})
test('faqSchema: no FAQ, no items or blank questions give null or skip the item', () => {
assert.equal(faqSchema(null, 'https://acme.test/'), null)
assert.equal(faqSchema({ ...FAQ, content: [] }, 'https://acme.test/'), null)
const node = faqSchema({ ...FAQ, content: [{ question: ' ', answer: '<p>x</p>' }, { question: 'Q', answer: '' }] }, 'https://acme.test/x#top')
assert.equal(node?.['@id'], 'https://acme.test/x#faq')
assert.deepEqual(node?.mainEntity, [{ '@type': 'Question', name: 'Q', acceptedAnswer: { '@type': 'Answer', text: '' } }])
})
test('faqAnswerText: markup stripped, entities decoded, scripts and styles dropped, whitespace collapsed', () => {
assert.equal(faqAnswerText('<p>a</p>\n<p>b<br>c</p>'), 'a b c')
assert.equal(faqAnswerText('<script>alert(1)</script><style>p{}</style><p>ok</p>'), 'ok')
assert.equal(faqAnswerText('&quot;x&quot; &#39;y&#39; &#x41; &#66;'), '"x" \'y\' A B')
assert.equal(faqAnswerText(null), '')
})