summaryrefslogtreecommitdiff
path: root/spec/frontend/helpers/indent_helper_spec.js
blob: fca12f0d1ef7b432594284260673c36cd461e482 (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
import IndentHelper from '~/helpers/indent_helper';

function createMockTextarea() {
  const el = document.createElement('textarea');
  el.setCursor = pos => el.setSelectionRange(pos, pos);
  el.setCursorToEnd = () => el.setCursor(el.value.length);
  el.selection = () => [el.selectionStart, el.selectionEnd];
  el.cursor = () => {
    const [start, end] = el.selection();
    return start === end ? start : undefined;
  };
  return el;
}

describe('indent_helper', () => {
  let element;
  let ih;

  beforeEach(() => {
    element = createMockTextarea();
    ih = new IndentHelper(element);
  });

  describe('indents', () => {
    describe('a single line', () => {
      it('when on an empty line; and cursor follows', () => {
        element.value = '';
        ih.indent();
        expect(element.value).toBe('    ');
        expect(element.cursor()).toBe(4);
        ih.indent();
        expect(element.value).toBe('        ');
        expect(element.cursor()).toBe(8);
      });

      it('when at the start of a line; and cursor stays at start', () => {
        element.value = 'foobar';
        element.setCursor(0);
        ih.indent();
        expect(element.value).toBe('    foobar');
        expect(element.cursor()).toBe(4);
      });

      it('when the cursor is in the middle; and cursor follows', () => {
        element.value = 'foobar';
        element.setCursor(3);
        ih.indent();
        expect(element.value).toBe('    foobar');
        expect(element.cursor()).toBe(7);
      });
    });

    describe('several lines', () => {
      it('when everything is selected; and everything remains selected', () => {
        element.value = 'foo\nbar\nbaz';
        element.setSelectionRange(0, 11);
        ih.indent();
        expect(element.value).toBe('    foo\n    bar\n    baz');
        expect(element.selection()).toEqual([0, 23]);
      });

      it('when all lines are partially selected; and the selection adapts', () => {
        element.value = 'foo\nbar\nbaz';
        element.setSelectionRange(2, 9);
        ih.indent();
        expect(element.value).toBe('    foo\n    bar\n    baz');
        expect(element.selection()).toEqual([6, 21]);
      });

      it('when some lines are entirely selected; and entire lines remain selected', () => {
        element.value = 'foo\nbar\nbaz';
        element.setSelectionRange(4, 11);
        ih.indent();
        expect(element.value).toBe('foo\n    bar\n    baz');
        expect(element.selection()).toEqual([4, 19]);
      });

      it('when some lines are partially selected; and the selection adapts', () => {
        element.value = 'foo\nbar\nbaz';
        element.setSelectionRange(5, 9);
        ih.indent();
        expect(element.value).toBe('foo\n    bar\n    baz');
        expect(element.selection()).toEqual([5 + 4, 9 + 2 * 4]);
      });

      it('having different indentation when some lines are entirely selected; and entire lines remain selected', () => {
        element.value = '    foo\nbar\n    baz';
        element.setSelectionRange(8, 19);
        ih.indent();
        expect(element.value).toBe('    foo\n    bar\n        baz');
        expect(element.selection()).toEqual([8, 27]);
      });

      it('having different indentation when some lines are partially selected; and the selection adapts', () => {
        element.value = '    foo\nbar\n    baz';
        element.setSelectionRange(9, 14);
        ih.indent();
        expect(element.value).toBe('    foo\n    bar\n        baz');
        expect(element.selection()).toEqual([13, 22]);
      });
    });
  });

  describe('unindents', () => {
    describe('a single line', () => {
      it('but does nothing if there is not indent', () => {
        element.value = 'foobar';
        element.setCursor(2);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(2);
      });

      it('but does nothing if there is a partial indent', () => {
        element.value = '  foobar';
        element.setCursor(1);
        ih.unindent();
        expect(element.value).toBe('  foobar');
        expect(element.cursor()).toBe(1);
      });

      it('when the cursor is in the line text; cursor follows', () => {
        element.value = '    foobar';
        element.setCursor(6);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(2);
      });

      it('when the cursor is in the indent; and cursor goes to start', () => {
        element.value = '    foobar';
        element.setCursor(2);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(0);
      });

      it('when the cursor is at line start; and cursor stays at start', () => {
        element.value = '    foobar';
        element.setCursor(0);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(0);
      });

      it('when a selection includes part of the indent and text', () => {
        element.value = '    foobar';
        element.setSelectionRange(2, 8);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.selection()).toEqual([0, 4]);
      });

      it('when a selection includes part of the indent only', () => {
        element.value = '    foobar';
        element.setSelectionRange(0, 4);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(0);

        element.value = '    foobar';
        element.setSelectionRange(1, 3);
        ih.unindent();
        expect(element.value).toBe('foobar');
        expect(element.cursor()).toBe(0);
      });
    });

    describe('several lines', () => {
      it('when everything is selected', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(0, 27);
        ih.unindent();
        expect(element.value).toBe('foo\n    bar\nbaz');
        expect(element.selection()).toEqual([0, 15]);
      });

      it('when all lines are partially selected', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(5, 26);
        ih.unindent();
        expect(element.value).toBe('foo\n    bar\nbaz');
        expect(element.selection()).toEqual([1, 14]);
      });

      it('when all lines are entirely selected', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(8, 27);
        ih.unindent();
        expect(element.value).toBe('    foo\n    bar\nbaz');
        expect(element.selection()).toEqual([8, 19]);
      });

      it('when some lines are entirely selected', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(8, 27);
        ih.unindent();
        expect(element.value).toBe('    foo\n    bar\nbaz');
        expect(element.selection()).toEqual([8, 19]);
      });

      it('when some lines are partially selected', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(17, 26);
        ih.unindent();
        expect(element.value).toBe('    foo\n    bar\nbaz');
        expect(element.selection()).toEqual([13, 18]);
      });

      it('when some lines are partially selected within their indents', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setSelectionRange(10, 22);
        ih.unindent();
        expect(element.value).toBe('    foo\n    bar\nbaz');
        expect(element.selection()).toEqual([8, 16]);
      });
    });
  });

  describe('newline', () => {
    describe('on a single line', () => {
      it('auto-indents the new line', () => {
        element.value = 'foo\n bar\n    baz\n        qux';

        element.setCursor(3);
        ih.newline();
        expect(element.value).toBe('foo\n\n bar\n    baz\n        qux');
        expect(element.cursor()).toBe(4);

        element.setCursor(9);
        ih.newline();
        expect(element.value).toBe('foo\n\n bar\n \n    baz\n        qux');
        expect(element.cursor()).toBe(11);

        element.setCursor(19);
        ih.newline();
        expect(element.value).toBe('foo\n\n bar\n \n    baz\n    \n        qux');
        expect(element.cursor()).toBe(24);

        element.setCursor(36);
        ih.newline();
        expect(element.value).toBe('foo\n\n bar\n \n    baz\n    \n        qux\n        ');
        expect(element.cursor()).toBe(45);
      });

      it('splits a line and auto-indents', () => {
        element.value = '    foobar';
        element.setCursor(7);
        ih.newline();
        expect(element.value).toBe('    foo\n    bar');
        expect(element.cursor()).toBe(12);
      });

      it('replaces selection with an indented newline', () => {
        element.value = '    foobarbaz';
        element.setSelectionRange(7, 10);
        ih.newline();
        expect(element.value).toBe('    foo\n    baz');
        expect(element.cursor()).toBe(12);
      });
    });

    it('on several lines.replaces selection with indented newline', () => {
      element.value = '  foo\n    bar\n  baz';
      element.setSelectionRange(4, 17);
      ih.newline();
      expect(element.value).toBe('  fo\n  az');
      expect(element.cursor()).toBe(7);
    });
  });

  describe('backspace', () => {
    let event;

    // This suite tests only the special indent-removing behaviour of the
    // backspace() method, since non-special cases are handled natively as a
    // backspace keypress.

    beforeEach(() => {
      event = { preventDefault: jest.fn() };
    });

    describe('on a single line', () => {
      it('does nothing special if in the line text', () => {
        element.value = '    foobar';
        element.setCursor(7);
        ih.backspace(event);
        expect(event.preventDefault).not.toHaveBeenCalled();
      });

      it('does nothing special if after a non-leading indent', () => {
        element.value = '    foo    bar';
        element.setCursor(11);
        ih.backspace(event);
        expect(event.preventDefault).not.toHaveBeenCalled();
      });

      it('deletes one leading indent', () => {
        element.value = '        foo';
        element.setCursor(8);
        ih.backspace(event);
        expect(event.preventDefault).toHaveBeenCalled();
        expect(element.value).toBe('    foo');
        expect(element.cursor()).toBe(4);
      });

      it('does nothing if cursor is inside the leading indent', () => {
        element.value = '        foo';
        element.setCursor(4);
        ih.backspace(event);
        expect(event.preventDefault).not.toHaveBeenCalled();
      });

      it('does nothing if cursor is at the start of the line', () => {
        element.value = '    foo';
        element.setCursor(0);
        ih.backspace(event);
        expect(event.preventDefault).not.toHaveBeenCalled();
      });

      it('deletes one partial indent', () => {
        element.value = '      foo';
        element.setCursor(6);
        ih.backspace(event);
        expect(event.preventDefault).toHaveBeenCalled();
        expect(element.value).toBe('    foo');
        expect(element.cursor()).toBe(4);
      });

      it('deletes indents sequentially', () => {
        element.value = '          foo';
        element.setCursor(10);
        ih.backspace(event);
        ih.backspace(event);
        ih.backspace(event);
        expect(event.preventDefault).toHaveBeenCalled();
        expect(element.value).toBe('foo');
        expect(element.cursor()).toBe(0);
      });
    });

    describe('on several lines', () => {
      it('deletes indent only on its own line', () => {
        element.value = '    foo\n        bar\n    baz';
        element.setCursor(16);
        ih.backspace(event);
        expect(event.preventDefault).toHaveBeenCalled();
        expect(element.value).toBe('    foo\n    bar\n    baz');
        expect(element.cursor()).toBe(12);
      });

      it('has no special behaviour with any range selection', () => {
        const text = '  foo\n    bar\n  baz';
        for (let start = 0; start < text.length; start += 1) {
          for (let end = start + 1; end < text.length; end += 1) {
            element.value = text;
            element.setSelectionRange(start, end);
            ih.backspace(event);
            expect(event.preventDefault).not.toHaveBeenCalled();

            // Ensure that the backspace() method doesn't change state
            // In reality, these two statements won't hold because the browser
            // will natively process the backspace event.
            expect(element.value).toBe(text);
            expect(element.selection()).toEqual([start, end]);
          }
        }
      });
    });
  });
});