summaryrefslogtreecommitdiff
path: root/spec/frontend/content_editor/components/suggestions_dropdown_spec.js
blob: e72eb892e74ed64ece41a5cfce29a38dcdcae672 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import { GlAvatarLabeled, GlDropdownItem } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
import SuggestionsDropdown from '~/content_editor/components/suggestions_dropdown.vue';

describe('~/content_editor/components/suggestions_dropdown', () => {
  let wrapper;

  const buildWrapper = ({ propsData } = {}) => {
    wrapper = extendedWrapper(
      shallowMount(SuggestionsDropdown, {
        propsData: {
          nodeType: 'reference',
          command: jest.fn(),
          ...propsData,
        },
      }),
    );
  };

  const exampleUser = { username: 'root', avatar_url: 'root_avatar.png', type: 'User' };
  const exampleIssue = { iid: 123, title: 'Test Issue' };
  const exampleMergeRequest = { iid: 224, title: 'Test MR' };
  const exampleMilestone1 = { iid: 21, title: '13' };
  const exampleMilestone2 = { iid: 24, title: 'Milestone with spaces' };

  const exampleCommand = {
    name: 'due',
    description: 'Set due date',
    params: ['<in 2 days | this Friday | December 31st>'],
  };
  const exampleEpic = {
    iid: 8884,
    title: '❓ Remote Development | Solution validation',
    reference: 'gitlab-org&8884',
  };
  const exampleLabel1 = {
    title: 'Create',
    color: '#E44D2A',
    type: 'GroupLabel',
    textColor: '#FFFFFF',
  };
  const exampleLabel2 = {
    title: 'Weekly Team Announcement',
    color: '#E44D2A',
    type: 'GroupLabel',
    textColor: '#FFFFFF',
  };
  const exampleLabel3 = {
    title: 'devops::create',
    color: '#E44D2A',
    type: 'GroupLabel',
    textColor: '#FFFFFF',
  };
  const exampleVulnerability = {
    id: 60850147,
    title: 'System procs network activity',
  };
  const exampleSnippet = {
    id: 2420859,
    title: 'Project creation QueryRecorder logs',
  };
  const exampleEmoji = {
    c: 'people',
    e: '😃',
    d: 'smiling face with open mouth',
    u: '6.0',
    name: 'smiley',
  };

  const insertedEmojiProps = {
    name: 'smiley',
    title: 'smiling face with open mouth',
    moji: '😃',
    unicodeVersion: '6.0',
  };

  describe('on item select', () => {
    it.each`
      nodeType       | referenceType      | char                 | reference               | insertedText                  | insertedProps
      ${'reference'} | ${'user'}          | ${'@'}               | ${exampleUser}          | ${`@root`}                    | ${{}}
      ${'reference'} | ${'issue'}         | ${'#'}               | ${exampleIssue}         | ${`#123`}                     | ${{}}
      ${'reference'} | ${'merge_request'} | ${'!'}               | ${exampleMergeRequest}  | ${`!224`}                     | ${{}}
      ${'reference'} | ${'milestone'}     | ${'%'}               | ${exampleMilestone1}    | ${`%13`}                      | ${{}}
      ${'reference'} | ${'milestone'}     | ${'%'}               | ${exampleMilestone2}    | ${`%Milestone with spaces`}   | ${{ originalText: '%"Milestone with spaces"' }}
      ${'reference'} | ${'command'}       | ${'/'}               | ${exampleCommand}       | ${'/due'}                     | ${{}}
      ${'reference'} | ${'epic'}          | ${'&'}               | ${exampleEpic}          | ${`gitlab-org&8884`}          | ${{}}
      ${'reference'} | ${'label'}         | ${'~'}               | ${exampleLabel1}        | ${`Create`}                   | ${{}}
      ${'reference'} | ${'label'}         | ${'~'}               | ${exampleLabel2}        | ${`Weekly Team Announcement`} | ${{ originalText: '~"Weekly Team Announcement"' }}
      ${'reference'} | ${'label'}         | ${'~'}               | ${exampleLabel3}        | ${`devops::create`}           | ${{ originalText: '~"devops::create"', text: 'devops::create' }}
      ${'reference'} | ${'vulnerability'} | ${'[vulnerability:'} | ${exampleVulnerability} | ${`[vulnerability:60850147]`} | ${{}}
      ${'reference'} | ${'snippet'}       | ${'$'}               | ${exampleSnippet}       | ${`$2420859`}                 | ${{}}
      ${'emoji'}     | ${'emoji'}         | ${':'}               | ${exampleEmoji}         | ${`😃`}                       | ${insertedEmojiProps}
    `(
      'runs a command to insert the selected $referenceType',
      ({ char, nodeType, referenceType, reference, insertedText, insertedProps }) => {
        const commandSpy = jest.fn();

        buildWrapper({
          propsData: {
            char,
            command: commandSpy,
            nodeType,
            nodeProps: {
              referenceType,
              test: 'prop',
            },
            items: [reference],
          },
        });

        wrapper.findComponent(GlDropdownItem).vm.$emit('click');

        expect(commandSpy).toHaveBeenCalledWith(
          expect.objectContaining({
            text: insertedText,
            test: 'prop',
            ...insertedProps,
          }),
        );
      },
    );
  });

  describe('rendering user references', () => {
    it('displays avatar labeled component', () => {
      buildWrapper({
        propsData: {
          char: '@',
          nodeProps: {
            referenceType: 'user',
          },
          items: [exampleUser],
        },
      });

      expect(wrapper.findComponent(GlAvatarLabeled).attributes()).toEqual(
        expect.objectContaining({
          label: exampleUser.username,
          shape: 'circle',
          src: exampleUser.avatar_url,
        }),
      );
    });

    describe.each`
      referenceType      | char   | reference              | displaysID
      ${'issue'}         | ${'#'} | ${exampleIssue}        | ${true}
      ${'merge_request'} | ${'!'} | ${exampleMergeRequest} | ${true}
      ${'milestone'}     | ${'%'} | ${exampleMilestone1}   | ${false}
    `('rendering $referenceType references', ({ referenceType, char, reference, displaysID }) => {
      it(`displays ${referenceType} ID and title`, () => {
        buildWrapper({
          propsData: {
            char,
            nodeType: 'reference',
            nodeProps: {
              referenceType,
            },
            items: [reference],
          },
        });

        if (displaysID) expect(wrapper.text()).toContain(`${reference.iid}`);
        else expect(wrapper.text()).not.toContain(`${reference.iid}`);
        expect(wrapper.text()).toContain(`${reference.title}`);
      });
    });

    describe.each`
      referenceType      | char                 | reference
      ${'snippet'}       | ${'$'}               | ${exampleSnippet}
      ${'vulnerability'} | ${'[vulnerability:'} | ${exampleVulnerability}
    `('rendering $referenceType references', ({ referenceType, char, reference }) => {
      it(`displays ${referenceType} ID and title`, () => {
        buildWrapper({
          propsData: {
            char,
            nodeProps: {
              referenceType,
            },
            items: [reference],
          },
        });

        expect(wrapper.text()).toContain(`${reference.id}`);
        expect(wrapper.text()).toContain(`${reference.title}`);
      });
    });

    describe('rendering label references', () => {
      it.each`
        label            | displayedTitle                | displayedColor
        ${exampleLabel1} | ${'Create'}                   | ${'rgb(228, 77, 42)' /* #E44D2A */}
        ${exampleLabel2} | ${'Weekly Team Announcement'} | ${'rgb(228, 77, 42)' /* #E44D2A */}
        ${exampleLabel3} | ${'devops::create'}           | ${'rgb(228, 77, 42)' /* #E44D2A */}
      `('displays label title and color', ({ label, displayedTitle, displayedColor }) => {
        buildWrapper({
          propsData: {
            char: '~',
            nodeProps: {
              referenceType: 'label',
            },
            items: [label],
          },
        });

        expect(wrapper.text()).toContain(displayedTitle);
        expect(wrapper.text()).not.toContain('"'); // no quotes in the dropdown list
        expect(wrapper.findByTestId('label-color-box').attributes().style).toEqual(
          `background-color: ${displayedColor};`,
        );
      });
    });

    describe('rendering epic references', () => {
      it('displays epic title and reference', () => {
        buildWrapper({
          propsData: {
            char: '&',
            nodeProps: {
              referenceType: 'epic',
            },
            items: [exampleEpic],
          },
        });

        expect(wrapper.text()).toContain(`${exampleEpic.reference}`);
        expect(wrapper.text()).toContain(`${exampleEpic.title}`);
      });
    });

    describe('rendering a command (quick action)', () => {
      it('displays command name with a slash', () => {
        buildWrapper({
          propsData: {
            char: '/',
            nodeProps: {
              referenceType: 'command',
            },
            items: [exampleCommand],
          },
        });

        expect(wrapper.text()).toContain(`${exampleCommand.name} `);
      });
    });

    describe('rendering emoji references', () => {
      it('displays emoji', () => {
        const testEmojis = [
          {
            c: 'people',
            e: '😄',
            d: 'smiling face with open mouth and smiling eyes',
            u: '6.0',
            name: 'smile',
          },
          {
            c: 'people',
            e: '😸',
            d: 'grinning cat face with smiling eyes',
            u: '6.0',
            name: 'smile_cat',
          },
          { c: 'people', e: '😃', d: 'smiling face with open mouth', u: '6.0', name: 'smiley' },
        ];

        buildWrapper({
          propsData: {
            char: ':',
            nodeType: 'emoji',
            nodeProps: {},
            items: testEmojis,
          },
        });

        testEmojis.forEach((testEmoji) => {
          expect(wrapper.text()).toContain(testEmoji.e);
          expect(wrapper.text()).toContain(testEmoji.d);
          expect(wrapper.text()).toContain(testEmoji.name);
        });
      });
    });
  });
});