summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/extensions/details_spec.js
blob: cd59943982f3690362541c252dc05f96a83931e8 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import Details from '~/content_editor/extensions/details';
import DetailsContent from '~/content_editor/extensions/details_content';
import { createTestEditor, createDocBuilder } from '../test_utils';

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

  beforeEach(() => {
    tiptapEditor = createTestEditor({ extensions: [Details, DetailsContent] });

    ({
      builders: { doc, p, details, detailsContent },
    } = createDocBuilder({
      tiptapEditor,
      names: {
        details: { nodeType: Details.name },
        detailsContent: { nodeType: DetailsContent.name },
      },
    }));
  });

  describe('setDetails command', () => {
    describe('when current block is a paragraph', () => {
      it('converts current paragraph into a details block', () => {
        const initialDoc = doc(p('Text content'));
        const expectedDoc = doc(details(detailsContent(p('Text content'))));

        tiptapEditor.commands.setContent(initialDoc.toJSON());
        tiptapEditor.commands.setDetails();

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

    describe('when current block is a details block', () => {
      it('maintains the same document structure', () => {
        const initialDoc = doc(details(detailsContent(p('Text content'))));

        tiptapEditor.commands.setContent(initialDoc.toJSON());
        tiptapEditor.commands.setDetails();

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

  describe('toggleDetails command', () => {
    describe('when current block is a paragraph', () => {
      it('converts current paragraph into a details block', () => {
        const initialDoc = doc(p('Text content'));
        const expectedDoc = doc(details(detailsContent(p('Text content'))));

        tiptapEditor.commands.setContent(initialDoc.toJSON());
        tiptapEditor.commands.toggleDetails();

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

    describe('when current block is a details block', () => {
      it('convert details block into a paragraph', () => {
        const initialDoc = doc(details(detailsContent(p('Text content'))));
        const expectedDoc = doc(p('Text content'));

        tiptapEditor.commands.setContent(initialDoc.toJSON());
        tiptapEditor.commands.toggleDetails();

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

  it.each`
    input          | insertedNode
    ${'<details>'} | ${(...args) => details(detailsContent(p(...args)))}
    ${'<details'}  | ${(...args) => p(...args)}
    ${'details>'}  | ${(...args) => p(...args)}
  `('with input=$input, then should insert a $insertedNode', ({ input, insertedNode }) => {
    const { view } = tiptapEditor;
    const { selection } = view.state;
    const expectedDoc = doc(insertedNode());

    // Triggers the event handler that input rules listen to
    view.someProp('handleTextInput', (f) => f(view, selection.from, selection.to, input));

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