forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspark_tests.js
3887 lines (3356 loc) · 112 KB
/
spark_tests.js
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
// XXX make sure that when tests use id="..." to trigger patching, "preserve" happens
// XXX test that events inside constant regions still work after patching
// XXX test arguments to landmark rendered callback
// XXX test variable wrapping (eg TR vs THEAD) inside each branch of Spark.list?
Spark._checkIECompliance = true;
// Tests can use {preserve: idNameLabels} or renderWithPreservation
// to cause any element with an id or name to be preserved. This effect
// is similar to what the preserve-inputs package does, though it applies
// to all elements, not just inputs.
var idNameLabels = {
'*[id], *[name]': Spark._labelFromIdOrName
};
var renderWithPreservation = function (htmlFunc) {
return Meteor.render(function () {
return Spark.createLandmark({ preserve: idNameLabels}, htmlFunc);
});
};
var eventmap = function (/*args*/) {
// support event_buf as final argument
var event_buf = null;
if (arguments.length && _.isArray(arguments[arguments.length-1])) {
event_buf = arguments[arguments.length-1];
arguments.length--;
}
var events = {};
_.each(arguments, function (esel) {
var etyp = esel.split(' ')[0];
events[esel] = function (evt) {
if (evt.type !== etyp)
throw new Error(etyp+" event arrived as "+evt.type);
(event_buf || this).push(esel);
};
});
return events;
};
var nodesToArray = function (array) {
// Starting in underscore 1.4, _.toArray does not work right on a node
// list in IE8. This is a workaround to support IE8.
return _.map(array, _.identity);
};
Tinytest.add("spark - assembly", function (test) {
var furtherCanon = function(str) {
// further canonicalize innerHTML in IE by adding close
// li tags to "<ul><li>one<li>two<li>three</li></ul>"
return str.replace(/<li>(\w*)(?=<li>)/g, function(s) {
return s+"</li>";
});
};
var doTest = function (calc) {
var frag = Spark.render(function () {
return calc(function (str, expected) {
return Spark.setDataContext(null, str);
});
});
var groups = [];
var html = calc(function (str, expected, noRange) {
if (arguments.length > 1)
str = expected;
if (! noRange)
groups.push(str);
return str;
});
var f = WrappedFrag(frag);
test.equal(furtherCanon(f.html()), html);
var actualGroups = [];
var tempRange = new LiveRange(Spark._TAG, frag);
tempRange.visit(function (isStart, rng) {
if (! isStart && rng.type === Spark._ANNOTATION_DATA)
actualGroups.push(furtherCanon(canonicalizeHtml(
DomUtils.rangeToHtml(rng.firstNode(), rng.lastNode()))));
});
test.equal(actualGroups.join(','), groups.join(','));
};
doTest(function (A) { return "<p>Hello</p>"; });
doTest(function (A) { return "<td>Hello</td><td>World</td>"; });
doTest(function (A) { return "<td>"+A("Hello")+"</td>"; });
doTest(function (A) { return A("<td>"+A("Hello")+"</td>"); });
doTest(function (A) { return A(A(A(A(A(A("foo")))))); });
doTest(
function (A) { return "<div>Yo"+A("<p>Hello "+A(A("World")),"<p>Hello World</p>")+
"</div>"; });
doTest(function (A) {
return A("<ul>"+A("<li>one","<li>one</li>")+
A("<li>two","<li>two</li>")+
A("<li>three","<li>three</li>"),
"<ul><li>one</li><li>two</li><li>three</li></ul>"); });
doTest(function (A) {
return A("<table>"+A("<tr>"+A("<td>"+A("Hi")+"</td>")+"</tr>")+"</table>",
"<table><tbody><tr><td>Hi</td></tr></tbody></table>");
});
test.throws(function () {
doTest(function (A) {
var z = A("Hello");
return z+z;
});
});
var frag = Spark.render(function () {
return '<div foo="abc' +
Spark.setDataContext(null, "bar") +
'xyz">Hello</div>';
});
var div = frag.firstChild;
test.equal(div.nodeName, "DIV");
var attrValue = div.getAttribute('foo');
test.isTrue(attrValue.indexOf('abc<!--') === 0, attrValue);
test.isTrue(attrValue.indexOf('-->xyz') >= 0, attrValue);
});
Tinytest.add("spark - repeat inclusion", function(test) {
test.throws(function() {
var frag = Spark.render(function() {
var x = Spark.setDataContext({}, "abc");
return x + x;
});
});
});
Tinytest.add("spark - replace tag contents", function (test) {
// adapted from nateps / metamorph
var do_onscreen = function (f) {
var div = OnscreenDiv();
var stuff = {
div: div,
node: _.bind(div.node, div),
render: function (rfunc) {
div.node().appendChild(Meteor.render(rfunc));
}
};
f.call(stuff);
div.kill();
};
var R, div;
// basic text replace
do_onscreen(function () {
R = ReactiveVar("one two three");
this.render(function () {
return R.get();
});
R.set("three four five six");
Deps.flush();
test.equal(this.div.html(), "three four five six");
});
// work inside a table
do_onscreen(function () {
R = ReactiveVar("<tr><td>HI!</td></tr>");
this.render(function () {
return "<table id='morphing'>" + R.get() + "</table>";
});
test.equal($(this.node()).find("#morphing td").text(), "HI!");
R.set("<tr><td>BUH BYE!</td></tr>");
Deps.flush();
test.equal($(this.node()).find("#morphing td").text(), "BUH BYE!");
});
// work inside a tbody
do_onscreen(function () {
R = ReactiveVar("<tr><td>HI!</td></tr>");
this.render(function () {
return "<table id='morphing'><tbody>" + R.get() + "</tbody></table>";
});
test.equal($(this.node()).find("#morphing td").text(), "HI!");
R.set("<tr><td>BUH BYE!</td></tr>");
Deps.flush();
test.equal($(this.node()).find("#morphing td").text(), "BUH BYE!");
});
// work inside a tr
do_onscreen(function () {
R = ReactiveVar("<td>HI!</td>");
this.render(function () {
return "<table id='morphing'><tr>" + R.get() + "</tr></table>";
});
test.equal($(this.node()).find("#morphing td").text(), "HI!");
R.set("<td>BUH BYE!</td>");
Deps.flush();
test.equal($(this.node()).find("#morphing td").text(), "BUH BYE!");
});
// work inside a ul
do_onscreen(function () {
R = ReactiveVar("<li>HI!</li>");
this.render(function () {
return "<ul id='morphing'>" + R.get() + "</ul>";
});
test.equal($(this.node()).find("#morphing li").text(), "HI!");
R.set("<li>BUH BYE!</li>");
Deps.flush();
test.equal($(this.node()).find("#morphing li").text(), "BUH BYE!");
});
// work inside a select
do_onscreen(function () {
R = ReactiveVar("<option>HI!</option>");
this.render(function () {
return "<select id='morphing'>" + R.get() + "</select>";
});
test.equal($(this.node()).find("#morphing option").text(), "HI!");
R.set("<option>BUH BYE!</option>");
Deps.flush();
test.equal($(this.node()).find("#morphing option").text(), "BUH BYE!");
});
// list of select options
do_onscreen(function () {
var c = new LocalCollection();
c.insert({name: 'Hamburger', value: 1});
c.insert({name: 'Cheeseburger', value: 2});
this.render(function () {
return "<select id='morphing' name='fred'>" +
Spark.list(c.find({}, {sort: ['value']}), function (doc) {
return '<option value="' + doc.value + '">' + doc.name + '</option>';
}) +
"</select>";
});
var furtherCanon = function (html) {
return html.replace(/\s*selected="selected"/g, '');
};
test.equal(furtherCanon(this.div.html()),
'<select id="morphing" name="fred">' +
'<option value="1">Hamburger</option>' +
'<option value="2">Cheeseburger</option>' +
'</select>');
c.insert({name: 'Chicken Snickers', value: 8});
Deps.flush();
test.equal(furtherCanon(this.div.html()),
'<select id="morphing" name="fred">' +
'<option value="1">Hamburger</option>' +
'<option value="2">Cheeseburger</option>' +
'<option value="8">Chicken Snickers</option>' +
'</select>');
c.remove({value: 1});
c.remove({value: 2});
Deps.flush();
test.equal(furtherCanon(this.div.html()),
'<select id="morphing" name="fred">' +
'<option value="8">Chicken Snickers</option>' +
'</select>');
c.remove({});
Deps.flush();
test.equal(furtherCanon(this.div.html()),
'<select id="morphing" name="fred">' +
'<!---->' +
'</select>');
c.insert({name: 'Hamburger', value: 1});
c.insert({name: 'Cheeseburger', value: 2});
Deps.flush();
test.equal(furtherCanon(this.div.html()),
'<select id="morphing" name="fred">' +
'<option value="1">Hamburger</option>' +
'<option value="2">Cheeseburger</option>' +
'</select>');
});
});
Tinytest.add("spark - basic isolate", function (test) {
var R = ReactiveVar('foo');
var div = OnscreenDiv(Spark.render(function () {
return '<div>' + Spark.isolate(function () {
return '<span>' + R.get() + '</span>';
}) + '</div>';
}));
test.equal(div.html(), '<div><span>foo</span></div>');
R.set('bar');
test.equal(div.html(), '<div><span>foo</span></div>');
Deps.flush();
test.equal(div.html(), '<div><span>bar</span></div>');
R.set('baz');
Deps.flush();
test.equal(div.html(), '<div><span>baz</span></div>');
div.kill();
Deps.flush();
});
Tinytest.add("spark - one render", function (test) {
var R = ReactiveVar("foo");
var frag = WrappedFrag(Meteor.render(function () {
return R.get();
})).hold();
test.equal(R.numListeners(), 1);
// frag should be "foo" initially
test.equal(frag.html(), "foo");
R.set("bar");
// haven't flushed yet, so update won't have happened
test.equal(frag.html(), "foo");
Deps.flush();
// flushed now, frag should say "bar"
test.equal(frag.html(), "bar");
frag.release(); // frag is now considered offscreen
Deps.flush();
R.set("baz");
Deps.flush();
// no update should have happened, offscreen range dep killed
test.equal(frag.html(), "bar");
// should be back to no listeners
test.equal(R.numListeners(), 0);
// empty return value should work, and show up as a comment
frag = WrappedFrag(Meteor.render(function () {
return "";
}));
test.equal(frag.html(), "<!---->");
// nodes coming and going at top level of fragment
R.set(true);
frag = WrappedFrag(Meteor.render(function () {
return R.get() ? "<div>hello</div><div>world</div>" : "";
})).hold();
test.equal(frag.html(), "<div>hello</div><div>world</div>");
R.set(false);
Deps.flush();
test.equal(frag.html(), "<!---->");
R.set(true);
Deps.flush();
test.equal(frag.html(), "<div>hello</div><div>world</div>");
test.equal(R.numListeners(), 1);
frag.release();
Deps.flush();
test.equal(R.numListeners(), 0);
// more complicated changes
R.set(1);
frag = WrappedFrag(Meteor.render(function () {
var result = [];
for(var i=0; i<R.get(); i++) {
result.push('<div id="x'+i+'" class="foo" name="bar"><p><b>'+
R.get()+'</b></p></div>');
}
return result.join('');
})).hold();
test.equal(frag.html(),
'<div class="foo" id="x0" name="bar"><p><b>1</b></p></div>');
R.set(3);
Deps.flush();
test.equal(frag.html(),
'<div class="foo" id="x0" name="bar"><p><b>3</b></p></div>'+
'<div class="foo" id="x1" name="bar"><p><b>3</b></p></div>'+
'<div class="foo" id="x2" name="bar"><p><b>3</b></p></div>');
R.set(2);
Deps.flush();
test.equal(frag.html(),
'<div class="foo" id="x0" name="bar"><p><b>2</b></p></div>'+
'<div class="foo" id="x1" name="bar"><p><b>2</b></p></div>');
frag.release();
Deps.flush();
test.equal(R.numListeners(), 0);
// caller violating preconditions
test.equal(WrappedFrag(Meteor.render("foo")).html(), "foo");
});
Tinytest.add("spark - heuristic finalize", function (test) {
var R = ReactiveVar(123);
var div = OnscreenDiv(Meteor.render(function () {
return "<p>The number is "+R.get()+".</p><hr><br><br><u>underlined</u>";
}));
test.equal(div.html(), "<p>The number is 123.</p><hr><br><br><u>underlined</u>");
test.equal(R.numListeners(), 1);
Deps.flush();
R.set(456); // won't take effect until flush()
test.equal(div.html(), "<p>The number is 123.</p><hr><br><br><u>underlined</u>");
test.equal(R.numListeners(), 0); // listener already gone
Deps.flush();
test.equal(div.html(), "<p>The number is 456.</p><hr><br><br><u>underlined</u>");
test.equal(R.numListeners(), 1);
div.remove();
R.set(789); // update should force div dependency to be GCed when div is updated
Deps.flush();
test.equal(R.numListeners(), 0);
});
Tinytest.add("spark - isolate", function (test) {
var inc = function (v) {
v.set(v.get() + 1); };
var R1 = ReactiveVar(0);
var R2 = ReactiveVar(0);
var R3 = ReactiveVar(0);
var count1 = 0, count2 = 0, count3 = 0;
var frag = WrappedFrag(Meteor.render(function () {
return R1.get() + "," + (count1++) + " " +
Spark.isolate(function () {
return R2.get() + "," + (count2++) + " " +
Spark.isolate(function () {
return R3.get() + "," + (count3++);
});
});
})).hold();
test.equal(frag.html(), "0,0 0,0 0,0");
inc(R1); Deps.flush();
test.equal(frag.html(), "1,1 0,1 0,1");
inc(R2); Deps.flush();
test.equal(frag.html(), "1,1 1,2 0,2");
inc(R3); Deps.flush();
test.equal(frag.html(), "1,1 1,2 1,3");
inc(R2); Deps.flush();
test.equal(frag.html(), "1,1 2,3 1,4");
inc(R1); Deps.flush();
test.equal(frag.html(), "2,2 2,4 1,5");
frag.release();
Deps.flush();
test.equal(R1.numListeners(), 0);
test.equal(R2.numListeners(), 0);
test.equal(R3.numListeners(), 0);
R1.set(0);
R2.set(0);
R3.set(0);
frag = WrappedFrag(Meteor.render(function () {
var buf = [];
buf.push('<div class="foo', R1.get(), '">');
buf.push(Spark.isolate(function () {
var buf = [];
for(var i=0; i<R2.get(); i++) {
buf.push(Spark.isolate(function () {
return '<div>'+R3.get()+'</div>';
}));
}
return buf.join('');
}));
buf.push('</div>');
return buf.join('');
})).hold();
test.equal(frag.html(), '<div class="foo0"><!----></div>');
R2.set(3); Deps.flush();
test.equal(frag.html(), '<div class="foo0">'+
'<div>0</div><div>0</div><div>0</div>'+
'</div>');
R3.set(5); Deps.flush();
test.equal(frag.html(), '<div class="foo0">'+
'<div>5</div><div>5</div><div>5</div>'+
'</div>');
R1.set(7); Deps.flush();
test.equal(frag.html(), '<div class="foo7">'+
'<div>5</div><div>5</div><div>5</div>'+
'</div>');
R2.set(1); Deps.flush();
test.equal(frag.html(), '<div class="foo7">'+
'<div>5</div>'+
'</div>');
R1.set(11); Deps.flush();
test.equal(frag.html(), '<div class="foo11">'+
'<div>5</div>'+
'</div>');
R2.set(2); Deps.flush();
test.equal(frag.html(), '<div class="foo11">'+
'<div>5</div><div>5</div>'+
'</div>');
R3.set(4); Deps.flush();
test.equal(frag.html(), '<div class="foo11">'+
'<div>4</div><div>4</div>'+
'</div>');
frag.release();
// calling isolate() outside of render mode
test.equal(Spark.isolate(function () { return "foo"; }), "foo");
// caller violating preconditions
test.throws(function () {
Meteor.render(function () {
return Spark.isolate("foo");
});
});
// unused isolate
var Q = ReactiveVar("foo");
Meteor.render(function () {
// create an isolate, in render mode,
// but don't use it.
Spark.isolate(function () {
return Q.get();
});
return "";
});
Q.set("bar");
// might get an error on flush() if implementation
// deals poorly with unused isolates, or a listener
// still existing after flush.
Deps.flush();
test.equal(Q.numListeners(), 0);
// nesting
var stuff = ReactiveVar(true);
var div = OnscreenDiv(Meteor.render(function () {
return Spark.isolate(function () {
return "x"+(stuff.get() ? 'y' : '') + Spark.isolate(function () {
return "hi";
});
});
}));
test.equal(div.html(), "xyhi");
stuff.set(false);
Deps.flush();
test.equal(div.html(), "xhi");
div.kill();
Deps.flush();
// more nesting
var num1 = ReactiveVar(false);
var num2 = ReactiveVar(false);
var num3 = ReactiveVar(false);
var numset = function (n) {
_.each([num1, num2, num3], function (v, i) {
v.set((i+1) === n);
});
};
numset(1);
var div = OnscreenDiv(Meteor.render(function () {
return Spark.isolate(function () {
return (num1.get() ? '1' : '')+
Spark.isolate(function () {
return (num2.get() ? '2' : '')+
Spark.isolate(function () {
return (num3.get() ? '3' : '')+'x';
});
});
});
}));
test.equal(div.html(), "1x");
numset(2);
Deps.flush();
test.equal(div.html(), "2x");
numset(3);
Deps.flush();
test.equal(div.html(), "3x");
numset(1);
Deps.flush();
test.equal(div.html(), "1x");
numset(3);
Deps.flush();
test.equal(div.html(), "3x");
numset(2);
Deps.flush();
test.equal(div.html(), "2x");
div.remove();
Deps.flush();
// the real test for slow-path GC finalization:
num2.set(! num2.get());
Deps.flush();
test.equal(num1.numListeners(), 0);
test.equal(num2.numListeners(), 0);
test.equal(num3.numListeners(), 0);
});
Tinytest.add("spark - data context", function (test) {
var d1 = {x: 1};
var d2 = {x: 2};
var d3 = {x: 3};
var d4 = {x: 4};
var d5 = {x: 5};
var traverse = function (frag) {
var out = '';
var walkChildren = function (parent) {
for (var node = parent.firstChild; node; node = node.nextSibling) {
if (node.nodeType !== 8 /* COMMENT */) {
var data = Spark.getDataContext(node);
out += (data === null) ? "_" : data.x;
}
if (node.nodeType === 1 /* ELEMENT */)
walkChildren(node);
}
};
walkChildren(frag);
return out;
};
var testData = function (serialized, htmlFunc) {
test.equal(traverse(Spark.render(htmlFunc)), serialized);
};
testData("_", function () {
return "hi";
});
testData("__", function () {
return "<div>hi</div>";
});
testData("_1", function () {
return "<div>" + Spark.setDataContext(d1, "hi") + "</div>";
});
testData("21", function () {
return Spark.setDataContext(
d2, "<div>" + Spark.setDataContext(d1, "hi") + "</div>");
});
testData("21", function () {
return Spark.setDataContext(
d2, "<div>" +
Spark.setDataContext(d3,
Spark.setDataContext(d1, "hi")) +
"</div>");
});
testData("23", function () {
return Spark.setDataContext(
d2, "<div>" +
Spark.setDataContext(d1,
Spark.setDataContext(d3, "hi")) +
"</div>");
});
testData("23", function () {
var html = Spark.setDataContext(
d2, "<div>" +
Spark.setDataContext(d1,
Spark.setDataContext(d3, "hi")) +
"</div>");
return Spark.setDataContext(d4, html);
});
testData("1_2", function () {
return Spark.setDataContext(d1, "hi") + "-" +
Spark.setDataContext(d2, "there");
});
testData("_122_3__45", function () {
return "<div>" +
Spark.setDataContext(d1, "<div></div>") +
Spark.setDataContext(d2, "<div><div></div></div>") +
"<div></div>" +
Spark.setDataContext(d3, "<div></div>") +
"<div><div></div></div>" +
Spark.setDataContext(d4, "<div>" +
Spark.setDataContext(d5, "<div></div>") +
"</div>");
});
});
Tinytest.add("spark - tables", function (test) {
var R = ReactiveVar(0);
var table = OnscreenDiv(Meteor.render(function () {
var buf = [];
buf.push("<table>");
for(var i=0; i<R.get(); i++)
buf.push("<tr><td>"+(i+1)+"</td></tr>");
buf.push("</table>");
return buf.join('');
}));
R.set(1);
Deps.flush();
test.equal(table.html(), "<table><tbody><tr><td>1</td></tr></tbody></table>");
R.set(10);
test.equal(table.html(), "<table><tbody><tr><td>1</td></tr></tbody></table>");
Deps.flush();
test.equal(table.html(), "<table><tbody>"+
"<tr><td>1</td></tr>"+
"<tr><td>2</td></tr>"+
"<tr><td>3</td></tr>"+
"<tr><td>4</td></tr>"+
"<tr><td>5</td></tr>"+
"<tr><td>6</td></tr>"+
"<tr><td>7</td></tr>"+
"<tr><td>8</td></tr>"+
"<tr><td>9</td></tr>"+
"<tr><td>10</td></tr>"+
"</tbody></table>");
R.set(0);
Deps.flush();
test.equal(table.html(), "<table></table>");
table.kill();
Deps.flush();
test.equal(R.numListeners(), 0);
var div = OnscreenDiv();
div.node().appendChild(document.createElement("TABLE"));
div.node().firstChild.appendChild(Meteor.render(function () {
var buf = [];
for(var i=0; i<R.get(); i++)
buf.push("<tr><td>"+(i+1)+"</td></tr>");
return buf.join('');
}));
test.equal(div.html(), "<table><!----></table>");
R.set(3);
Deps.flush();
test.equal(div.html(), "<table><tbody>"+
"<tr><td>1</td></tr>"+
"<tr><td>2</td></tr>"+
"<tr><td>3</td></tr>"+
"</tbody></table>");
test.equal(div.node().firstChild.rows.length, 3);
R.set(0);
Deps.flush();
test.equal(div.html(), "<table><!----></table>");
div.kill();
Deps.flush();
test.equal(R.numListeners(), 0);
div = OnscreenDiv();
div.node().appendChild(DomUtils.htmlToFragment("<table><tr></tr></table>"));
R.set(3);
div.node().getElementsByTagName("tr")[0].appendChild(Meteor.render(
function () {
var buf = [];
for(var i=0; i<R.get(); i++)
buf.push("<td>"+(i+1)+"</td>");
return buf.join('');
}));
test.equal(div.html(),
"<table><tbody><tr><td>1</td><td>2</td><td>3</td>"+
"</tr></tbody></table>");
R.set(1);
Deps.flush();
test.equal(div.html(),
"<table><tbody><tr><td>1</td></tr></tbody></table>");
div.kill();
Deps.flush();
test.equal(R.numListeners(), 0);
div = OnscreenDiv(renderWithPreservation(function() {
return '<table id="my-awesome-table">'+R.get()+'</table>';
}));
Deps.flush();
R.set("<tr><td>Hello</td></tr>");
Deps.flush();
test.equal(
div.html(),
'<table id="my-awesome-table"><tbody><tr><td>Hello</td></tr></tbody></table>');
div.kill();
Deps.flush();
test.equal(R.numListeners(), 0);
});
Tinytest.add("spark - event handling", function (test) {
var event_buf = [];
var getid = function (id) {
return document.getElementById(id);
};
var div;
var chunk = function (htmlFunc, options) {
var html = Spark.isolate(htmlFunc);
options = options || {};
if (options.events)
html = Spark.attachEvents(options.events, html);
if (options.event_data)
html = Spark.setDataContext(options.event_data, html);
return html;
};
var render = function (htmlFunc, options) {
return Spark.render(function () {
return chunk(htmlFunc, options);
});
};
// clicking on a div at top level
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return '<div id="foozy">Foo</div>';
}, {events: eventmap("click"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click']);
div.kill();
Deps.flush();
// selector that specifies a top-level div
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return '<div id="foozy">Foo</div>';
}, {events: eventmap("click div"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click div']);
div.kill();
Deps.flush();
// selector that specifies a second-level span
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return '<div id="foozy"><span>Foo</span></div>';
}, {events: eventmap("click span"), event_data:event_buf}));
clickElement(getid("foozy").firstChild);
test.equal(event_buf, ['click span']);
div.kill();
Deps.flush();
// replaced top-level elements still have event handlers
// even if replaced by an isolate above the handlers in the DOM
var R = ReactiveVar("p");
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return chunk(function () {
return '<'+R.get()+' id="foozy">Hello</'+R.get()+'>';
});
}, {events: eventmap("click"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click']);
event_buf.length = 0;
R.set("div"); // change tag, which is sure to replace element
Deps.flush();
clickElement(getid("foozy")); // still clickable?
test.equal(event_buf, ['click']);
event_buf.length = 0;
R.set("p");
Deps.flush();
clickElement(getid("foozy"));
test.equal(event_buf, ['click']);
event_buf.length = 0;
div.kill();
Deps.flush();
// bubbling from event on descendent of element matched
// by selector
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return '<div id="foozy"><span><u><b>Foo</b></u></span>'+
'<span>Bar</span></div>';
}, {events: eventmap("click span"), event_data:event_buf}));
clickElement(
getid("foozy").firstChild.firstChild.firstChild);
test.equal(event_buf, ['click span']);
div.kill();
Deps.flush();
// bubbling order (for same event, same render node, different selector nodes)
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return '<div id="foozy"><span><u><b>Foo</b></u></span>'+
'<span>Bar</span></div>';
}, {events: eventmap("click span", "click b"), event_data:event_buf}));
clickElement(
getid("foozy").firstChild.firstChild.firstChild);
test.equal(event_buf, ['click b', 'click span']);
div.kill();
Deps.flush();
// "bubbling" order for handlers at same level
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return chunk(function () {
return chunk(function () {
return '<span id="foozy" class="a b c">Hello</span>';
}, {events: eventmap("click .c"), event_data:event_buf});
}, {events: eventmap("click .b"), event_data:event_buf});
}, {events: eventmap("click .a"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click .c', 'click .b', 'click .a']);
event_buf.length = 0;
div.kill();
Deps.flush();
// stopPropagation doesn't prevent other event maps from
// handling same node
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return chunk(function () {
return chunk(function () {
return '<span id="foozy" class="a b c">Hello</span>';
}, {events: eventmap("click .c"), event_data:event_buf});
}, {events: {"click .b": function (evt) {
event_buf.push("click .b"); evt.stopPropagation();}}});
}, {events: eventmap("click .a"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click .c', 'click .b', 'click .a']);
event_buf.length = 0;
div.kill();
Deps.flush();
// stopImmediatePropagation DOES
event_buf.length = 0;
div = OnscreenDiv(render(function () {
return chunk(function () {
return chunk(function () {
return '<span id="foozy" class="a b c">Hello</span>';
}, {events: eventmap("click .c"), event_data:event_buf});
}, {events: {"click .b": function (evt) {
event_buf.push("click .b");
evt.stopImmediatePropagation();}}});
}, {events: eventmap("click .a"), event_data:event_buf}));
clickElement(getid("foozy"));
test.equal(event_buf, ['click .c', 'click .b']);
event_buf.length = 0;
div.kill();
Deps.flush();
// bubbling continues even with DOM change
event_buf.length = 0;
R = ReactiveVar(true);
div = OnscreenDiv(render(function () {
return chunk(function () {
return '<div id="blarn">'+(R.get()?'<span id="foozy">abcd</span>':'')+'</div>';
}, {events: { 'click span': function () {
event_buf.push('click span');
R.set(false);
Deps.flush(); // kill the span
}, 'click div': function (evt) {
event_buf.push('click div');
}}});
}));
// click on span
clickElement(getid("foozy"));
test.expect_fail(); // doesn't seem to work in old IE
test.equal(event_buf, ['click span', 'click div']);
event_buf.length = 0;
div.kill();
Deps.flush();
// "deep reach" from high node down to replaced low node.
// Tests that events are registered correctly to work in
// old IE. Also tests change event bubbling
// and proper interpretation of event maps.
event_buf.length = 0;
R = ReactiveVar('foo');
div = OnscreenDiv(render(function () {
return '<div><p><span><b>'+
chunk(function () {
return '<input type="checkbox">'+R.get();
}, {events: eventmap('click input'), event_data:event_buf}) +
'</b></span></p></div>';
}, { events: eventmap('change b', 'change input'), event_data:event_buf }));
R.set('bar');
Deps.flush();
// click on input