summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/extensions/heading_spec.js
blob: 2fa25e03cdc380d08baf10d22e9229cd6251c2f8 (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
import Heading from '~/content_editor/extensions/heading';
import { createTestEditor, createDocBuilder, triggerNodeInputRule } from '../test_utils';

describe('content_editor/extensions/heading', () => {
  let tiptapEditor;
  let doc;
  let p;
  let heading;

  beforeEach(() => {
    tiptapEditor = createTestEditor({ extensions: [Heading] });
    ({
      builders: { doc, p, heading },
    } = createDocBuilder({
      tiptapEditor,
      names: {
        heading: { nodeType: Heading.name },
      },
    }));
  });

  describe('when typing a valid heading input rule', () => {
    it.each`
      level | inputRuleText
      ${1}  | ${'# '}
      ${2}  | ${'## '}
      ${3}  | ${'### '}
      ${4}  | ${'#### '}
      ${5}  | ${'##### '}
      ${6}  | ${'###### '}
    `('inserts a heading node for $inputRuleText', ({ level, inputRuleText }) => {
      const expectedDoc = doc(heading({ level }));

      triggerNodeInputRule({ tiptapEditor, inputRuleText });

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

  describe('when typing a invalid heading input rule', () => {
    it.each`
      inputRuleText
      ${'#hi'}
      ${'#\n'}
    `('does not insert a heading node for $inputRuleText', ({ inputRuleText }) => {
      const expectedDoc = doc(p());

      triggerNodeInputRule({ tiptapEditor, inputRuleText });

      // no change to the document
      expect(tiptapEditor.getJSON()).toEqual(expectedDoc.toJSON());
    });
  });
});