summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token_spec.js
blob: 7a7e3055520d7fef895a7e23384c51c59b03aaa4 (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
import {
  buildTextToken,
  buildUneditableOpenTokens,
  buildUneditableCloseToken,
  buildUneditableCloseTokens,
  buildUneditableBlockTokens,
  buildUneditableInlineTokens,
  buildUneditableHtmlAsTextTokens,
} from '~/vue_shared/components/rich_content_editor/services/renderers/build_uneditable_token';

import {
  originInlineToken,
  originToken,
  uneditableOpenTokens,
  uneditableCloseToken,
  uneditableCloseTokens,
  uneditableBlockTokens,
  uneditableInlineTokens,
  uneditableTokens,
} from './mock_data';

describe('Build Uneditable Token renderer helper', () => {
  describe('buildTextToken', () => {
    it('returns an object literal representing a text token', () => {
      const text = originToken.content;
      expect(buildTextToken(text)).toStrictEqual(originToken);
    });
  });

  describe('buildUneditableOpenTokens', () => {
    it('returns a 2-item array of tokens with the originToken appended to an open token', () => {
      const result = buildUneditableOpenTokens(originToken);

      expect(result).toHaveLength(2);
      expect(result).toStrictEqual(uneditableOpenTokens);
    });
  });

  describe('buildUneditableCloseToken', () => {
    it('returns an object literal representing the uneditable close token', () => {
      expect(buildUneditableCloseToken()).toStrictEqual(uneditableCloseToken);
    });
  });

  describe('buildUneditableCloseTokens', () => {
    it('returns a 2-item array of tokens with the originToken prepended to a close token', () => {
      const result = buildUneditableCloseTokens(originToken);

      expect(result).toHaveLength(2);
      expect(result).toStrictEqual(uneditableCloseTokens);
    });
  });

  describe('buildUneditableBlockTokens', () => {
    it('returns a 3-item array of tokens with the originToken wrapped in the middle of block tokens', () => {
      const result = buildUneditableBlockTokens(originToken);

      expect(result).toHaveLength(3);
      expect(result).toStrictEqual(uneditableTokens);
    });
  });

  describe('buildUneditableInlineTokens', () => {
    it('returns a 3-item array of tokens with the originInlineToken wrapped in the middle of inline tokens', () => {
      const result = buildUneditableInlineTokens(originInlineToken);

      expect(result).toHaveLength(3);
      expect(result).toStrictEqual(uneditableInlineTokens);
    });
  });

  describe('buildUneditableHtmlAsTextTokens', () => {
    it('returns a 3-item array of tokens with the htmlBlockNode wrapped as a text token in the middle of block tokens', () => {
      const htmlBlockNode = {
        type: 'htmlBlock',
        literal: '<div data-tomark-pass ><h1>Some header</h1><p>Some paragraph</p></div>',
      };
      const result = buildUneditableHtmlAsTextTokens(htmlBlockNode);
      const { type, content } = result[1];

      expect(type).toBe('text');
      expect(content).not.toMatch(/ data-tomark-pass /);

      expect(result).toHaveLength(3);
      expect(result).toStrictEqual(uneditableBlockTokens);
    });
  });
});