-
Notifications
You must be signed in to change notification settings - Fork 879
/
render_test.ts
1460 lines (1250 loc) · 48.2 KB
/
render_test.ts
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
import {AttributePart, directive, html, noChange, NodePart, nothing, Part, render, svg, templateFactory} from '../../lit-html.js';
import {stripExpressionMarkers} from '../test-utils/strip-markers.js';
const assert = chai.assert;
// tslint:disable:no-any OK in test code.
const isTemplatePolyfilled =
((HTMLTemplateElement as any).decorate != null ||
(window as any).ShadyDOM && (window as any).ShadyDOM.inUse);
const testSkipForTemplatePolyfill = (test: any) =>
isTemplatePolyfilled ? test.skip : test;
const isSafari10_0 =
(window.navigator.userAgent.indexOf('AppleWebKit/602') !== -1);
const testSkipSafari10_0 = (test: any) => isSafari10_0 ? test.skip : test;
const isChrome41 = (window.navigator.userAgent.indexOf('Chrome/41') !== -1);
const testSkipChrome41 = (test: any) => isChrome41 ? test.skip : test;
const testIfHasSymbol = (test: any) =>
(window as any).Symbol === undefined ? test.skip : test;
const ua = window.navigator.userAgent;
const isIe = ua.indexOf('Trident/') > 0;
const suiteIfCustomElementsAreSupported = (suite: any) =>
(window.customElements != null) ? suite : suite.skip;
suite('render()', () => {
let container: HTMLElement;
setup(() => {
container = document.createElement('div');
});
suite('text', () => {
test('renders plain text expression', () => {
render(html`test`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), 'test');
});
test('renders a string', () => {
render(html`<div>${'foo'}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo</div>');
});
test('renders a number', () => {
render(html`<div>${123}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>123</div>');
});
test('renders undefined', () => {
render(html`<div>${undefined}</div>`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
});
test('renders null', () => {
render(html`<div>${null}</div>`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
});
test('renders noChange', () => {
const template = (i: any) => html`<div>${i}</div>`;
render(template('foo'), container);
render(template(noChange), container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo</div>');
});
test('renders nothing', () => {
const template = (i: any) => html`<div>${i}</div>`;
render(template('foo'), container);
render(template(nothing), container);
const children = Array.from(container.querySelector('div')!.childNodes);
assert.isEmpty(
children.filter((node) => node.nodeType !== Node.COMMENT_NODE));
});
testIfHasSymbol(test)('renders a Symbol', () => {
render(html`<div>${Symbol('A')}</div>`, container);
assert.include(
container.querySelector('div')!.textContent!.toLowerCase(), 'symbol');
});
test('does not call a function bound to text', () => {
const f = () => {
throw new Error();
};
render(html`${f}`, container);
});
test('renders arrays', () => {
render(html`<div>${[1, 2, 3]}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>123</div>');
});
test('renders nested templates', () => {
const partial = html`<h1>${'foo'}</h1>`;
render(html`${partial}${'bar'}`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<h1>foo</h1>bar');
});
test('renders parts with whitespace after them', () => {
render(html`<div>${'foo'} </div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo </div>');
});
test('renders parts that look like attributes', () => {
render(html`<div>foo bar=${'baz'}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div>foo bar=baz</div>');
});
test('renders multiple parts per element, preserving whitespace', () => {
render(html`<div>${'foo'} ${'bar'}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo bar</div>');
});
test('values contain interpolated values', () => {
const t = html`${'a'},${'b'},${'c'}`;
render(t, container);
assert.equal(stripExpressionMarkers(container.innerHTML), 'a,b,c');
});
test('renders a template nested multiple times', () => {
const partial = html`<h1>${'foo'}</h1>`;
render(html`${partial}${'bar'}${partial}${'baz'}qux`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<h1>foo</h1>bar<h1>foo</h1>bazqux');
});
test('renders arrays of nested templates', () => {
render(html`<div>${[1, 2, 3].map((i) => html`${i}`)}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>123</div>');
});
// TODO(justinfagnani): add this test back with a test TemplateProcessor and
// html tag.
test.skip(
'overwrites an existing (plain) TemplateInstance if one exists, ' +
'even if it has a matching Template',
() => {
const t = (tag: any) => tag`<div>foo</div>`;
render(t(html), container);
assert.equal(container.children.length, 1);
const firstDiv = container.children[0];
assert.equal(firstDiv.textContent, 'foo');
render(t(html), container);
assert.equal(container.children.length, 1);
const secondDiv = container.children[0];
assert.equal(secondDiv.textContent, 'foo');
assert.notEqual(firstDiv, secondDiv);
});
test('renders an element', () => {
const child = document.createElement('p');
render(html`<div>${child}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div><p></p></div>');
});
test('renders an array of elements', () => {
const children = [
document.createElement('p'),
document.createElement('a'),
document.createElement('span')
];
render(html`<div>${children}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div><p></p><a></a><span></span></div>');
});
test('renders forms as elements', () => {
// forms are both Node and iterable
const form = document.createElement('form');
const inputOne = document.createElement('input');
inputOne.name = 'one';
const inputTwo = document.createElement('input');
inputTwo.name = 'two';
form.appendChild(inputOne);
form.appendChild(inputTwo);
render(html`${form}`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<form><input name="one"><input name="two"></form>');
});
test('renders SVG', () => {
const container = document.createElement('svg');
const t = svg`<line y1="1" y2="1"/>`;
render(t, container);
const line = container.firstElementChild!;
assert.equal(line.tagName, 'line');
assert.equal(line.namespaceURI, 'http://www.w3.org/2000/svg');
});
test('renders SVG with bindings', () => {
const container = document.createElement('svg');
const pathDefinition = 'M7 14l5-5 5 5z';
const t = svg`<path d="${pathDefinition}" />`;
render(t, container);
const path = container.firstElementChild as SVGPathElement;
// IE and Edge rewrite paths, so check a normalized value too
assert.include(
['M7 14l5-5 5 5z', 'M 7 14 l 5 -5 l 5 5 Z'],
path.getAttribute('d'),
pathDefinition);
});
test('renders templates with comments', () => {
const t = html`
<div>
<!-- this is a comment -->
<h1 class="${'foo'}">title</h1>
<p>${'foo'}</p>
</div>`;
render(t, container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<div>
<!-- this is a comment -->
<h1 class="foo">title</h1>
<p>foo</p>
</div>`);
});
test('renders comments with bindings', () => {
const t = html`
<!-- ${'foo'} -->
<p>${'bar'}</p>`;
render(t, container);
assert.equal(container.querySelector('p')!.textContent, 'bar');
});
test('renders comments with attribute-like bindings', () => {
const t = html`
<!-- <div class="${'foo'}"></div> -->
<p>${'bar'}</p>`;
render(t, container);
assert.equal(container.querySelector('p')!.textContent, 'bar');
});
test('renders comments with bindings followed by attr', () => {
const t = html`
<!-- ${'foo'} -->
<p bar=${'baz'}></p>`;
render(t, container);
assert.equal(container.querySelector('p')!.getAttribute('bar'), 'baz');
});
test('renders comments with multiple bindings', () => {
const t = html`
<!-- <div class="${'foo'}">${'bar'}</div> -->
<p>${'baz'}</p>`;
render(t, container);
assert.equal(container.querySelector('p')!.textContent, 'baz');
});
test('handles comments with bindings', () => {
const t = html`<p>A<!-- ${'B'} ${'C'} -->D</p>`;
render(t, container);
// Use innerText instead of textContent because of a crazy bug in
// Chrome 41 where textContent would include the textContent of comments!
assert.equal(container.querySelector('p')!.innerText, 'AD');
});
test('handles elements in comments with attribute bindings', () => {
const t = html`A<!-- <div foo=${'bar'}>B</div>${'baz'} -->C`;
render(t, container);
// Use innerText instead of textContent because of a crazy bug in
// Chrome 41 where textContent would include the textContent of comments!
assert.equal(container.innerText, 'AC');
});
test('handles attribute bindings with comment-like values', () => {
const t = html`A<div foo="<!--${'bar'}">B</div>C`;
render(t, container);
assert.equal(container.textContent, 'ABC');
});
test('handles comments with attribute-like content', () => {
const t = html`A<!-- foo=${'bar'}-->B`;
render(t, container);
// Use innerText instead of textContent because of a crazy bug in
// Chrome 41 where textContent would include the textContent of
// comments!
assert.equal(container.innerText, 'AB');
});
test('handles elements in comments with node bindings', () => {
const t = html`A<!-- <div>${'B'}</div>${'baz'} -->C`;
render(t, container);
// Use innerText instead of textContent because of a crazy bug in
// Chrome 41 where textContent would include the textContent of comments!
assert.equal(container.innerText, 'AC');
});
test('renders comments with multiple bindings followed by attr', () => {
const t = html`
<!-- ${'foo'} ${'bar'} -->
<p baz=${'qux'}></p>`;
render(t, container);
assert.equal(container.querySelector('p')!.getAttribute('baz'), 'qux');
});
test('does not break with an attempted dynamic start tag', () => {
// this won't work, but we'd like it to not throw an exception or break
// other bindings
render(html`<${'div'}></div><p>${'foo'}</p>`, container);
assert.equal(container.querySelector('p')!.textContent, 'foo');
});
test('does not break with an attempted dynamic end tag', () => {
// this won't work, but we'd like it to not throw an exception or break
// other bindings
render(html`<div></${'div'}><p>${'foo'}</p>`, container);
assert.equal(container.querySelector('p')!.textContent, 'foo');
});
test('renders legacy marker sequences in text nodes', () => {
// {{}} used to be the marker text and it was important to test that
// markers in user-templates weren't interpreted as expressions
const result = html`{{}}`;
assert.equal(templateFactory(result).parts.length, 0);
render(result, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '{{}}');
});
test('renders expressions with preceding elements', () => {
render(html`<a>${'foo'}</a>${html`<h1>${'bar'}</h1>`}`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<a>foo</a><h1>bar</h1>');
// This is nearly the same test case as above, but was causing a
// different stack trace
render(html`<a>${'foo'}</a>${'bar'}`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<a>foo</a>bar');
});
});
suite('attributes', () => {
test('renders to an attribute', () => {
render(html`<div foo="${'bar'}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo="bar"></div>');
});
testIfHasSymbol(test)('renders a Symbol to an attribute', () => {
render(html`<div foo=${Symbol('A')}></div>`, container);
assert.include(
container.querySelector('div')!.getAttribute('foo')!.toLowerCase(),
'symbol');
});
testIfHasSymbol(
test)('renders a Symbol in an array to an attribute', () => {
render(html`<div foo=${[Symbol('A')]}></div>`, container);
assert.include(
container.querySelector('div')!.getAttribute('foo')!.toLowerCase(),
'symbol');
});
test('renders multiple bindings in an attribute', () => {
render(html`<div foo="a${'b'}c${'d'}e"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div foo="abcde"></div>');
});
test('renders two attributes on one element', () => {
const result = html`<div a="${1}" b="${2}"></div>`;
render(result, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div a="1" b="2"></div>');
});
test('renders a binding in a style attribute', () => {
const t = html`<div style="color: ${'red'}"></div>`;
render(t, container);
if (isIe) {
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div style="color: red;"></div>');
} else {
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div style="color: red"></div>');
}
});
test('renders multiple bindings in a style attribute', () => {
const t = html`<div style="${'color'}: ${'red'}"></div>`;
render(t, container);
if (isIe) {
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div style="color: red;"></div>');
} else {
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div style="color: red"></div>');
}
});
test('renders a binding in a class attribute', () => {
render(html`<div class="${'red'}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div class="red"></div>');
});
test('renders a binding in an input value attribute', () => {
render(html`<input value="${'the-value'}">`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<input value="the-value">');
assert.equal(container.querySelector('input')!.value, 'the-value');
});
test('renders a case-sensitive attribute', () => {
const size = 100;
render(html`<svg viewBox="0 0 ${size} ${size}"></svg>`, container);
assert.include(
stripExpressionMarkers(container.innerHTML), 'viewBox="0 0 100 100"');
// Make sure non-alpha valid attribute name characters are handled
render(html`<svg view_Box="0 0 ${size} ${size}"></svg>`, container);
assert.include(
stripExpressionMarkers(container.innerHTML),
'view_Box="0 0 100 100"');
});
test(
'renders to an attribute expression after an attribute literal', () => {
render(html`<div a="b" foo="${'bar'}"></div>`, container);
// IE and Edge can switch attribute order!
assert.include(
['<div a="b" foo="bar"></div>', '<div foo="bar" a="b"></div>'],
stripExpressionMarkers(container.innerHTML));
});
test(
'renders to an attribute expression before an attribute literal',
() => {
render(html`<div foo="${'bar'}" a="b"></div>`, container);
// IE and Edge can switch attribute order!
assert.include(
['<div a="b" foo="bar"></div>', '<div foo="bar" a="b"></div>'],
stripExpressionMarkers(container.innerHTML));
});
// Regression test for exception in template parsing caused by attributes
// reordering when a attribute binding precedes an attribute literal.
test('renders attribute binding after attribute binding that moved', () => {
render(
html`<a href="${'foo'}" class="bar"><div id=${'a'}></div></a>`,
container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
`<a class="bar" href="foo"><div id="a"></div></a>`);
});
test('renders to an attribute without quotes', () => {
render(html`<div foo=${'bar'}></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo="bar"></div>');
});
test('renders to multiple attribute expressions', () => {
render(
html`<div foo="${'Foo'}" bar="${'Bar'}" baz=${'Baz'}></div>`,
container);
assert.oneOf(stripExpressionMarkers(container.innerHTML), [
'<div foo="Foo" bar="Bar" baz="Baz"></div>',
'<div foo="Foo" baz="Baz" bar="Bar"></div>',
'<div bar="Bar" foo="Foo" baz="Baz"></div>'
]);
});
test('renders to attributes with attribute-like values', () => {
render(html`<div foo="bar=${'foo'}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div foo="bar=foo"></div>');
});
test('renders interpolation to an attribute', () => {
render(html`<div foo="1${'bar'}2${'baz'}3"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div foo="1bar2baz3"></div>');
});
test('does not call a function bound to an attribute', () => {
const f = () => {
throw new Error();
};
render(html`<div foo=${f}></div>`, container);
const div = container.querySelector('div')!;
assert.isTrue(div.hasAttribute('foo'));
});
test('renders an array to an attribute', () => {
render(html`<div foo=${[1, 2, 3]}></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo="123"></div>');
});
test('renders to an attribute before a node', () => {
render(html`<div foo="${'bar'}">${'baz'}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div foo="bar">baz</div>');
});
test('renders to an attribute after a node', () => {
render(html`<div>${'baz'}</div><div foo="${'bar'}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div>baz</div><div foo="bar"></div>');
});
test('renders undefined in attributes', () => {
render(html`<div attribute="it's ${undefined}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<div attribute="it\'s undefined"></div>');
});
});
suite('properties', () => {
test('sets properties', () => {
render(html`<div .foo=${123} .bar=${456}></div>`, container);
const div = container.querySelector('div')!;
assert.strictEqual((div as any).foo, 123);
assert.strictEqual((div as any).bar, 456);
});
});
suite('boolean attributes', () => {
test('renders a boolean attribute as an empty string when truthy', () => {
const t = (value: any) => html`<div ?foo="${value}"></div>`;
render(t(true), container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo=""></div>');
render(t('a'), container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo=""></div>');
render(t(1), container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo=""></div>');
});
test('removes a boolean attribute when falsey', () => {
const t = (value: any) => html`<div ?foo="${value}"></div>`;
render(t(false), container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
render(t(0), container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
render(t(null), container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
render(t(undefined), container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
});
});
suite('events', () => {
setup(() => {
document.body.appendChild(container);
});
teardown(() => {
document.body.removeChild(container);
});
test('adds event listener functions, calls with right this value', () => {
let thisValue;
let event: Event|undefined = undefined;
const listener = function(this: any, e: any) {
event = e;
thisValue = this;
};
const eventContext = {} as EventTarget;
render(html`<div @click=${listener}></div>`, container, {eventContext});
const div = container.querySelector('div')!;
div.click();
if (event === undefined) {
throw new Error(`Event listener never fired!`);
}
assert.equal(thisValue, eventContext);
// MouseEvent is not a function in IE, so the event cannot be an instance
// of it
if (typeof MouseEvent === 'function') {
assert.instanceOf(event, MouseEvent);
} else {
assert.isDefined((event as MouseEvent).initMouseEvent);
}
});
test('adds event listener objects, calls with right this value', () => {
let thisValue;
const listener = {
handleEvent(_e: Event) {
thisValue = this;
}
};
const eventContext = {} as EventTarget;
render(html`<div @click=${listener}></div>`, container, {eventContext});
const div = container.querySelector('div')!;
div.click();
assert.equal(thisValue, listener);
});
test('only adds event listener once', () => {
let count = 0;
const listener = () => {
count++;
};
render(html`<div @click=${listener}></div>`, container);
render(html`<div @click=${listener}></div>`, container);
const div = container.querySelector('div')!;
div.click();
assert.equal(count, 1);
});
test('allows updating event listener', () => {
let count1 = 0;
const listener1 = () => {
count1++;
};
let count2 = 0;
const listener2 = () => {
count2++;
};
const t = (listener: () => void) => html`<div @click=${listener}></div>`;
render(t(listener1), container);
render(t(listener2), container);
const div = container.querySelector('div')!;
div.click();
assert.equal(count1, 0);
assert.equal(count2, 1);
});
test(
'allows updating event listener without extra calls to remove/addEventListener',
() => {
let listener: Function|null;
const t = () => html`<div @click=${listener}></div>`;
render(t(), container);
const div = container.querySelector('div')!;
let addCount = 0;
let removeCount = 0;
div.addEventListener = () => addCount++;
div.removeEventListener = () => removeCount++;
listener = () => {};
render(t(), container);
assert.equal(addCount, 1);
assert.equal(removeCount, 0);
listener = () => {};
render(t(), container);
assert.equal(addCount, 1);
assert.equal(removeCount, 0);
listener = null;
render(t(), container);
assert.equal(addCount, 1);
assert.equal(removeCount, 1);
listener = () => {};
render(t(), container);
assert.equal(addCount, 2);
assert.equal(removeCount, 1);
listener = () => {};
render(t(), container);
assert.equal(addCount, 2);
assert.equal(removeCount, 1);
});
test('removes event listeners', () => {
let target;
let listener: any = (e: any) => target = e.target;
const t = () => html`<div @click=${listener}></div>`;
render(t(), container);
const div = container.querySelector('div')!;
div.click();
assert.equal(target, div);
listener = null;
target = undefined;
render(t(), container);
div.click();
assert.equal(target, undefined);
});
test('allows capturing events', () => {
let event!: Event;
let eventPhase!: number;
const listener = {
handleEvent(e: Event) {
event = e;
// read here because it changes
eventPhase = event.eventPhase;
},
capture: true,
};
render(
html`
<div id="outer" @test=${listener}>
<div id="inner"><div>
</div>
`,
container);
const inner = container.querySelector('#inner')!;
inner.dispatchEvent(new Event('test'));
assert.isOk(event);
assert.equal(eventPhase, Event.CAPTURING_PHASE);
});
});
suite('directives', () => {
test('renders directives on NodeParts', () => {
const fooDirective = directive(() => (part: Part) => {
part.setValue('foo');
});
render(html`<div>${fooDirective()}</div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div>foo</div>');
});
test('renders directives on AttributeParts', () => {
const fooDirective = directive(() => (part: AttributePart) => {
part.setValue('foo');
});
render(html`<div foo="${fooDirective()}"></div>`, container);
assert.equal(
stripExpressionMarkers(container.innerHTML), '<div foo="foo"></div>');
});
test('renders directives on PropertyParts', () => {
const fooDirective = directive(() => (part: AttributePart) => {
part.setValue(1234);
});
render(html`<div .foo="${fooDirective()}"></div>`, container);
assert.equal(stripExpressionMarkers(container.innerHTML), '<div></div>');
assert.strictEqual((container.firstElementChild as any).foo, 1234);
});
testSkipChrome41(test)(
'event listeners can see events fired by dynamic children', () => {
// This tests that node directives are called in the commit phase, not
// the setValue phase
let event: Event|undefined = undefined;
document.body.appendChild(container);
render(
html`
<div @test-event=${(e: Event) => {
event = e;
}}>
${directive(() => (part: NodePart) => {
// This emulates a custom element that fires an event in its
// connectedCallback
part.startNode.dispatchEvent(new CustomEvent('test-event', {
bubbles: true,
}));
})()}
</div>`,
container);
document.body.removeChild(container);
assert.isOk(event);
});
test(
'event listeners can see events fired directives in AttributeParts',
() => {
// This tests that attribute directives are called in the commit
// phase, not the setValue phase
let event = undefined;
const fire = directive(() => (part: AttributePart) => {
part.committer.element.dispatchEvent(new CustomEvent('test-event', {
bubbles: true,
}));
});
render(
html`<div @test-event=${(e: Event) => {
event = e;
}} b=${fire()}></div>`,
container);
assert.isOk(event);
});
});
suite('<table>', () => {
testSkipForTemplatePolyfill(
test)('renders nested templates within table content', () => {
let table = html`<table>${html`<tr>${html`<td></td>`}</tr>`}</table>`;
render(table, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<table><tr><td></td></tr></table>');
table = html`<tbody>${html`<tr></tr>`}</tbody>`;
render(table, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<tbody><tr></tr></tbody>');
table = html`<table><tr></tr>${html`<tr></tr>`}</table>`;
render(table, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<table><tbody><tr></tr><tr></tr></tbody></table>');
table = html`<table><tr><td></td>${html`<td></td>`}</tr></table>`;
render(table, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<table><tbody><tr><td></td><td></td></tr></tbody></table>');
table = html`<table><tr><td></td>${html`<td></td>`}${
html`<td></td>`}</tr></table>`;
render(table, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
'<table><tbody><tr><td></td><td></td><td></td></tr></tbody></table>');
});
// On Safari 10.0 (but not 10.1), the attribute value "<table>" is
// escaped to "<table>". That shouldn't cause this test to
// fail, so we skip
testSkipSafari10_0(test)(
'renders quoted attributes with "<table>" before an expression', () => {
const template = html`<div a="<table>${'foo'}"></div>`;
render(template, container);
assert.equal(
stripExpressionMarkers(container.innerHTML),
`<div a="<table>foo"></div>`);
});
});
suite('<style>', () => {
test('renders no comments inside textContent', () => {
render(html`<style>${''}</style>`, container);
assert.equal(container.firstElementChild!.textContent, '');
});
test('renders style tags with expressions correctly', () => {
const color = 'red';
const t = html`
<style>
h1 {
color: ${color};
}
</style>`;
render(t, container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<style>
h1 {
color: red;
}
</style>`);
});
test('renders an attribute after a style binding', () => {
render(
html`
<style>
.foo {
background: ${'black'};
}
</style>
<a href="/buy/${'foo'}"></a>
`,
container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<style>
.foo {
background: black;
}
</style>
<a href="/buy/foo"></a>
`);
});
test('renders an attribute after empty style node binding', () => {
// This test is sensitive to the exact binding in the style tag.
// Make sure the binding takes up the whole element with no text
// on either side of it
// Work around a false positive lit-html pluging lint error:
const h = html;
render(
h`
<style>${'bar'}</style>
<a href="/buy/${'foo'}"></a>
`,
container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<style>bar</style>
<a href="/buy/foo"></a>
`);
});
});
suite('<template>', () => {
test('bindings work in <template>', () => {
const t = (a: string, b: string) => html`
<template>
<span>${a}</span>
</template>
<div>${b}</div>
`;
render(t('1', '2'), container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<template>
<span>1</span>
</template>
<div>2</div>
`);
});
test('bindings work in <template> with attribute bindings', () => {
const t = (a: string, b: string, c: string) => html`
<template foo=${a}>
<span>${b}</span>
</template>
<div>${c}</div>
`;
render(t('1', '2', '3'), container);
assert.equal(stripExpressionMarkers(container.innerHTML), `
<template foo="1">
<span>2</span>
</template>
<div>3</div>
`);
});
});
suite('text and attributes', () => {
test('renders attributes bindings after text bindings', () => {
render(
html`
<div>${''}</div>
<div foo=${'bar'}></div>
`,
container);