summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/extensions/frontmatter_spec.js
blob: 9bd29070858cb685f4c74fb028d7c37af81903d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Frontmatter from '~/content_editor/extensions/frontmatter';
import CodeBlockHighlight from '~/content_editor/extensions/code_block_highlight';
import { createTestEditor, createDocBuilder, triggerNodeInputRule } from '../test_utils';

describe('content_editor/extensions/frontmatter', () => {
  let tiptapEditor;
  let doc;
  let frontmatter;
  let codeBlock;

  beforeEach(() => {
    tiptapEditor = createTestEditor({ extensions: [Frontmatter, CodeBlockHighlight] });

    ({
      builders: { doc, codeBlock, frontmatter },
    } = createDocBuilder({
      tiptapEditor,
      names: {
        frontmatter: { nodeType: Frontmatter.name },
        codeBlock: { nodeType: CodeBlockHighlight.name },
      },
    }));
  });

  it('inherits from code block highlight extension', () => {
    expect(Frontmatter.parent).toBe(CodeBlockHighlight);
  });

  it('does not insert a frontmatter block when executing code block input rule', () => {
    const expectedDoc = doc(codeBlock({ language: 'plaintext' }, ''));
    const inputRuleText = '``` ';

    triggerNodeInputRule({ tiptapEditor, inputRuleText });

    expect(tiptapEditor.getJSON()).toEqual(expectedDoc.toJSON());
  });

  it('sets isFrontmatter attribute to true by default', () => {
    expect(Frontmatter.config.addAttributes()).toEqual(
      expect.objectContaining({
        isFrontmatter: { default: true },
      }),
    );
  });

  it.each`
    command                | result                        | resultDesc
    ${'toggleCodeBlock'}   | ${() => doc(codeBlock(''))}   | ${'code block element'}
    ${'setCodeBlock'}      | ${() => doc(codeBlock(''))}   | ${'code block element'}
    ${'setFrontmatter'}    | ${() => doc(frontmatter(''))} | ${'frontmatter element'}
    ${'toggleFrontmatter'} | ${() => doc(frontmatter(''))} | ${'frontmatter element'}
  `('executing $command should generate a document with a $resultDesc', ({ command, result }) => {
    const expectedDoc = result();

    tiptapEditor.commands[command]();

    expect(tiptapEditor.getJSON()).toEqual(expectedDoc.toJSON());
  });
});