summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.html
blob: ad036c58c14dc0e73879cb0650f14865e5b339a4 (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
<!DOCTYPE html>
<!--
@license
Copyright (C) 2016 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>gr-editable-label</title>

<script src="../../../bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<script src="../../../bower_components/web-component-tester/browser.js"></script>
<link rel="import" href="../../../test/common-test-setup.html"/>
<link rel="import" href="gr-formatted-text.html">

<script>void(0);</script>

<test-fixture id="basic">
  <template>
    <gr-formatted-text></gr-formatted-text>
  </template>
</test-fixture>

<script>
  suite('gr-formatted-text tests', () => {
    let element;
    let sandbox;

    function assertBlock(result, index, type, text) {
      assert.equal(result[index].type, type);
      assert.equal(result[index].text, text);
    }

    function assertListBlock(result, resultIndex, itemIndex, text) {
      assert.equal(result[resultIndex].type, 'list');
      assert.equal(result[resultIndex].items[itemIndex], text);
    }

    setup(() => {
      element = fixture('basic');
      sandbox = sinon.sandbox.create();
    });

    teardown(() => {
      sandbox.restore();
    });

    test('parse null undefined and empty', () => {
      assert.lengthOf(element._computeBlocks(null), 0);
      assert.lengthOf(element._computeBlocks(undefined), 0);
      assert.lengthOf(element._computeBlocks(''), 0);
    });

    test('parse simple', () => {
      const comment = 'Para1';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'paragraph', comment);
    });

    test('parse multiline para', () => {
      const comment = 'Para 1\nStill para 1';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'paragraph', comment);
    });

    test('parse para break', () => {
      const comment = 'Para 1\n\nPara 2\n\nPara 3';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'Para 1');
      assertBlock(result, 1, 'paragraph', 'Para 2');
      assertBlock(result, 2, 'paragraph', 'Para 3');
    });

    test('parse quote', () => {
      const comment = '> Quote text';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assert.equal(result[0].type, 'quote');
      assert.lengthOf(result[0].blocks, 1);
      assertBlock(result[0].blocks, 0, 'paragraph', 'Quote text');
    });

    test('parse quote lead space', () => {
      const comment = ' > Quote text';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assert.equal(result[0].type, 'quote');
      assert.lengthOf(result[0].blocks, 1);
      assertBlock(result[0].blocks, 0, 'paragraph', 'Quote text');
    });

    test('parse excludes empty', () => {
      const comment = 'Para 1\n\n\n\nPara 2';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'Para 1');
      assertBlock(result, 1, 'paragraph', 'Para 2');
    });

    test('parse multiline quote', () => {
      const comment = '> Quote line 1\n> Quote line 2\n > Quote line 3\n';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assert.equal(result[0].type, 'quote');
      assert.lengthOf(result[0].blocks, 1);
      assertBlock(result[0].blocks, 0, 'paragraph',
          'Quote line 1\nQuote line 2\nQuote line 3\n');
    });

    test('parse pre', () => {
      const comment = '    Four space indent.';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'pre', comment);
    });

    test('parse one space pre', () => {
      const comment = ' One space indent.\n Another line.';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'pre', comment);
    });

    test('parse tab pre', () => {
      const comment = '\tOne tab indent.\n\tAnother line.\n  Yet another!';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'pre', comment);
    });

    test('parse intermediate leading whitespace pre', () => {
      const comment = 'No indent.\n\tNonzero indent.\nNo indent again.';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertBlock(result, 0, 'pre', comment);
    });

    test('parse star list', () => {
      const comment = '* Item 1\n* Item 2\n* Item 3';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertListBlock(result, 0, 0, 'Item 1');
      assertListBlock(result, 0, 1, 'Item 2');
      assertListBlock(result, 0, 2, 'Item 3');
    });

    test('parse dash list', () => {
      const comment = '- Item 1\n- Item 2\n- Item 3';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertListBlock(result, 0, 0, 'Item 1');
      assertListBlock(result, 0, 1, 'Item 2');
      assertListBlock(result, 0, 2, 'Item 3');
    });

    test('parse mixed list', () => {
      const comment = '- Item 1\n* Item 2\n- Item 3\n* Item 4';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assertListBlock(result, 0, 0, 'Item 1');
      assertListBlock(result, 0, 1, 'Item 2');
      assertListBlock(result, 0, 2, 'Item 3');
      assertListBlock(result, 0, 3, 'Item 4');
    });

    test('parse mixed block types', () => {
      const comment = 'Paragraph\nacross\na\nfew\nlines.' +
          '\n\n' +
          '> Quote\n> across\n> not many lines.' +
          '\n\n' +
          'Another paragraph' +
          '\n\n' +
          '* Series\n* of\n* list\n* items' +
          '\n\n' +
          'Yet another paragraph' +
          '\n\n' +
          '\tPreformatted text.' +
          '\n\n' +
          'Parting words.';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 7);
      assertBlock(result, 0, 'paragraph', 'Paragraph\nacross\na\nfew\nlines.');

      assert.equal(result[1].type, 'quote');
      assert.lengthOf(result[1].blocks, 1);
      assertBlock(result[1].blocks, 0, 'paragraph',
          'Quote\nacross\nnot many lines.');

      assertBlock(result, 2, 'paragraph', 'Another paragraph');
      assertListBlock(result, 3, 0, 'Series');
      assertListBlock(result, 3, 1, 'of');
      assertListBlock(result, 3, 2, 'list');
      assertListBlock(result, 3, 3, 'items');
      assertBlock(result, 4, 'paragraph', 'Yet another paragraph');
      assertBlock(result, 5, 'pre', '\tPreformatted text.');
      assertBlock(result, 6, 'paragraph', 'Parting words.');
    });

    test('bullet list 1', () => {
      const comment = 'A\n\n* line 1\n* 2nd line';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'A');
      assertListBlock(result, 1, 0, 'line 1');
      assertListBlock(result, 1, 1, '2nd line');
    });

    test('bullet list 2', () => {
      const comment = 'A\n\n* line 1\n* 2nd line\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'A');
      assertListBlock(result, 1, 0, 'line 1');
      assertListBlock(result, 1, 1, '2nd line');
      assertBlock(result, 2, 'paragraph', 'B');
    });

    test('bullet list 3', () => {
      const comment = '* line 1\n* 2nd line\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertListBlock(result, 0, 0, 'line 1');
      assertListBlock(result, 0, 1, '2nd line');
      assertBlock(result, 1, 'paragraph', 'B');
    });

    test('bullet list 4', () => {
      const comment = 'To see this bug, you have to:\n' +
          '* Be on IMAP or EAS (not on POP)\n' +
          '* Be very unlucky\n';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'To see this bug, you have to:');
      assertListBlock(result, 1, 0, 'Be on IMAP or EAS (not on POP)');
      assertListBlock(result, 1, 1, 'Be very unlucky');
    });

    test('bullet list 5', () => {
      const comment = 'To see this bug,\n' +
          'you have to:\n' +
          '* Be on IMAP or EAS (not on POP)\n' +
          '* Be very unlucky\n';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'To see this bug, you have to:');
      assertListBlock(result, 1, 0, 'Be on IMAP or EAS (not on POP)');
      assertListBlock(result, 1, 1, 'Be very unlucky');
    });

    test('dash list 1', () => {
      const comment = 'A\n\n- line 1\n- 2nd line';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'A');
      assertListBlock(result, 1, 0, 'line 1');
      assertListBlock(result, 1, 1, '2nd line');
    });

    test('dash list 2', () => {
      const comment = 'A\n\n- line 1\n- 2nd line\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'A');
      assertListBlock(result, 1, 0, 'line 1');
      assertListBlock(result, 1, 1, '2nd line');
      assertBlock(result, 2, 'paragraph', 'B');
    });

    test('dash list 3', () => {
      const comment = '- line 1\n- 2nd line\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertListBlock(result, 0, 0, 'line 1');
      assertListBlock(result, 0, 1, '2nd line');
      assertBlock(result, 1, 'paragraph', 'B');
    });

    test('pre format 1', () => {
      const comment = 'A\n\n  This is pre\n  formatted';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'paragraph', 'A');
      assertBlock(result, 1, 'pre', '  This is pre\n  formatted');
    });

    test('pre format 2', () => {
      const comment = 'A\n\n  This is pre\n  formatted\n\nbut this is not';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'A');
      assertBlock(result, 1, 'pre', '  This is pre\n  formatted');
      assertBlock(result, 2, 'paragraph', 'but this is not');
    });

    test('pre format 3', () => {
      const comment = 'A\n\n  Q\n    <R>\n  S\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'A');
      assertBlock(result, 1, 'pre', '  Q\n    <R>\n  S');
      assertBlock(result, 2, 'paragraph', 'B');
    });

    test('pre format 4', () => {
      const comment = '  Q\n    <R>\n  S\n\nB';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assertBlock(result, 0, 'pre', '  Q\n    <R>\n  S');
      assertBlock(result, 1, 'paragraph', 'B');
    });

    test('quote 1', () => {
      const comment = '> I\'m happy\n > with quotes!\n\nSee above.';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 2);
      assert.equal(result[0].type, 'quote');
      assert.lengthOf(result[0].blocks, 1);
      assertBlock(result[0].blocks, 0, 'paragraph', 'I\'m happy\nwith quotes!');
      assertBlock(result, 1, 'paragraph', 'See above.');
    });

    test('quote 2', () => {
      const comment = 'See this said:\n\n > a quoted\n > string block\n\nOK?';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 3);
      assertBlock(result, 0, 'paragraph', 'See this said:');
      assert.equal(result[1].type, 'quote');
      assert.lengthOf(result[1].blocks, 1);
      assertBlock(result[1].blocks, 0, 'paragraph', 'a quoted\nstring block');
      assertBlock(result, 2, 'paragraph', 'OK?');
    });

    test('nested quotes', () => {
      const comment = ' > > prior\n > \n > next\n';
      const result = element._computeBlocks(comment);
      assert.lengthOf(result, 1);
      assert.equal(result[0].type, 'quote');
      assert.lengthOf(result[0].blocks, 2);
      assert.equal(result[0].blocks[0].type, 'quote');
      assert.lengthOf(result[0].blocks[0].blocks, 1);
      assertBlock(result[0].blocks[0].blocks, 0, 'paragraph', 'prior');
      assertBlock(result[0].blocks, 1, 'paragraph', 'next\n');
    });

    test('getTextContent', () => {
      const comment = 'Paragraph\n\n  pre\n\n* List\n* Of\n* Items\n\n> Quote';
      element.content = comment;
      const result = element.getTextContent();
      const expected = 'Paragraph\n\n  pre\n\nList\nOf\nItems\n\nQuote';
      assert.equal(result, expected);
    });

    test('_computeNodes called without config', () => {
      const computeNodesSpy = sandbox.spy(element, '_computeNodes');
      element.content = 'some text';
      assert.isTrue(computeNodesSpy.called);
    });

    test('_contentOrConfigChanged called with config', () => {
      const contentStub = sandbox.stub(element, '_contentChanged');
      const contentConfigStub = sandbox.stub(element, '_contentOrConfigChanged');
      element.content = 'some text';
      element.config = {};
      assert.isTrue(contentStub.called);
      assert.isTrue(contentConfigStub.called);
    });
  });
</script>