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

source_editor_yaml_ext_spec.js « editor « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a861d9c7a455af5ae276b4fd4ddfacd85c842b54 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { Document } from 'yaml';
import SourceEditor from '~/editor/source_editor';
import { YamlEditorExtension } from '~/editor/extensions/source_editor_yaml_ext';
import { SourceEditorExtension } from '~/editor/extensions/source_editor_extension_base';
import { spyOnApi } from 'jest/editor/helpers';

let baseExtension;
let yamlExtension;

const getEditorInstance = (editorInstanceOptions = {}) => {
  setFixtures('<div id="editor"></div>');
  return new SourceEditor().createInstance({
    el: document.getElementById('editor'),
    blobPath: '.gitlab-ci.yml',
    language: 'yaml',
    ...editorInstanceOptions,
  });
};

const getEditorInstanceWithExtension = (extensionOptions = {}, editorInstanceOptions = {}) => {
  setFixtures('<div id="editor"></div>');
  const instance = getEditorInstance(editorInstanceOptions);
  [baseExtension, yamlExtension] = instance.use([
    { definition: SourceEditorExtension },
    { definition: YamlEditorExtension, setupOptions: extensionOptions },
  ]);

  // Remove the below once
  // https://gitlab.com/gitlab-org/gitlab/-/issues/325992 is resolved
  if (editorInstanceOptions.value && !extensionOptions.model) {
    instance.setValue(editorInstanceOptions.value);
  }

  return instance;
};

