-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathplayground-code-editor_test.ts
More file actions
691 lines (595 loc) · 19.6 KB
/
playground-code-editor_test.ts
File metadata and controls
691 lines (595 loc) · 19.6 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import {assert} from '@esm-bundle/chai';
import '../playground-code-editor.js';
import {PlaygroundCodeEditor} from '../playground-code-editor.js';
import {sendKeys} from '@web/test-runner-commands';
import {EditorView} from '@codemirror/view';
import {undo} from '@codemirror/commands';
const raf = async () => new Promise((r) => requestAnimationFrame(r));
const wait = async (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
suite('playground-code-editor', () => {
let container: HTMLDivElement;
setup(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
teardown(() => {
container.remove();
});
test('is registered', () => {
assert.instanceOf(
document.createElement('playground-code-editor'),
PlaygroundCodeEditor
);
});
test('renders initial value', async () => {
const editor = document.createElement('playground-code-editor');
editor.value = 'foo';
container.appendChild(editor);
await editor.updateComplete;
assert.include(editor.shadowRoot!.innerHTML, 'foo');
});
test('renders updated value', async () => {
const editor = document.createElement('playground-code-editor');
editor.value = 'foo';
container.appendChild(editor);
await editor.updateComplete;
editor.value = 'bar';
await editor.updateComplete;
assert.include(editor.shadowRoot!.innerHTML, 'bar');
});
test('dispatches change event', async () => {
const editor = document.createElement('playground-code-editor');
editor.value = 'foo';
container.appendChild(editor);
await editor.updateComplete;
await new Promise<void>((resolve) => {
editor.addEventListener('change', () => resolve());
const editorInternals = editor as unknown as {
_editorView: EditorView;
};
// Update the text via a CodeMirror 6 transaction
editorInternals._editorView!.dispatch({
changes: {
from: 0,
to: editorInternals._editorView!.state.doc.length,
insert: 'bar',
},
});
});
});
test('does NOT dispatch change event when switching documentKey and value together', async () => {
const editor = document.createElement('playground-code-editor');
const DOCUMENT_KEY1 = {doc: 1};
const DOCUMENT_KEY2 = {doc: 2};
editor.value = 'document 1';
editor.documentKey = DOCUMENT_KEY1;
container.appendChild(editor);
await editor.updateComplete;
await raf();
let changeCount = 0;
editor.addEventListener('change', () => {
changeCount++;
});
// This simulates what playground-file-editor does when switching files
// Both value AND documentKey change together in a single update
editor.value = 'document 2';
editor.documentKey = DOCUMENT_KEY2;
// Wait for Lit's update cycle
await editor.updateComplete;
await raf();
// Wait for any potential async change events
await wait(100);
// Switch back - this is where the bug manifests in lit.dev
editor.value = 'document 1 modified';
editor.documentKey = DOCUMENT_KEY1;
await editor.updateComplete;
await raf();
await wait(100);
assert.equal(
changeCount,
0,
'Switching documentKey and value together should not fire change event'
);
});
test('does NOT dispatch change event when switching documentKey', async () => {
const editor = document.createElement('playground-code-editor');
const DOCUMENT_KEY1 = {dockey: 1};
const DOCUMENT_KEY2 = {dockey: 2};
editor.value = 'document 1';
editor.documentKey = DOCUMENT_KEY1;
container.appendChild(editor);
await editor.updateComplete;
await raf();
let changeCount = 0;
editor.addEventListener('change', () => changeCount++);
// Switch to a different document - should NOT fire change event
editor.value = 'document 2';
editor.documentKey = DOCUMENT_KEY2;
await editor.updateComplete;
await raf();
assert.equal(
changeCount,
0,
'Switching documentKey should not fire change event'
);
// Switch back to first document - should NOT fire change event
editor.value = 'document 1';
editor.documentKey = DOCUMENT_KEY1;
await editor.updateComplete;
await raf();
assert.equal(
changeCount,
0,
'Switching back to original documentKey should not fire change event'
);
});
test('dispatches change event only for user edits, not programmatic changes', async () => {
const editor = document.createElement('playground-code-editor');
editor.value = 'foo';
container.appendChild(editor);
await editor.updateComplete;
await raf();
const editorInternals = editor as unknown as {
_editorView: EditorView;
};
let changeCount = 0;
editor.addEventListener('change', () => changeCount++);
// Programmatic change - should NOT fire
editor.value = 'bar';
await editor.updateComplete;
await raf();
assert.equal(changeCount, 0, 'No change event after programmatic update');
// User edit via CodeMirror - SHOULD fire
editorInternals._editorView.dispatch({
changes: {
from: 0,
to: editorInternals._editorView.state.doc.length,
insert: 'baz',
},
});
await raf();
assert.equal(changeCount, 1, 'Change event fired after user edit');
// Another programmatic change - should NOT fire
editor.value = 'qux';
await editor.updateComplete;
await raf();
assert.equal(
changeCount,
1,
'No additional change event after another programmatic update'
);
});
suite('history', () => {
let editor: PlaygroundCodeEditor;
let editorInternals: {
_editorView: EditorView;
};
setup(async () => {
editor = document.createElement('playground-code-editor');
// For correct history, CodeMirror needs to be initialized and attached to
// the DOM.
container.appendChild(editor);
await raf();
editorInternals = editor as unknown as typeof editorInternals;
});
teardown(() => {
editor.remove();
});
test(`and doc instance cache won't drive editor value`, async () => {
const DOCUMENT_KEY1 = {dockey: 1};
const DOCUMENT_KEY2 = {dockey: 2};
editor.value = 'document key 1';
editor.documentKey = DOCUMENT_KEY1;
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 1'
);
editor.value = 'document key 2';
editor.documentKey = DOCUMENT_KEY2;
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 2'
);
// If only the documentKey is changed, the current value is set on the
// document cache. The `value` property drives the CodeMirror contents.
editor.documentKey = DOCUMENT_KEY1;
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 2'
);
// Changing documentKey and unsetting the value should clear the editor.
editor.value = undefined;
editor.documentKey = DOCUMENT_KEY2;
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), '');
// Unset the cache should result in a
editor.documentKey = undefined;
editor.value = 'value with no cache';
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'value with no cache'
);
});
test(`is cleared on unsetting documentKey`, async () => {
const DOCUMENT_KEY1 = {dockey: 1};
editor.value = 'no cache';
await raf();
editor.documentKey = DOCUMENT_KEY1;
await raf();
editor.value = 'update on cache';
await raf();
editor.documentKey = undefined;
await raf();
assert.equal(
editorInternals._editorView!.state.doc.toString(),
'update on cache'
);
// No-op, because unsetting documentKey clears history.
undo(editorInternals._editorView!);
await raf();
assert.equal(
editorInternals._editorView!.state.doc.toString(),
'update on cache'
);
await raf();
editor.value = 'changed';
await raf();
assert.equal(
editorInternals._editorView!.state.doc.toString(),
'changed'
);
undo(editorInternals._editorView!);
await raf();
assert.equal(
editorInternals._editorView!.state.doc.toString(),
'update on cache'
);
});
test('is updated if value gets changed with doc cache', async () => {
const DOCUMENT_KEY1 = {dockey: 1};
const DOCUMENT_KEY2 = {dockey: 2};
editor.value = 'document key 1';
editor.documentKey = DOCUMENT_KEY1;
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 1'
);
editor.value = 'document key 2';
editor.documentKey = DOCUMENT_KEY2;
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 2'
);
editor.documentKey = DOCUMENT_KEY1;
editor.value = 'override document key 1';
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'override document key 1'
);
undo(editorInternals._editorView);
await raf();
assert.equal(
editorInternals._editorView.state.doc.toString(),
'document key 1'
);
});
test('is maintained without using documentKey', async () => {
editor.value = 'foo';
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'foo');
editor.value = 'bar';
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'bar');
undo(editorInternals._editorView);
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'foo');
});
test('is maintained with a document key', async () => {
const DOCUMENT_KEY1 = {};
editor.documentKey = DOCUMENT_KEY1;
editor.value = 'foo';
await editor.updateComplete;
assert.equal(editorInternals._editorView.state.doc.toString(), 'foo');
editor.value = 'bar';
await editor.updateComplete;
assert.equal(editorInternals._editorView.state.doc.toString(), 'bar');
undo(editorInternals._editorView);
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'foo');
});
test('is associated to the documentKey property', async () => {
const DOCUMENT_KEY1 = {};
const DOCUMENT_KEY2 = {};
editor.documentKey = DOCUMENT_KEY1;
editor.value = 'foo';
await editor.updateComplete;
editor.value = 'potato';
editor.documentKey = DOCUMENT_KEY2;
await editor.updateComplete;
assert.equal(editorInternals._editorView.state.doc.toString(), 'potato');
undo(editorInternals._editorView);
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'potato');
});
test('can be rehydrated from a saved document instance', async () => {
const DOCUMENT_KEY1 = {};
const DOCUMENT_KEY2 = {};
editor.documentKey = DOCUMENT_KEY1;
editor.value = 'foo';
await raf();
editor.value = 'bar';
await raf();
editor.documentKey = DOCUMENT_KEY2;
await raf();
editor.value = 'potato';
await raf();
editor.documentKey = DOCUMENT_KEY1;
editor.value = 'bar';
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'bar');
undo(editorInternals._editorView);
await raf();
assert.equal(editorInternals._editorView.state.doc.toString(), 'foo');
});
});
suite('syntax highlight', () => {
/**
* Traverse the given root node depth first, and return the first node whose
* trimmed text content is exactly equal to the given text.
*/
const findNodeWithText = (node: Node, text: string): Node | undefined => {
if (node.textContent?.trim() === text) {
return node;
}
for (const child of Array.from(node.childNodes)) {
const r = findNodeWithText(child, text);
if (r) {
return r;
}
}
return undefined;
};
const assertHighlight = async (
type: PlaygroundCodeEditor['type'],
value: string,
text: string,
color: string
) => {
const editor = document.createElement('playground-code-editor');
editor.type = type;
editor.value = value;
container.appendChild(editor);
await editor.updateComplete;
const element = findNodeWithText(editor.shadowRoot!, text);
assert.isDefined(element);
const style = window.getComputedStyle(element as HTMLElement);
assert.equal(style.color, color);
};
const typeColor = 'rgb(0, 136, 85)';
const boolColor = 'rgb(34, 17, 153)';
const keywordColor = 'rgb(119, 0, 136)';
const literalColor = 'rgb(34, 17, 153)';
const stringColor = 'rgb(170, 17, 17)';
test('html', async () =>
assertHighlight('html', '<p>foo</p>', 'p', typeColor));
test('css', async () =>
assertHighlight('css', 'p { color: blue; }', 'blue', literalColor));
test('js', async () =>
assertHighlight('js', 'if (true) {}', 'true', boolColor));
test('html-in-js', async () =>
assertHighlight(
'js',
'import {LitElement, html} from "lit";',
'import',
keywordColor
));
test('ts', async () =>
assertHighlight('ts', 'const x: string;', 'string', typeColor));
test('jsx', async () =>
assertHighlight('jsx', 'const foo = () => <p>foo</p>;', 'p', typeColor));
test('tsx', async () =>
assertHighlight(
'tsx',
'const x: () => unknown = () => <p>foo</p>;',
'p',
typeColor
));
test('html-in-js', async () =>
assertHighlight('js', 'html`<p>foo</p>`', 'p', typeColor));
test('html-in-ts', async () =>
assertHighlight('ts', 'html`<p>foo</p>`', 'p', typeColor));
test('css-in-js', async () =>
assertHighlight('js', 'css`p { color: blue; }`', 'blue', literalColor));
test('css-in-ts', async () =>
assertHighlight('ts', 'css`p { color: blue; }`', 'blue', literalColor));
test('json', async () =>
assertHighlight('json', '{"foo": 123}', '"foo"', stringColor));
});
suite('comment toggle', () => {
async function assertToggle(
type: PlaygroundCodeEditor['type'],
value: string,
expect: string
) {
const editor = document.createElement('playground-code-editor');
editor.type = type;
editor.value = value;
container.appendChild(editor);
await editor.updateComplete;
await raf();
await raf();
const focusContainer =
editor.shadowRoot!.querySelector<HTMLDivElement>('#focusContainer')!;
editor.focus();
await wait(100);
// The Meta key might not be available on all test platforms, so we instead
// send both Meta+/ and Control+/ to toggle comments.
await sendKeys({
down: 'Meta',
});
await sendKeys({
press: 'Slash',
});
await sendKeys({
up: 'Meta',
});
await sendKeys({
down: 'Control',
});
await sendKeys({
press: 'Slash',
});
await sendKeys({
up: 'Control',
});
await wait(100);
await raf();
await editor.updateComplete;
assert.include(focusContainer.innerText, expect);
await sendKeys({
down: 'Meta',
});
await sendKeys({
press: 'Slash',
});
await sendKeys({
up: 'Meta',
});
await sendKeys({
down: 'Control',
});
await sendKeys({
press: 'Slash',
});
await sendKeys({
up: 'Control',
});
await wait(100);
await raf();
await editor.updateComplete;
assert.include(focusContainer.innerText, value);
}
test('ts', async () =>
assertToggle('ts', 'const g = 3;', '// const g = 3;'));
test('js', async () =>
assertToggle('js', 'const g = 3;', '// const g = 3;'));
test('html', async () =>
assertToggle('html', '<p>foo</p>', '<!-- <p>foo</p> -->'));
test('css', async () =>
assertToggle('css', 'p { color: blue; }', '/* p { color: blue; } */'));
test('ignored when readonly', async () => {
const editor = document.createElement('playground-code-editor');
editor.type = 'ts';
editor.readonly = true;
editor.value = 'const g = 3;';
container.appendChild(editor);
await editor.updateComplete;
// TODO(aomarks) There's some flakiness here that requires a little pause.
await raf();
editor.focus();
await sendKeys({
down: 'Control',
});
await sendKeys({
press: 'Slash',
});
await sendKeys({
up: 'Control',
});
await raf();
assert.include(
// There isn't a focusContainer when the editor is in readonly mode.
editor.shadowRoot!.querySelector<HTMLDivElement>('div')!.innerText,
'const g = 3;'
);
});
});
suite('tokenUnderCursor', () => {
let editor: PlaygroundCodeEditor;
let editorInternals: {
_editorView: EditorView;
};
setup(async () => {
editor = document.createElement('playground-code-editor');
container.appendChild(editor);
await raf();
editorInternals = editor as unknown as typeof editorInternals;
});
teardown(() => {
editor.remove();
});
test('returns token for JavaScript identifier', async () => {
editor.type = 'js';
editor.value = 'const myVariable = 42;';
await raf();
// Position cursor inside 'myVariable'
const pos = 'const my'.length;
editorInternals._editorView.dispatch({
selection: {anchor: pos, head: pos},
});
await raf();
const token = editor.tokenUnderCursor;
assert.equal(token.string, 'myVariable');
assert.equal(token.start, 6); // After 'const '
assert.equal(token.end, 16); // Before ' = 42'
});
test('returns token for HTML tag', async () => {
editor.type = 'html';
editor.value = '<div class="container"></div>';
await raf();
const pos = '<d'.length;
editorInternals._editorView.dispatch({
selection: {anchor: pos, head: pos},
});
await raf();
const token = editor.tokenUnderCursor;
assert.equal(token.string, 'div');
});
test('returns token for CSS property', async () => {
editor.type = 'css';
editor.value = 'body { color: red; }';
await raf();
const pos = 'body { co'.length;
editorInternals._editorView.dispatch({
selection: {anchor: pos, head: pos},
});
await raf();
const token = editor.tokenUnderCursor;
assert.equal(token.string, 'color');
});
test('handles empty document', async () => {
editor.type = 'js';
editor.value = '';
await raf();
const token = editor.tokenUnderCursor;
assert.equal(token.string, '');
assert.equal(token.start, 0);
assert.equal(token.end, 0);
});
test('handles cursor at punctuation', async () => {
editor.type = 'js';
editor.value = 'obj.property';
await raf();
const pos = 'obj'.length;
editorInternals._editorView.dispatch({
selection: {anchor: pos, head: pos},
});
await raf();
const token = editor.tokenUnderCursor;
assert.isTrue(token.string.length > 0);
});
});
});