/* eslint-env jest */
import { addFaqToHtml, mergeNewFaqs } from '../faq-merge';
const block = (name: string, attributes: Record = {}) => ({
name,
attributes,
});
describe('a FAQ added to a page that has one', () => {
it('joins the FAQ the page has, without asking twice', () => {
const own = block('recomaze/faq', {
questions: [{ question: 'Is it unisex?', answer: 'Yes.' }],
});
const heading = block('core/heading', { content: 'FAQ' });
const added = block('recomaze/faq', {
questions: [
{ question: 'is it UNISEX?', answer: 'Yes, it is.' },
{ question: 'How do I wash it?', answer: 'Cold.' },
],
});
const blocks = [block('core/paragraph'), own, heading, added];
const merged = mergeNewFaqs(blocks, new Set([heading, added]));
expect(merged.map(item => item.name)).toEqual([
'core/paragraph',
'recomaze/faq',
]);
expect(
(merged[1].attributes.questions as { question: string }[]).map(
pair => pair.question
)
).toEqual(['Is it unisex?', 'How do I wash it?']);
});
it('stays a block of its own when the page has no FAQ yet', () => {
const heading = block('core/heading', { content: 'FAQ' });
const added = block('recomaze/faq', { questions: [] });
const blocks = [block('core/paragraph'), heading, added];
expect(mergeNewFaqs(blocks, new Set([heading, added]))).toBe(blocks);
});
});
describe('a FAQ added to HTML content', () => {
const written = [
{ question: 'Is it UNISEX?', answer: 'Yes, it is.' },
{ question: 'How do I wash it?', answer: 'Cold & slow.' },
];
it('joins the FAQ section the page has, without asking twice', () => {
const content =
'A warm hoodie.
\n';
const html = addFaqToHtml(content, written);
expect(html.match(/section-faq/g)).toHaveLength(1);
expect(html).toContain('Is it unisex?
Yes.
');
expect(html).toContain('How do I wash it?
Cold & slow.
');
expect(html.match(/unisex/gi)).toHaveLength(1);
expect(html.startsWith('A warm hoodie.
')).toBe(true);
});
it('adds a section at the end of a page without one', () => {
const html = addFaqToHtml('A warm hoodie.
', written);
expect(html).toBe(
'A warm hoodie.
\nFAQ
Is it UNISEX?
Yes, it is.
How do I wash it?
Cold & slow.
'
);
});
it('leaves the content alone when every question is asked already', () => {
const content =
'Is it unisex?
Yes.
How do I wash it?
Cold.
';
expect(addFaqToHtml(content, written)).toBe(content);
});
it('joins questions written by hand under a FAQ heading', () => {
const content =
'Intro.
Frequently asked questions
Is it unisex?
Yes.
Care
Wash cold.
';
const html = addFaqToHtml(content, written);
expect(html).not.toContain('section-faq');
expect(html.match(/unisex/gi)).toHaveLength(1);
expect(html).toContain(
'Is it unisex?
Yes.
How do I wash it?
Cold & slow.
Care
'
);
});
});