describe('YamlCreatorExtension', () => {
  describe('constructor', () => {
    it('saves setupOptions options on the extension, but does not expose those to instance', () => {
      const highlightPath = 'foo';
      const instance = getEditorInstanceWithExtension({
        highlightPath,
        enableComments: true,
      });
      expect(yamlExtension.obj.highlightPath).toBe(highlightPath);
      expect(yamlExtension.obj.enableComments).toBe(true);
      expect(instance.highlightPath).toBeUndefined();
      expect(instance.enableComments).toBeUndefined();
    });

    it('dumps values loaded with the model constructor options', () => {
      const model = { foo: 'bar' };
      const expected = 'foo: bar\n';
      const instance = getEditorInstanceWithExtension({ model });
      expect(instance.getDoc().get('foo')).toBeDefined();
      expect(instance.getValue()).toEqual(expected);
    });

    it('registers the onUpdate() function', () => {
      const instance = getEditorInstance();
      const onDidChangeModelContent = jest.spyOn(instance, 'onDidChangeModelContent');
      instance.use({ definition: YamlEditorExtension });
      expect(onDidChangeModelContent).toHaveBeenCalledWith(expect.any(Function));
    });

    it("If not provided with a load constructor option, it will parse the editor's value", () => {
      const editorValue = 'foo: bar';
      const instance = getEditorInstanceWithExtension({}, { value: editorValue });
      expect(instance.getDoc().get('foo')).toBeDefined();
    });

    it("Prefers values loaded with the load constructor option over the editor's existing value", () => {
      const editorValue = 'oldValue: this should be overriden';
      const model = { thisShould: 'be the actual value' };
      const expected = 'thisShould: be the actual value\n';
      const instance = getEditorInstanceWithExtension({ model }, { value: editorValue });
      expect(instance.getDoc().get('oldValue')).toBeUndefined();
      expect(instance.getValue()).toEqual(expected);
    });
  });

  describe('initFromModel', () => {
    const model = { foo: 'bar', 1: 2, abc: ['def'] };
    const doc = new Document(model);

    it('should call transformComments if enableComments is true', () => {
      const instance = getEditorInstanceWithExtension({ enableComments: true });
      const transformComments = jest.spyOn(YamlEditorExtension, 'transformComments');
      instance.initFromModel(model);
      expect(transformComments).toHaveBeenCalled();
    });

    it('should not call transformComments if enableComments is false', () => {
      const instance = getEditorInstanceWithExtension({ enableComments: false });
      const transformComments = jest.spyOn(YamlEditorExtension, 'transformComments');
      instance.initFromModel(model);
      expect(transformComments).not.toHaveBeenCalled();
    });

    it('should call setValue with the stringified model', () => {
      const instance = getEditorInstanceWithExtension();
      const setValue = jest.spyOn(instance, 'setValue');
      instance.initFromModel(model);
      expect(setValue).toHaveBeenCalledWith(doc.toString());
    });
  });

  describe('wrapCommentString', () => {
    const longString =
      'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';

    it('should add spaces before each line', () => {
      const result = YamlEditorExtension.wrapCommentString(longString);
      const lines = result.split('\n');
      expect(lines.every((ln) => ln.startsWith(' '))).toBe(true);
    });

    it('should break long comments into lines of max. 79 chars', () => {
      // 79 = 80 char width minus 1 char for the '#' at the start of each line
      const result = YamlEditorExtension.wrapCommentString(longString);
      const lines = result.split('\n');
      expect(lines.every((ln) => ln.length <= 79)).toBe(true);
    });

    it('should decrease the line width if passed a level by 2 chars per level', () => {
      for (let i = 0; i <= 5; i += 1) {
        const result = YamlEditorExtension.wrapCommentString(longString, i);
        const lines = result.split('\n');
        const decreaseLineWidthBy = i * 2;
        const maxLineWith = 79 - decreaseLineWidthBy;
        const isValidLine = (ln) => {
          if (ln.length <= maxLineWith) return true;
          // The line may exceed the max line width in case the word is the
          // only one in the line and thus cannot be broken further
          return ln.split(' ').length <= 1;
        };
        expect(lines.every(isValidLine)).toBe(true);
      }
    });

    it('return null if passed an invalid string value', () => {
      expect(YamlEditorExtension.wrapCommentString(null)).toBe(null);
      expect(YamlEditorExtension.wrapCommentString()).toBe(null);
    });

    it('throw an error if passed an invalid level value', () => {
      expect(() => YamlEditorExtension.wrapCommentString('abc', -5)).toThrow(
        'Invalid value "-5" for variable `level`',
      );
      expect(() => YamlEditorExtension.wrapCommentString('abc', 'invalid')).toThrow(
        'Invalid value "invalid" for variable `level`',
      );
    });
  });

  describe('transformComments', () => {
    const getInstanceWithModel = (model) => {
      return getEditorInstanceWithExtension({
        model,
        enableComments: true,
      });
    };

    it('converts comments inside an array', () => {
      const model = ['# test comment', 'def', '# foo', 999];
      const expected = `# test comment\n- def\n# foo\n- 999\n`;
      const instance = getInstanceWithModel(model);
      expect(instance.getValue()).toEqual(expected);
    });

    it('converts generic comments inside an object and places them at the top', () => {
      const model = { foo: 'bar', 1: 2, '#': 'test comment' };
      const expected = `# test comment\n"1": 2\nfoo: bar\n`;
      const instance = getInstanceWithModel(model);
      expect(instance.getValue()).toEqual(expected);
    });

    it('adds specific comments before the mentioned entry of an object', () => {
      const model = { foo: 'bar', 1: 2, '#|foo': 'foo comment' };
      const expected = `"1": 2\n# foo comment\nfoo: bar\n`;
      const instance = getInstanceWithModel(model);
      expect(instance.getValue()).toEqual(expected);
    });

    it('limits long comments to 80 char width, including indentation', () => {
      const model = {
        '#|foo':
          'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.',
        foo: {
          nested1: {
            nested2: {
              nested3: {
                '#|bar':
                  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.',
                bar: 'baz',
              },
            },
          },
        },
      };
      const expected = `# Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
# eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
# voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
foo:
  nested1:
    nested2:
      nested3:
        # Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
        # nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
        # sed diam voluptua. At vero eos et accusam et justo duo dolores et ea
        # rebum.
        bar: baz
`;
      const instance = getInstanceWithModel(model);
      expect(instance.getValue()).toEqual(expected);
    });
  });

  describe('getDoc', () => {
    it('returns a yaml `Document` Type', () => {
      const instance = getEditorInstanceWithExtension();
      expect(instance.getDoc()).toBeInstanceOf(Document);
    });
  });

  describe('setDoc', () => {
    const model = { foo: 'bar', 1: 2, abc: ['def'] };
    const doc = new Document(model);

    it('should call transformComments if enableComments is true', () => {
      const spy = jest.spyOn(YamlEditorExtension, 'transformComments');
      const instance = getEditorInstanceWithExtension({ enableComments: true });
      instance.setDoc(doc);
      expect(spy).toHaveBeenCalledWith(doc);
    });

    it('should not call transformComments if enableComments is false', () => {
      const spy = jest.spyOn(YamlEditorExtension, 'transformComments');
      const instance = getEditorInstanceWithExtension({ enableComments: false });
      instance.setDoc(doc);
      expect(spy).not.toHaveBeenCalled();
    });

    it("should call setValue with the stringified doc if the editor's value is empty", () => {
      const instance = getEditorInstanceWithExtension();
      const setValue = jest.spyOn(instance, 'setValue');
      const updateValueSpy = jest.fn();
      spyOnApi(yamlExtension, {
        updateValue: updateValueSpy,
      });
      instance.setDoc(doc);
      expect(setValue).toHaveBeenCalledWith(doc.toString());
      expect(updateValueSpy).not.toHaveBeenCalled();
    });

    it("should call updateValue with the stringified doc if the editor's value is not empty", () => {
      const instance = getEditorInstanceWithExtension({}, { value: 'asjkdhkasjdh' });
      const setValue = jest.spyOn(instance, 'setValue');
      const updateValueSpy = jest.fn();
      spyOnApi(yamlExtension, {
        updateValue: updateValueSpy,
      });
      instance.setDoc(doc);
      expect(setValue).not.toHaveBeenCalled();
      expect(updateValueSpy).toHaveBeenCalledWith(instance, doc.toString());
    });

    it('should trigger the onUpdate method', () => {
      const instance = getEditorInstanceWithExtension();
      const onUpdateSpy = jest.fn();
      spyOnApi(yamlExtension, {
        onUpdate: onUpdateSpy,
      });
      instance.setDoc(doc);
      expect(onUpdateSpy).toHaveBeenCalled();
    });
  });

  describe('getDataModel', () => {
    it('returns the model as JS', () => {
      const value = 'abc: def\nfoo:\n  - bar\n  - baz\n';
      const expected = { abc: 'def', foo: ['bar', 'baz'] };
      const instance = getEditorInstanceWithExtension({}, { value });
      expect(instance.getDataModel()).toEqual(expected);
    });
  });

  describe('setDataModel', () => {
    it('sets the value to a YAML-representation of the Doc', () => {
      const model = {
        abc: ['def'],
        '#|foo': 'foo comment',
        foo: {
          '#|abc': 'abc comment',
          abc: [{ def: 'ghl', lorem: 'ipsum' }, '# array comment', null],
          bar: 'baz',
        },
      };
      const expected =
        'abc:\n' +
        '  - def\n' +
        '# foo comment\n' +
        'foo:\n' +
        '  # abc comment\n' +
        '  abc:\n' +
        '    - def: ghl\n' +
        '      lorem: ipsum\n' +
        '    # array comment\n' +
        '    - null\n' +
        '  bar: baz\n';

      const instance = getEditorInstanceWithExtension({ enableComments: true });
      const setValue = jest.spyOn(instance, 'setValue');

      instance.setDataModel(model);

      expect(setValue).toHaveBeenCalledWith(expected);
    });

    it('causes the editor value to be updated', () => {
      const initialModel = { foo: 'this should be overriden' };
      const initialValue = 'foo: this should be overriden\n';
      const newValue = { thisShould: 'be the actual value' };
      const expected = 'thisShould: be the actual value\n';
      const instance = getEditorInstanceWithExtension({ model: initialModel });
      expect(instance.getValue()).toEqual(initialValue);
      instance.setDataModel(newValue);
      expect(instance.getValue()).toEqual(expected);
    });
  });

  describe('onUpdate', () => {
    it('calls highlight', () => {
      const highlightPath = 'foo';
      const instance = getEditorInstanceWithExtension({ highlightPath });
      // Here we do not spy on the public API method of the extension, but rather
      // the public method of the extension's instance.
      // This is required based on how `onUpdate` works
      const highlightSpy = jest.spyOn(yamlExtension.obj, 'highlight');
      instance.onUpdate();
      expect(highlightSpy).toHaveBeenCalledWith(instance, highlightPath);
    });
  });

  describe('updateValue', () => {
    it("causes the editor's value to be updated", () => {
      const oldValue = 'foobar';
      const newValue = 'bazboo';
      const instance = getEditorInstanceWithExtension({}, { value: oldValue });
      instance.updateValue(newValue);
      expect(instance.getValue()).toEqual(newValue);
    });
  });

  describe('highlight', () => {
    const highlightPathOnSetup = 'abc';
    const value = `foo:
  bar:
    - baz
    - boo
  abc: def
`;
    let instance;
    let highlightLinesSpy;
    let removeHighlightsSpy;

    beforeEach(() => {
      instance = getEditorInstanceWithExtension({ highlightPath: highlightPathOnSetup }, { value });
      highlightLinesSpy = jest.fn();
      removeHighlightsSpy = jest.fn();
      spyOnApi(baseExtension, {
        highlightLines: highlightLinesSpy,
        removeHighlights: removeHighlightsSpy,
      });
    });

    afterEach(() => {
      jest.clearAllMocks();
    });

    it('saves the highlighted path in highlightPath', () => {
      const path = 'foo.bar';
      instance.highlight(path);
      expect(yamlExtension.obj.highlightPath).toEqual(path);
    });

    it('calls highlightLines with a number of lines', () => {
      const path = 'foo.bar';
      instance.highlight(path);
      expect(highlightLinesSpy).toHaveBeenCalledWith(instance, [2, 4]);
    });

    it('calls removeHighlights if path is null', () => {
      instance.highlight(null);
      expect(removeHighlightsSpy).toHaveBeenCalledWith(instance);
      expect(highlightLinesSpy).not.toHaveBeenCalled();
      expect(yamlExtension.obj.highlightPath).toBeNull();
    });

    it('throws an error if path is invalid and does not change the highlighted path', () => {
      expect(() => instance.highlight('invalidPath[0]')).toThrow(
        'The node invalidPath[0] could not be found inside the document.',
      );
      expect(yamlExtension.obj.highlightPath).toEqual(highlightPathOnSetup);
      expect(highlightLinesSpy).not.toHaveBeenCalled();
      expect(removeHighlightsSpy).not.toHaveBeenCalled();
    });
  });

  describe('locate', () => {
    const options = {
      enableComments: true,
      model: {
        abc: ['def'],
        '#|foo': 'foo comment',
        foo: {
          '#|abc': 'abc comment',
          abc: [{ def: 'ghl', lorem: 'ipsum' }, '# array comment', null],
          bar: 'baz',
        },
      },
    };

    const value =
      /*  1 */ 'abc:\n' +
      /*  2 */ '  - def\n' +
      /*  3 */ '# foo comment\n' +
      /*  4 */ 'foo:\n' +
      /*  5 */ '  # abc comment\n' +
      /*  6 */ '  abc:\n' +
      /*  7 */ '    - def: ghl\n' +
      /*  8 */ '      lorem: ipsum\n' +
      /*  9 */ '    # array comment\n' +
      /* 10 */ '    - null\n' +
      /* 11 */ '  bar: baz\n';

    it('asserts that the test setup is correct', () => {
      const instance = getEditorInstanceWithExtension(options);
      expect(instance.getValue()).toEqual(value);
    });

    it('returns the expected line numbers for a path to an object inside the yaml', () => {
      const path = 'foo.abc';
      const expected = [6, 10];
      const instance = getEditorInstanceWithExtension(options);
      expect(instance.locate(path)).toEqual(expected);
    });

    it('throws an error if a path cannot be found inside the yaml', () => {
      const path = 'baz[8]';
      const instance = getEditorInstanceWithExtension(options);
      expect(() => instance.locate(path)).toThrow();
    });

    it('returns the expected line numbers for a path to an array entry inside the yaml', () => {
      const path = 'foo.abc[0]';
      const expected = [7, 8];
      const instance = getEditorInstanceWithExtension(options);
      expect(instance.locate(path)).toEqual(expected);
    });

    it('returns the expected line numbers for a path that includes a comment inside the yaml', () => {
      const path = 'foo';
      const expected = [4, 11];
      const instance = getEditorInstanceWithExtension(options);
      expect(instance.locate(path)).toEqual(expected);
    });
  });
});