This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.html
897 lines (800 loc) · 25 KB
/
test.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>RetroV Test Suite</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background: #000; color: #FFF;
font-size: 18px;
max-width: 700px;
margin: auto;
}
#test_container { margin-top: 1em; }
.result-box {
display: inline-block;
padding: 4px;
border: 1px solid silver;
margin: 2px;
}
.assert-box {
display: inline-block;
padding: 4px;
color: #000;
}
.assert-box + .assert-box {
margin-left: 4px; /* space between assert boxes */
}
.good { background: #af5; }
.bad { background: red; }
.tooltip {
visibility: hidden;
border: 1px solid white;
background: black;
position: absolute;
margin: -30px 20px;
padding: 5px;
}
/* click or hover shows tooltip with test description */
.result-box:hover .tooltip, .result-box:focus .tooltip {
visibility: visible;
}
.result-box:focus {
background: white;
}
.fatal {
background: red;
margin: 1em;
padding: 2em;
}
</style>
</head>
<body>
<h1>RetroV Test Suite</h1>
<p>If all goes well, you should see a bunch of green boxes and the console will
be clear of errors. Roll over (or click on) a test box to see a description.
</p>
<script>
function test(output_id, container_id){
// Hint:
// If an assertion about the contents of the container fails,
// a fast way to see what was rendered is to put this after
// the render:
//
// output.append(container.innerHTML);
//
var output = document.getElementById(output_id);
var container = document.getElementById(container_id);
var current_test = "not started";
var asserts = [];
var test_count = 0;
var assert_count = 0;
window.onerror = function(message, url, line_num){
var fatal_box = document.createElement('div');
fatal_box.innerHTML = "Fatal error while running: <b>'"
+ current_test +
"'</b> See console for details.";
fatal_box.className = 'fatal';
output.append(fatal_box);
// okay, we're done, false doesn't stop the error from getting
// to the default handler (browser)
return false;
}
function start_test(description, v){
// clear stuff for new test
container.replaceChildren();
delete container.rv_old_v;
current_test = description;
asserts = [];
// now render the first v tree
RV.render(container, v);
}
function add_render(v){
RV.render(container, v);
}
function end_test(){
var box = document.createElement('div');
box.className = 'result-box';
box.tabIndex = test_count+1;
var tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.innerHTML = current_test;
box.append(tooltip);
// loop over assert results
asserts.forEach(function(ar, i){
var result = document.createElement('div');
result.className = "assert-box " + (ar ? "good" : "bad");
result.innerHTML = i+1;
box.append(result);
});
output.append(box);
test_count++;
}
function assert(maybe){
asserts.push(maybe);
if(!maybe){
console.error("Failed:",current_test," assert #",asserts.length);
}
assert_count++;
}
function $(query){
return container.querySelector(query);
}
/* =======================================================================
* The Tests
* =======================================================================
*/
// First, tests for rendering in increasing complexity.
// Of particular focus are arrays because they're surprisingly challenging:
// * Nested arrays are always flattened
// * Node children are always completely flattened
// (items in arrays in a node child list become just part of the list)
// * Arrays that are *not* node children remain arrays
// * Sibling elements, in general are tricky because we have to
// compare them by sequential position
// * Especially deleting them - we have to delete in reverse or the DOM
// indexing will not line up (deleting #2 means #3 is now #2!)
//
start_test("Text node", 'foo');
assert(container.innerHTML == 'foo')
end_test();
start_test("Empty array should make comment", []);
assert(container.innerHTML.includes('RV:array-placeholder'));
end_test();
start_test("Null should make comment", null);
assert(container.innerHTML.includes('RV:null-placeholder'));
end_test();
// The first time we render, undefined values do *not* indicate that
// a child or bare array list has changed size. Make placeholder comment.
start_test("Undefined should make comment", undefined);
assert(container.innerHTML.includes('RV:undefined-placeholder'));
end_test();
start_test("Render a tag", ['div']);
assert($('div'));
end_test();
start_test("Render a tag with a text node", ['div', 'foo']);
assert($('div'));
assert($('div').innerHTML == 'foo');
end_test();
start_test("Render a tag with two text nodes",
['div', 'foo', 'bar']
);
assert($('div'));
assert($('div').innerHTML == 'foobar');
end_test();
start_test("Tag with css class",
['div', {'class':'x'}]
);
assert($('div.x'));
end_test();
start_test("Tag with css class shorthand",
['span.x']
);
assert($('span.x'));
end_test();
start_test("Implicit div with css class shorthand",
['.x']
);
assert($('div.x'));
end_test();
start_test("Implicit div with three css classes shorthand",
['.x.y.z']
);
assert($('div.x.y.z'));
end_test();
start_test("Implicit div, one shorthand, two regular classes",
['.x', {'class':'y z'}]
);
assert($('div.x.y.z'));
end_test();
start_test("Render a tag with a data- property and a text node",
['div', {title:'world'}, 'hello']
);
assert($('[title="world"]'));
assert($('div').innerHTML == 'hello');
end_test();
start_test("Two tags in an array",
[
['span.a'],
['span.b']
]
);
assert($('span.a'));
assert($('span.b'));
end_test();
start_test("Tag with two child tags",
['div',
['span.a'],
['span.b']
]
);
assert($('div > span.a'));
assert($('div > span.b'));
end_test();
start_test("Tag with array of tags (nested)",
['div',
[
['span.a'],
['span.b']
]
]
);
assert($('div > span.a'));
assert($('div > span.b'));
end_test();
start_test("Tag with four children in two symetrical arrays (nested)",
['div',
[
['span.a'],
['span.b']
],
[
['span.c'],
['span.d']
],
]
);
assert($('div > span.a'));
assert($('div > span.b'));
assert($('div > span.c'));
assert($('div > span.d'));
end_test();
start_test("Tag with four children in asymetric arrays (nested)",
['div',
[
['span.a'],
[
['span.b'],
['span.c'],
],
['span.d'],
]
]
);
assert($('div > span.a'));
assert($('div > span.b'));
assert($('div > span.c'));
assert($('div > span.d'));
end_test();
start_test("Tree: 5 children, two grandchildren, weird array nesting",
['div.grandpa',
[
['span.a'], // child 1
[
['span.b'], // child 2
['span.c'], // child 3
],
['span.d', // child 4
[
['span.e', 'f', 'g'],
['span.h', 'i', 'j'],
],
],
[
[
[
[
['span.x', 'y', 'z'], // child 5
],
],
],
],
]
]
);
assert($('div.grandpa > span.a'));
assert($('div.grandpa > span.b'));
assert($('div.grandpa > span.c'));
assert($('div.grandpa > span.d'));
assert($('div.grandpa > span.d > span.e'));
assert($('div.grandpa > span.d > span.e').innerHTML == 'fg');
assert($('div.grandpa > span.d > span.h').innerHTML == 'ij');
assert($('div.grandpa > span.x'));
assert($('div.grandpa > span.x').innerHTML == 'yz');
end_test();
// =======================================================================
// Tests for diffing
before = 'hello';
after = 'world';
start_test("Simple text change", before);
assert(container.innerHTML == 'hello')
add_render(after);
assert(container.innerHTML == 'world')
end_test();
before = ['div.w',['div.x'],null,['div.z']];
after = ['div.w',['div.x'],['div.y'],['div.z']];
start_test("Null child placeholder", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = ['div.w',[['div.x'],null,['div.z']]];
after = ['div.w',[['div.x'],['div.y'],['div.z']]];
start_test("Null placeholder in array", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = ['div.w',['div.x'],undefined,['div.z']];
after = ['div.w',['div.x'],['div.y'],['div.z']];
start_test("Undefined child placeholder", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = ['div.w',[['div.x'],undefined,['div.z']]];
after = ['div.w',[['div.x'],['div.y'],['div.z']]];
start_test("Undefined placeholder in array", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = ['div.w',['div.x'],[],['div.z']];
after = ['div.w',['div.x'],['div.y'],['div.z']];
start_test("Div replaces empty array", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = [];
after = [['div.x'],['div.y'],['div.z']];
start_test("Multiple array items added, then removed", before);
// not much to assert here, container starts empty
add_render(after);
assert($('div.x'));
assert($('div.y'));
assert($('div.z'));
add_render(before);
assert(!$('div.x')); // not
assert(!$('div.y')); // not
assert(!$('div.z')); // not
end_test();
before = ['div.w'];
after = ['div.w',['div.x'],['div.y'],['div.z']];
start_test("Multiple children added then removed", before);
assert($('div.w'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
add_render(before);
assert(!$('div.w > div.x')); // not
assert(!$('div.w > div.y')); // not
assert(!$('div.w > div.z')); // not
end_test();
before = ['div.w',['div.x']];
after = ['div.w',['div.x'],['div.y'],['div.z']];
start_test("Multiple children added (to existing) then removed", before);
assert($('div.w > div.x'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
add_render(before);
assert($('div.w > div.x'));
assert(!$('div.w > div.y')); // not
assert(!$('div.w > div.z')); // not
end_test();
before = ['div.w', []];
after = ['div.w', [['div.x'],['div.y'],['div.z']]];
start_test("Multiple child array items added, then removed", before);
assert($('div.w'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
add_render(before);
assert(!$('div.w > div.x')); // not
assert(!$('div.w > div.y')); // not
assert(!$('div.w > div.z')); // not
end_test();
before = ['div.w',['div.x'],['div.y'],['div.z']];
after = ['div.w',['div.x'],[],['div.z']];
start_test("Empty array replaces div and back again", before);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert(!$('div.w > div.y')); // not
assert($('div.w > div.z'));
add_render(before);
assert($('div.w > div.x'));
assert($('div.w > div.y'));
assert($('div.w > div.z'));
end_test();
before = ['div.w',['div.x'],[],['div.z']];
after = ['div.w',['div.x'],[['div.a'],['div.b']],['div.z']];
start_test("Empty array filled, then emptied again.", before);
assert($('div.w > div.x'));
assert($('div.w > div.z'));
add_render(after);
assert($('div.w > div.x'));
assert($('div.w > div.a'));
assert($('div.w > div.b'));
assert($('div.w > div.z'));
add_render(before);
assert($('div.w > div.x'));
assert(!$('div.w > div.a')); // not
assert(!$('div.w > div.b')); // not
assert($('div.w > div.z'));
end_test();
before= ['div.a', 'A'];
after = ['div.b', 'B'];
start_test("Change class and text child, and back again", before);
assert($('div.a').innerHTML == 'A');
add_render(after);
assert(!$('div.a')); // not
assert($('div.b').innerHTML == 'B');
add_render(before);
assert(!$('div.b')); // not
assert($('div.a').innerHTML == 'A');
end_test();
before= ['div.a', 'A'];
after = ['span.b', 'B'];
start_test("Change tag, class, and text child, and back again", before);
assert($('div.a').innerHTML == 'A');
add_render(after);
assert(!$('div.a')); // not
assert($('span.b').innerHTML == 'B');
add_render(before);
assert(!$('span.b')); // not
assert($('div.a').innerHTML == 'A');
end_test();
before = ['form',
['p', 'Make a character!'],
['label', 'Character Name:',
['input', {type:'text',placeholder:'Your Name Here'}]
],
['p', 'Are you a goose?'],
['label', 'Yes',
['input', {type:'radio',name:'honk',value:'yes'}]
],
['label', 'No',
['input', {type:'radio',name:'honk',value:'no'}]
],
['button', 'Create Character'],
];
start_test("Completely change, empty, change, back again.", before);
assert($('form'));
assert($('form').childNodes.length == 6);
assert($('form > p'));
assert($('form > label'));
assert($('form > label > input[type="text"'));
assert($('form > label > input[value="yes"]'));
assert($('form > label > input[value="no"]'));
assert($('form > button').innerHTML == 'Create Character');
add_render([]);
assert(!$('form')); // not
assert(container.innerHTML.includes('RV:array-placeholder'));
add_render(['.nothing', [], [], null, []]);
assert($('.nothing'));
assert($('.nothing').innerHTML.includes('RV:null-placeholder'));
add_render(['.something', ['span','hi'], [], ['span','bye'], []]);
assert($('.something'));
assert($('.something > span'));
add_render(null);
assert(container.innerHTML.includes('RV:null-placeholder'));
add_render(['form', 'Write an essay:', ['textarea']]);
assert($('form > textarea'));
add_render(before);
// exact repeat of the assertions above
assert($('form'));
assert($('form').childNodes.length == 6);
assert($('form > p'));
assert($('form > label'));
assert($('form > label > input[type="text"'));
assert($('form > label > input[value="yes"]'));
assert($('form > label > input[value="no"]'));
assert($('form > button').innerHTML == 'Create Character');
end_test();
// =======================================================================
// Tests with "false means 'no change'"
before= ['div.a', 'A'];
after = false;
start_test("No render with false, then re-render", before);
assert($('div.a').innerHTML == 'A');
add_render(after);
assert($('div.a').innerHTML == 'A');
add_render(before);
assert($('div.a').innerHTML == 'A');
end_test();
before= ['div.a', ['.cb'], ['.cc']];
after = ['div.a', false, false];
start_test("No render with false (children) twice, then re-render", before);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
add_render(after);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
add_render(after);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
add_render(before);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
end_test();
before= ['div.a', ['.cb'], ['.cc']];
after = ['div.a', false, false];
after2 = ['div.a', ['span.cb'], ['span.cc']];
start_test("No render with false (children), then different", before);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
add_render(after);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
add_render(after2);
assert($('div.a > span.cb'));
assert($('div.a > span.cc'));
add_render(before);
assert($('div.a > div.cb'));
assert($('div.a > div.cc'));
end_test();
before = [['.a'], ['.b'], ['.c']];
after = [false, ['.b'], false];
after2 = false;
start_test("No render with false (array)", before);
assert($('div.a'));
assert($('div.b'));
assert($('div.c'));
add_render(after);
assert($('div.a'));
assert($('div.b'));
assert($('div.c'));
add_render(after2);
assert($('div.a'));
assert($('div.b'));
assert($('div.c'));
add_render(before);
assert($('div.a'));
assert($('div.b'));
assert($('div.c'));
end_test();
before = ['.foo'];
start_test("False, null, arrays, elements switch-a-roo", before);
assert($('div.foo'));
add_render(false);
assert($('div.foo'));
add_render(null);
assert(container.innerHTML.includes('RV:null-placeholder'));
add_render(false);
assert(container.innerHTML.includes('RV:null-placeholder'));
add_render(before);
assert($('div.foo'));
add_render(false);
add_render([]);
add_render(null);
add_render(false);
add_render([]);
add_render(false); // just seeing if it will blow up
add_render(before);
assert($('div.foo'));
end_test();
before = false;
start_test("False placeholder", before);
assert(container.innerHTML.includes('RV:false-placeholder'));
add_render(['.foo']);
assert($('div.foo'));
end_test();
before = ['.foo', false];
after = ['.foo', ['.bar']];
start_test("False placeholder child", before);
assert($('.foo').innerHTML.includes('RV:false-placeholder'));
add_render(after);
assert($('div.foo > .bar'));
add_render(before);
assert($('div.foo > .bar'));
end_test();
before = ['.foo', ['.bar', false, ['.baz']]];
after = ['.foo', ['.bar', ['.bonk'], ['.baz']]];
after2 = ['.foo', false];
after3 = false;
start_test("False placeholder grandchild", before);
assert($('div.foo > .bar > .baz'));
assert($('.foo > .bar').innerHTML.includes('RV:false-placeholder'));
add_render(after);
assert($('div.foo > .bar'));
assert($('div.foo > .bar > .baz'));
assert($('div.foo > .bar > .bonk'));
add_render(after2);
assert($('div.foo > .bar'));
assert($('div.foo > .bar > .baz'));
assert($('div.foo > .bar > .bonk'));
add_render(after3);
assert($('div.foo > .bar'));
assert($('div.foo > .bar > .baz'));
assert($('div.foo > .bar > .bonk'));
end_test();
// =======================================================================
// Styles
before = ['div', {
style: {
color: 'red',
fontSize: '2em',
},
},
['span', { // child of div
style: {
backgroundColor: 'green',
}
}],
];
after = ['div', {
style: {
color: 'red',
fontSize: '3em',
},
},
['span', { // child of div
style: {
textDecoration: 'underline',
}
}],
];
start_test("Set and change styles", before);
assert($('div').style.color === 'red');
assert($('div').style.fontSize === '2em');
assert($('div > span').style.backgroundColor === 'green');
add_render(after);
assert($('div').style.color === 'red');
assert($('div').style.fontSize === '3em');
assert($('div > span').style.backgroundColor === 'green');
assert($('div > span').style.textDecoration === 'underline');
add_render(before);
assert($('div').style.color === 'red');
assert($('div').style.fontSize === '2em');
assert($('div > span').style.backgroundColor === 'green');
end_test();
// =======================================================================
// Misc
// Special handling of label "for" attribute (is htmlFor in JS!)
start_test("Set label 'for' and change it", ['label', {'for':'X'}]);
assert($('label').htmlFor === 'X');
add_render(['label', {htmlFor:'Y'}]); // via real js name
assert($('label').htmlFor === 'Y');
add_render(['label', {'for':'Z'}]);
assert($('label').htmlFor === 'Z');
end_test();
// Special handling of form elements since their values can change
// on their own!
start_test("Set value for input, change it, set it again",
['input', {type:'text', value:'apple'}]);
assert($('input').value === 'apple');
$('input').value = 'grape';
assert($('input').value === 'grape');
// IMPORTANT: this is the SAME value we had in prev render:
add_render(['input', {type:'text', value:'apple'}]);
assert($('input').value === 'apple');
add_render(['input', {type:'text', value:'pear'}]);
assert($('input').value === 'pear');
end_test();
start_test("Set value for textarea, change it, set it again",
['textarea', {type:'text', value:'apple'}]);
assert($('textarea').value === 'apple');
$('textarea').value = 'grape';
assert($('textarea').value === 'grape');
// IMPORTANT: this is the SAME value we had in prev render:
add_render(['textarea', {type:'text', value:'apple'}]);
assert($('textarea').value === 'apple');
add_render(['textarea', {type:'text', value:'pear'}]);
assert($('textarea').value === 'pear');
end_test();
start_test("Set checkbox checked, change it, set again",
['input', {type:'checkbox', checked:true}]);
assert($('input[type="checkbox"]').checked);
$('input[type="checkbox"]').checked = false;
assert(!$('input[type="checkbox"]').checked); // not
// IMPORTANT: same as original render
add_render(['input', {type:'checkbox', checked:true}]);
assert($('input[type="checkbox"]').checked);
add_render(['input', {type:'checkbox', checked:false}]);
assert(!$('input[type="checkbox"]').checked); // not
end_test();
var my_input_ref = null;
start_test("Use oncreate to store ref, manipulate element",
['input', {
value: 'TEST1',
oncreate:function(el){
my_input_ref = el;
},
}]
);
assert($('input').value === 'TEST1');
my_input_ref.value = 'TEST2'; // manually change it through reference
assert($('input').value === 'TEST2');
end_test();
start_test("Raw HTML", ['div']);
assert($('div'));
add_render(['div', ['<span class="hi">Hello</span>']]);
assert($('div > span.hi'));
assert($('div > span.hi').innerHTML == 'Hello');
add_render(['div', ['<span class="hi">Bye</span>']]);
// we are *expecting* this to not reflect the new text - html
// literals do not re-evaluate
assert($('div > span.hi').innerHTML == 'Hello');
add_render(['div', ['b', 'B'], ['<span class="hi">Bye</span>']]);
assert($('div > b'));
assert($('div > span.hi'));
assert($('div > span.hi').innerHTML == 'Bye');
add_render(['div']);
assert(!$('div > span.hi')); // not
end_test();
// =======================================================================
// Regression tests (testing fixed bugs)
start_test("Non-empty DOM container should be emptied on first run", ['h1', 'Hello'] );
// This one is different. The above start_test() will insert some stuff
// and RetroV will set the "old_v" (rv_old_v) on the container. We're
// going to manually remove the rv_old_v to simulate having a non-empty
// container that appears to have never been rendered into before (as one
// might encounter when rendering to document.body for the first time).
assert($('h1').innerHTML == 'Hello') // has stuff
delete container.rv_old_v; // will appear to never have been rendered into before
add_render(['h2', ['i', 'Hello again']]);
assert(!$('h1')); // should not exist anymore, render thinks its first time
assert($('h2 i').innerHTML == 'Hello again');
// test rendering a second time - if the DOM wasn't cleared correctly before,
// we will CRASH because the DOM child numbering will be off for the update!
add_render(['h2', ['i', 'Hello yet again']]);
assert($('h2 i').innerHTML == 'Hello yet again');
end_test();
start_test("Null in second position is NOT a properties object, LOL",
['h1', null, 'Did not crash']);
// This test will flat-out crash with the initial bug present. But the assert
// makes certain that we did, in fact, render correctly (after not crashing).
assert($('h1').innerHTML.includes('Did not crash'));
end_test();
start_test("Updating null with same should not attempt to update properties",
['h1', null, null]);
assert($('h1'));
add_render(['h1', null, null]);
assert($('h1')); // just confirms we didn't crash :-)
end_test();
start_test("Updating undefined with same should not attempt to update properties",
['h1', undefined, undefined]);
assert($('h1'));
add_render(['h1', undefined, undefined]);
assert($('h1')); // just confirms we didn't crash :-)
end_test();
start_test("Replacing tag with list should replace, not 'append'",
['h2.info', 'start']);
add_render(['div', 'foo']);
add_render([ ['h2', 'bar'] ]);
assert($('h2').innerHTML == 'bar'); // should exist
assert(!$('div')); // original tag must NOT exist
end_test();
// =======================================================================
// All done, print simple count summary
container.replaceChildren(
'' + test_count + ' tests and ' + assert_count + ' assertions done!'
);
} // end of test()
</script>
<h2>Unminified:</h2>
<div id="test_output"></div>
<div id="test_container"></div>
<script src="retrov.js"></script>
<script>test('test_output', 'test_container');</script>
<h2>Minified:</h2>
<div id="test_output_min"></div>
<div id="test_container_min"></div>
<script src="retrov.min.js"></script>
<script>test('test_output_min', 'test_container_min');</script>
<br><br>
</body>
</html>