Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reference_spec.js « extensions « content_editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4b07d5127e8518c0523eed15304ebafb871c599 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import Reference from '~/content_editor/extensions/reference';
import ReferenceLabel from '~/content_editor/extensions/reference_label';
import AssetResolver from '~/content_editor/services/asset_resolver';
import {
  RESOLVED_ISSUE_HTML,
  RESOLVED_MERGE_REQUEST_HTML,
  RESOLVED_EPIC_HTML,
  RESOLVED_LABEL_HTML,
  RESOLVED_SNIPPET_HTML,
  RESOLVED_MILESTONE_HTML,
  RESOLVED_USER_HTML,
  RESOLVED_VULNERABILITY_HTML,
} from '../test_constants';
import {
  createTestEditor,
  createDocBuilder,
  triggerNodeInputRule,
  waitUntilTransaction,
} from '../test_utils';

describe('content_editor/extensions/reference', () => {
  let tiptapEditor;
  let doc;
  let p;
  let reference;
  let referenceLabel;
  let renderMarkdown;
  let assetResolver;

  beforeEach(() => {
    renderMarkdown = jest.fn().mockImplementation(() => new Promise(() => {}));
    assetResolver = new AssetResolver({ renderMarkdown });

    tiptapEditor = createTestEditor({
      extensions: [Reference.configure({ assetResolver }), ReferenceLabel],
    });

    ({
      builders: { doc, p, reference, referenceLabel },
    } = createDocBuilder({
      tiptapEditor,
      names: {
        reference: { nodeType: Reference.name },
        referenceLabel: { nodeType: ReferenceLabel.name },
      },
    }));
  });

  describe('when typing a valid reference input rule', () => {
    const buildExpectedDoc = (href, originalText, referenceType, text = originalText) =>
      doc(p(reference({ className: null, href, originalText, referenceType, text }), ' '));

    const buildExpectedDocForLabel = (href, originalText, text, color) =>
      doc(
        p(
          referenceLabel({
            className: null,
            referenceType: 'label',
            href,
            originalText,
            text,
            color,
          }),
          ' ',
        ),
      );

    it.each`
      inputRuleText          | mockReferenceHtml              | expectedDoc
      ${'#1'}                | ${RESOLVED_ISSUE_HTML}         | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/issues/1', '#1', 'issue', '#1 (closed)')}
      ${'#1+'}               | ${RESOLVED_ISSUE_HTML}         | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/issues/1', '#1+', 'issue', '500 error on MR approvers edit page (#1 - closed)')}
      ${'#1+s'}              | ${RESOLVED_ISSUE_HTML}         | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/issues/1', '#1+s', 'issue', '500 error on MR approvers edit page (#1 - closed) • Unassigned')}
      ${'!1'}                | ${RESOLVED_MERGE_REQUEST_HTML} | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/merge_requests/1', '!1', 'merge_request', '!1 (merged)')}
      ${'!1+'}               | ${RESOLVED_MERGE_REQUEST_HTML} | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/merge_requests/1', '!1+', 'merge_request', 'Enhance the LDAP group synchronization (!1 - merged)')}
      ${'!1+s'}              | ${RESOLVED_MERGE_REQUEST_HTML} | ${() => buildExpectedDoc('/gitlab-org/gitlab/-/merge_requests/1', '!1+s', 'merge_request', 'Enhance the LDAP group synchronization (!1 - merged) • John Doe')}
      ${'&1'}                | ${RESOLVED_EPIC_HTML}          | ${() => buildExpectedDoc('/groups/gitlab-org/-/epics/1', '&1', 'epic', '&1')}
      ${'&1+'}               | ${RESOLVED_EPIC_HTML}          | ${() => buildExpectedDoc('/groups/gitlab-org/-/epics/1', '&1+', 'epic', 'Approvals in merge request list (&1)')}
      ${'@root'}             | ${RESOLVED_USER_HTML}          | ${() => buildExpectedDoc('/root', '@root', 'user')}
      ${'~Aquanix'}          | ${RESOLVED_LABEL_HTML}         | ${() => buildExpectedDocForLabel('/gitlab-org/gitlab-shell/-/issues?label_name=Aquanix', '~Aquanix', 'Aquanix', 'rgb(230, 84, 49)')}
      ${'%v4.0'}             | ${RESOLVED_MILESTONE_HTML}     | ${() => buildExpectedDoc('/gitlab-org/gitlab-shell/-/milestones/5', '%v4.0', 'milestone')}
      ${'$25'}               | ${RESOLVED_SNIPPET_HTML}       | ${() => buildExpectedDoc('/gitlab-org/gitlab-shell/-/snippets/25', '$25', 'snippet')}
      ${'[vulnerability:1]'} | ${RESOLVED_VULNERABILITY_HTML} | ${() => buildExpectedDoc('/gitlab-org/gitlab-shell/-/security/vulnerabilities/1', '[vulnerability:1]', 'vulnerability')}
    `(
      'replaces the input rule ($inputRuleText) with a reference node',
      async ({ inputRuleText, mockReferenceHtml, expectedDoc }) => {
        await waitUntilTransaction({
          number: 2,
          tiptapEditor,
          action() {
            renderMarkdown.mockResolvedValueOnce(mockReferenceHtml);

            tiptapEditor.commands.insertContent({ type: 'text', text: `${inputRuleText} ` });
            triggerNodeInputRule({ tiptapEditor, inputRuleText: `${inputRuleText} ` });
          },
        });

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

    it('resolves multiple references in the same paragraph correctly', async () => {
      await waitUntilTransaction({
        number: 2,
        tiptapEditor,
        action() {
          renderMarkdown.mockResolvedValueOnce(RESOLVED_ISSUE_HTML);

          tiptapEditor.commands.insertContent({ type: 'text', text: '#1+ ' });
          triggerNodeInputRule({ tiptapEditor, inputRuleText: '#1+ ' });
        },
      });

      await waitUntilTransaction({
        number: 2,
        tiptapEditor,
        action() {
          renderMarkdown.mockResolvedValueOnce(RESOLVED_MERGE_REQUEST_HTML);

          tiptapEditor.commands.insertContent({ type: 'text', text: 'was resolved with !1+ ' });
          triggerNodeInputRule({ tiptapEditor, inputRuleText: 'was resolved with !1+ ' });
        },
      });

      expect(tiptapEditor.getJSON()).toEqual(
        doc(
          p(
            reference({
              referenceType: 'issue',
              originalText: '#1+',
              text: '500 error on MR approvers edit page (#1 - closed)',
              href: '/gitlab-org/gitlab/-/issues/1',
            }),
            ' was resolved with ',
            reference({
              referenceType: 'merge_request',
              originalText: '!1+',
              text: 'Enhance the LDAP group synchronization (!1 - merged)',
              href: '/gitlab-org/gitlab/-/merge_requests/1',
            }),
            ' ',
          ),
        ).toJSON(),
      );
    });

    it('resolves the input rule lazily in the correct position if the user makes a change before the request resolves', async () => {
      let resolvePromise;
      const promise = new Promise((resolve) => {
        resolvePromise = resolve;
      });

      renderMarkdown.mockImplementation(() => promise);

      tiptapEditor.commands.insertContent({ type: 'text', text: '#1+ ' });
      triggerNodeInputRule({ tiptapEditor, inputRuleText: '#1+ ' });

      // insert a new paragraph at a random location
      tiptapEditor.commands.insertContentAt(0, {
        type: 'paragraph',
        content: [{ type: 'text', text: 'Hello' }],
      });

      // update selection
      tiptapEditor.commands.selectAll();

      await waitUntilTransaction({
        number: 1,
        tiptapEditor,
        action() {
          resolvePromise(RESOLVED_ISSUE_HTML);
        },
      });

      expect(tiptapEditor.state.doc).toEqual(
        doc(
          p('Hello'),
          p(
            reference({
              referenceType: 'issue',
              originalText: '#1+',
              text: '500 error on MR approvers edit page (#1 - closed)',
              href: '/gitlab-org/gitlab/-/issues/1',
            }),
            ' ',
          ),
        ),
      );
    });
  });
});