forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent_test.js
1935 lines (1536 loc) · 49.2 KB
/
component_test.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
steal("can/util/vdom/document", "can/util/vdom/build_fragment","can", "can/map/define", "can/component", "can/view/stache" ,"can/route", "steal-qunit", "can/component/component_bindings_test.js",function () {
var simpleDocument = can.simpleDocument;
var innerHTML = function(node){
if("innerHTML" in node) {
return node.innerHTML;
}
};
function makeTest(name, doc) {
var oldDoc;
QUnit.module(name, {
setup: function () {
oldDoc = can.document;
can.document = doc;
if(doc) {
this.fixture = doc.createElement("div");
doc.body.appendChild(this.fixture);
this.$fixture = can.$(this.fixture);
} else {
this.fixture = can.$("#qunit-fixture")[0];
this.$fixture = can.$("#qunit-fixture");
}
},
teardown: function(){
can.document = oldDoc;
if(doc) {
doc.body.removeChild(this.fixture);
}
}
});
var Paginate = can.Map.extend({
count: Infinity,
offset: 0,
limit: 100,
// Prevent negative counts
setCount: function (newCount, success, error) {
return newCount < 0 ? 0 : newCount;
},
// Prevent negative offsets
setOffset: function (newOffset) {
return newOffset < 0 ?
0 :
Math.min(newOffset, !isNaN(this.count - 1) ?
this.count - 1 :
Infinity);
},
// move next
next: function () {
this.attr('offset', this.offset + this.limit);
},
prev: function () {
this.attr('offset', this.offset - this.limit);
},
canNext: function () {
return this.attr('offset') < this.attr('count') -
this.attr('limit');
},
canPrev: function () {
return this.attr('offset') > 0;
},
page: function (newVal) {
if (newVal === undefined) {
return Math.floor(this.attr('offset') / this.attr('limit')) + 1;
} else {
this.attr('offset', (parseInt(newVal) - 1) * this.attr('limit'));
}
},
pageCount: function () {
return this.attr('count') ?
Math.ceil(this.attr('count') / this.attr('limit')) : null;
}
});
test("basic tabs", function () {
// new Tabs() ..
can.Component.extend({
tag: "tabs",
template: can.stache("<ul>" +
"{{#panels}}" +
"<li {{#isActive}}class='active'{{/isActive}} can-click='makeActive'>{{title}}</li>" +
"{{/panels}}" +
"</ul>" +
"<content></content>"),
viewModel: {
panels: [],
addPanel: function (panel) {
if (this.attr("panels")
.length === 0) {
this.makeActive(panel);
}
this.attr("panels")
.push(panel);
},
removePanel: function (panel) {
var panels = this.attr("panels");
can.batch.start();
panels.splice(panels.indexOf(panel), 1);
if (panel === this.attr("active")) {
if (panels.length) {
this.makeActive(panels[0]);
} else {
this.removeAttr("active");
}
}
can.batch.stop();
},
makeActive: function (panel) {
this.attr("active", panel);
this.attr("panels")
.each(function (panel) {
panel.attr("active", false);
});
panel.attr("active", true);
},
// this is viewModel, not mustache
// consider removing viewModel as arg
isActive: function (panel) {
return this.attr('active') === panel;
}
}
});
can.Component.extend({
// make sure <content/> works
template: can.stache("{{#if active}}<content></content>{{/if}}"),
tag: "panel",
viewModel: {
active: false,
title: "@"
},
events: {
" inserted": function () {
can.viewModel(this.element[0].parentNode)
.addPanel(this.viewModel);
},
" removed": function () {
if (!can.viewModel(this.element[0].parentNode)) {
console.log("bruke");
}
can.viewModel(this.element[0].parentNode)
.removePanel(this.viewModel);
}
}
});
var template = can.stache("<tabs>{{#each foodTypes}}<panel title='{{title}}'>{{content}}</panel>{{/each}}</tabs>");
var foodTypes = new can.List([{
title: "Fruits",
content: "oranges, apples"
}, {
title: "Breads",
content: "pasta, cereal"
}, {
title: "Sweets",
content: "ice cream, candy"
}]);
var frag = template({
foodTypes: foodTypes
});
can.append(this.$fixture, frag);
var testArea = this.fixture,
lis = testArea.getElementsByTagName("li");
equal(lis.length, 3, "three lis added");
foodTypes.each(function (type, i) {
equal(innerHTML(lis[i]), type.attr("title"), "li " + i + " has the right content");
});
foodTypes.push({
title: "Vegies",
content: "carrots, kale"
});
lis = testArea.getElementsByTagName("li");
//lis = testArea.getElementsByTagName("li");
equal(lis.length, 4, "li added");
foodTypes.each(function (type, i) {
equal( innerHTML(lis[i]), type.attr("title"), "li " + i + " has the right content");
});
equal(testArea.getElementsByTagName("panel")
.length, 4, "panel added");
foodTypes.shift();
lis = testArea.getElementsByTagName("li");
equal(lis.length, 3, "removed li after shifting a foodType");
foodTypes.each(function (type, i) {
equal( innerHTML(lis[i]), type.attr("title"), "li " + i + " has the right content");
});
// test changing the active element
var panels = testArea.getElementsByTagName("panel");
equal(lis[0].className, "active", "the first element is active");
equal(innerHTML( panels[0] ), "pasta, cereal", "the first content is shown");
equal(innerHTML( panels[1] ), "", "the second content is removed");
can.trigger(lis[1], "click");
lis = testArea.getElementsByTagName("li");
equal(lis[1].className, "active", "the second element is active");
equal(lis[0].className, "", "the first element is not active");
equal( innerHTML( panels[0]), "", "the second content is removed");
equal( innerHTML( panels[1]), "ice cream, candy", "the second content is shown");
});
test("lexical scoping", function() {
can.Component.extend({
tag: "hello-world",
leakScope: false,
template: can.stache("{{greeting}} <content>World</content>{{exclamation}}"),
viewModel: {
greeting: "Hello"
}
});
var template = can.stache("<hello-world>{{greeting}}</hello-world>");
var frag = template({
greeting: "World",
exclamation: "!"
});
var hello = frag.firstChild;
equal(can.trim( innerHTML(hello) ), "Hello World");
can.Component.extend({
tag: "hello-world-no-template",
leakScope: false,
viewModel: {greeting: "Hello"}
});
template = can.stache("<hello-world-no-template>{{greeting}}</hello-world-no-template>");
frag = template({
greeting: "World",
exclamation: "!"
});
hello = frag.firstChild;
equal(can.trim( innerHTML(hello) ), "Hello",
"If no template is provided to can.Component, treat <content> bindings as dynamic.");
});
test("dynamic scoping", function() {
can.Component.extend({
tag: "hello-world",
leakScope: true,
template: can.stache("{{greeting}} <content>World</content>{{exclamation}}"),
viewModel: {greeting: "Hello"}
});
var template = can.stache("<hello-world>{{greeting}}</hello-world>");
var frag = template({
greeting: "World",
exclamation: "!"
});
var hello = frag.firstChild;
equal( can.trim( innerHTML(hello) ) , "Hello Hello!");
});
test("treecombo", function () {
can.Component.extend({
tag: "treecombo",
template: can.stache("<ul class='breadcrumb'>" +
"<li can-click='emptyBreadcrumb'>{{title}}</li>" +
"{{#each breadcrumb}}" +
"<li can-click='updateBreadcrumb'>{{title}}</li>" +
"{{/each}}" +
"</ul>" +
"<ul class='options'>" +
"<content>" +
"{{#selectableItems}}" +
"<li {{#isSelected}}class='active'{{/isSelected}} can-click='toggle'>" +
"<input type='checkbox' {{#isSelected}}checked{{/isSelected}}/>" +
"{{title}}" +
"{{#if children.length}}" +
"<button class='showChildren' can-click='showChildren'>+</button>" +
"{{/if}}" +
"</li>" +
"{{/selectableItems}}" +
"</content>" +
"</ul>"),
viewModel: {
items: [],
breadcrumb: [],
selected: [],
selectableItems: function () {
var breadcrumb = this.attr("breadcrumb");
// if there's an item in the breadcrumb
if (breadcrumb.attr('length')) {
// return the last item's children
return breadcrumb.attr("" + (breadcrumb.length - 1) + '.children');
} else {
// return the top list of items
return this.attr('items');
}
},
showChildren: function (item, el, ev) {
ev.stopPropagation();
this.attr('breadcrumb')
.push(item);
},
emptyBreadcrumb: function () {
this.attr("breadcrumb")
.attr([], true);
},
updateBreadcrumb: function (item) {
var breadcrumb = this.attr("breadcrumb"),
index = breadcrumb.indexOf(item);
breadcrumb.splice(index + 1, breadcrumb.length - index - 1);
},
toggle: function (item) {
var selected = this.attr('selected'),
index = selected.indexOf(item);
if (index === -1) {
selected.push(item);
} else {
selected.splice(index, 1);
}
}
},
helpers: {
isSelected: function (options) {
if (this.attr("selected")
.indexOf(options.context) > -1) {
return options.fn();
} else {
return options.inverse();
}
}
}
});
var template = can.stache("<treecombo items='{locations}' title='Locations'></treecombo>");
var base = new can.Map({});
var frag = template(base);
var root = doc.createElement("div");
root.appendChild(frag);
var items = [{
id: 1,
title: "Midwest",
children: [{
id: 5,
title: "Illinois",
children: [{
id: 23423,
title: "Chicago"
}, {
id: 4563,
title: "Springfield"
}, {
id: 4564,
title: "Naperville"
}]
}, {
id: 6,
title: "Wisconsin",
children: [{
id: 232423,
title: "Milwaulkee"
}, {
id: 45463,
title: "Green Bay"
}, {
id: 45464,
title: "Madison"
}]
}]
}, {
id: 2,
title: "East Coast",
children: [{
id: 25,
title: "New York",
children: [{
id: 3413,
title: "New York"
}, {
id: 4613,
title: "Rochester"
}, {
id: 4516,
title: "Syracuse"
}]
}, {
id: 6,
title: "Pennsylvania",
children: [{
id: 2362423,
title: "Philadelphia"
}, {
id: 454663,
title: "Harrisburg"
}, {
id: 454664,
title: "Scranton"
}]
}]
}];
stop();
setTimeout(function () {
base.attr('locations', items);
var itemsList = base.attr('locations');
// check that the DOM is right
var treecombo = root.firstChild,
breadcrumb = treecombo.firstChild,
breadcrumbLIs = function(){
return breadcrumb.getElementsByTagName('li');
},
options = treecombo.lastChild,
optionsLis = function(){
return options.getElementsByTagName('li');
};
equal(breadcrumbLIs().length, 1, "Only the default title is shown");
equal( innerHTML( breadcrumbLIs()[0] ) , "Locations", "The correct title from the attribute is shown");
equal( itemsList.length, optionsLis().length, "first level items are displayed");
// Test toggling selected, first by clicking
can.trigger(optionsLis()[0], "click");
equal(optionsLis()[0].className, "active", "toggling something not selected adds active");
ok(optionsLis()[0].getElementsByTagName('input')[0].checked, "toggling something not selected checks checkbox");
equal(can.viewModel(treecombo, "selected")
.length, 1, "there is one selected item");
equal(can.viewModel(treecombo, "selected.0"), itemsList.attr("0"), "the midwest is in selected");
// adjust the state and everything should update
can.viewModel(treecombo, "selected")
.pop();
equal(optionsLis()[0].className, "", "toggling something not selected adds active");
// Test going in a location
can.trigger(optionsLis()[0].getElementsByTagName('button')[0], "click");
equal(breadcrumbLIs().length, 2, "Only the default title is shown");
equal(innerHTML(breadcrumbLIs()[1]), "Midwest", "The breadcrumb has an item in it");
ok(/Illinois/.test( innerHTML(optionsLis()[0])), "A child of the top breadcrumb is displayed");
// Test going in a location without children
can.trigger(optionsLis()[0].getElementsByTagName('button')[0], "click");
ok(/Chicago/.test( innerHTML(optionsLis()[0] ) ), "A child of the top breadcrumb is displayed");
ok(!optionsLis()[0].getElementsByTagName('button')
.length, "no show children button");
// Test poping off breadcrumb
can.trigger(breadcrumbLIs()[1], "click");
equal(innerHTML(breadcrumbLIs()[1]), "Midwest", "The breadcrumb has an item in it");
ok(/Illinois/.test( innerHTML( optionsLis()[0])), "A child of the top breadcrumb is displayed");
// Test removing everything
can.trigger(breadcrumbLIs()[0], "click");
equal(breadcrumbLIs().length, 1, "Only the default title is shown");
equal( innerHTML(breadcrumbLIs()[0]), "Locations", "The correct title from the attribute is shown");
start();
}, 100);
});
test("deferred grid", function () {
// This test simulates a grid that reads a `deferreddata` property for
// items and displays them.
// If `deferreddata` is a deferred, it waits for those items to resolve.
// The grid also has a `waiting` property that is true while the deferred is being resolved.
can.Component.extend({
tag: "grid",
viewModel: {
items: [],
waiting: true
},
template: can.stache("<table><tbody><content></content></tbody></table>"),
events: {
init: function () {
this.update();
},
"{viewModel} deferreddata": "update",
update: function () {
var deferred = this.viewModel.attr('deferreddata'),
viewModel = this.viewModel;
if (can.isPromise(deferred)) {
this.viewModel.attr("waiting", true);
deferred.then(function (items) {
viewModel.attr('items')
.attr(items, true);
});
} else {
viewModel.attr('items')
.attr(deferred, true);
}
},
"{items} change": function () {
this.viewModel.attr("waiting", false);
}
}
});
// The context object has a `set` property and a
// deferredData property that reads from it and returns a new deferred.
var SimulatedScope = can.Map.extend({
set: 0,
deferredData: function () {
var deferred = new can.Deferred();
var set = this.attr('set');
if (set === 0) {
setTimeout(function () {
deferred.resolve([{
first: "Justin",
last: "Meyer"
}]);
}, 100);
} else if (set === 1) {
setTimeout(function () {
deferred.resolve([{
first: "Brian",
last: "Moschel"
}]);
}, 100);
}
return deferred;
}
});
var viewModel = new SimulatedScope();
var template = can.stache("<grid deferreddata='{viewModel.deferredData}'>" +
"{{#each items}}" +
"<tr>" +
"<td width='40%'>{{first}}</td>" +
"<td width='70%'>{{last}}</td>" +
"</tr>" +
"{{/each}}" +
"</grid>");
can.append(this.$fixture, template({
viewModel: viewModel
}));
var gridScope = can.viewModel(this.fixture.firstChild);
equal(gridScope.attr("waiting"), true, "The grid is initially waiting on the deferreddata to resolve");
stop();
var self = this;
var waitingHandler = function() {
gridScope.unbind('waiting', waitingHandler);
setTimeout(function () {
var tds = self.fixture.getElementsByTagName("td");
equal(tds.length, 2, "there are 2 tds");
gridScope.bind("waiting", function (ev, newVal) {
if (newVal === false) {
setTimeout(function () {
equal(innerHTML(tds[0]), "Brian", "td changed to brian");
start();
}, 10);
}
});
// update set to change the deferred.
viewModel.attr("set", 1);
}, 10);
};
gridScope.bind('waiting', waitingHandler);
});
test("nextprev", function () {
can.Component.extend({
tag: "next-prev",
template: can.stache(
'<a href="javascript://"' +
'class="prev {{#paginate.canPrev}}enabled{{/paginate.canPrev}}" can-click="{paginate.prev}">Prev</a>' +
'<a href="javascript://"' +
'class="next {{#paginate.canNext}}enabled{{/paginate.canNext}}" can-click="{paginate.next}">Next</a>')
});
var paginator = new Paginate({
limit: 20,
offset: 0,
count: 100
});
var template = can.stache("<next-prev paginate='{paginator}'></next-prev>");
var frag = template({
paginator: paginator
});
var nextPrev = frag.firstChild;
var prev = nextPrev.firstChild,
next = nextPrev.lastChild;
ok(!/enabled/.test( prev.className ), "prev is not enabled");
ok(/enabled/.test( next.className ), "next is enabled");
can.trigger(next, "click");
ok(/enabled/.test( prev.className ), "prev is enabled");
});
test("page-count", function () {
can.Component.extend({
tag: "page-count",
template: can.stache('Page <span>{{page}}</span>.')
});
var paginator = new Paginate({
limit: 20,
offset: 0,
count: 100
});
var template = can.stache("<page-count page='{paginator.page}'></page-count>");
var frag = template( new can.Map({
paginator: paginator
}) );
var span = frag.firstChild.getElementsByTagName("span")[0];
equal(span.firstChild.nodeValue, "1");
paginator.next();
equal(span.firstChild.nodeValue, "2");
paginator.next();
equal(span.firstChild.nodeValue, "3");
});
test("hello-world and whitespace around custom elements", function () {
can.Component.extend({
tag: "hello-world",
template: can.stache("{{#if visible}}{{message}}{{else}}Click me{{/if}}"),
viewModel: {
visible: false,
message: "Hello There!"
},
events: {
click: function () {
this.viewModel.attr("visible", true);
}
}
});
var template = can.stache(" <hello-world></hello-world> ");
var frag = template({});
var helloWorld = frag.childNodes.item(1);
can.trigger(can.$(helloWorld), "click");
equal( innerHTML(helloWorld) , "Hello There!");
});
test("self closing content tags", function () {
can.Component.extend({
"tag": "my-greeting",
template: can.stache("<h1><content/></h1>"),
viewModel: {
title: "can.Component"
}
});
var template = can.stache("<my-greeting><span>{{site}} - {{title}}</span></my-greeting>");
var frag = template({
site: "CanJS"
});
equal(frag.firstChild.getElementsByTagName("span")
.length, 1, "there is an h1");
});
test("can.viewModel utility", function() {
can.Component({
tag: "my-taggy-tag",
template: can.stache("<h1>hello</h1>"),
viewModel: {
foo: "bar"
}
});
var frag = can.stache("<my-taggy-tag id='x'></my-taggy-tag>")();
var el = can.$(frag.firstChild);
equal(can.viewModel(el), can.data(el, "viewModel"), "one argument grabs the viewModel object");
equal(can.viewModel(el, "foo"), "bar", "two arguments fetches a value");
can.viewModel(el, "foo", "baz");
equal(can.viewModel(el, "foo"), "baz", "Three arguments sets the value");
if (window.$ && $.fn) {
el = $(frag.firstChild);
equal(el.viewModel(), can.data(el, "viewModel"), "jQuery helper grabs the viewModel object");
equal(el.viewModel("foo"), "baz", "jQuery helper with one argument fetches a property");
equal(el.viewModel("foo", "bar").get(0), el.get(0), "jQuery helper returns the element");
equal(el.viewModel("foo"), "bar", "jQuery helper with two arguments sets the property");
}
});
test("can.viewModel backwards compatible with can.scope", function() {
equal(can.viewModel, can.scope, "can helper");
if (window.$ && $.fn) {
equal($.scope, $.viewModel, "jQuery helper");
}
});
test("can.viewModel creates one if it doesn't exist", function(){
var frag = can.stache("<div id='me'></div>")();
var el = can.$(frag.firstChild);
var viewModel = can.viewModel(el);
ok(!!viewModel, "viewModel created where it didn't exist.");
equal(viewModel, can.data(el, "viewModel"), "viewModel is in the data.");
});
test('setting passed variables - two way binding', function () {
can.Component.extend({
tag: "my-toggler",
template: can.stache("{{#if visible}}<content/>{{/if}}"),
viewModel: {
visible: true,
show: function () {
this.attr('visible', true);
},
hide: function () {
this.attr("visible", false);
}
}
});
can.Component.extend({
tag: "my-app",
viewModel: {
visible: true,
show: function () {
this.attr('visible', true);
}
}
});
var template = can.stache("<my-app>" +
'{{^visible}}<button can-click="show">show</button>{{/visible}}' +
'<my-toggler visible="{visible}">' +
'content' +
'<button can-click="hide">hide</button>' +
'</my-toggler>' +
'</my-app>');
var frag = template({});
var myApp = frag.firstChild,
buttons = myApp.getElementsByTagName("button");
equal( buttons.length, 1, "there is one button");
equal( innerHTML(buttons[0]) , "hide", "the button's text is hide");
can.trigger(buttons[0], "click");
buttons = myApp.getElementsByTagName("button");
equal(buttons.length, 1, "there is one button");
equal(innerHTML(buttons[0]), "show", "the button's text is show");
can.trigger(buttons[0], "click");
buttons = myApp.getElementsByTagName("button");
equal(buttons.length, 1, "there is one button");
equal(innerHTML(buttons[0]), "hide", "the button's text is hide");
});
test("helpers reference the correct instance (#515)", function () {
expect(2);
can.Component({
tag: 'my-text',
template: can.stache('<p>{{valueHelper}}</p>'),
helpers: {
valueHelper: function () {
return this.attr('value');
}
}
});
var template = can.stache('<my-text value="value1"></my-text><my-text value="value2"></my-text>');
var frag = template({});
equal(frag.firstChild.firstChild.firstChild.nodeValue, 'value1');
equal(frag.lastChild.firstChild.firstChild.nodeValue, 'value2');
});
test('access hypenated attributes via camelCase or hypenated', function () {
can.Component({
tag: 'hyphen',
viewModel: {
},
template: can.stache('<p>{{valueHelper}}</p>'),
helpers: {
valueHelper: function () {
return this.attr('camelCase');
}
}
});
var template = can.stache('<hyphen camel-case="value1"></hyphen>');
var frag = template({});
equal(frag.firstChild.firstChild.firstChild.nodeValue, 'value1');
});
test("a map as viewModel", function () {
var me = new can.Map({
name: "Justin"
});
can.Component.extend({
tag: 'my-viewmodel',
template: can.stache("{{name}}}"),
viewModel: me
});
var template = can.stache('<my-viewmodel></my-viewmodel>');
equal(template().firstChild.firstChild.nodeValue, "Justin");
});
test("content in a list", function () {
var template = can.stache('<my-list>{{name}}</my-list>');
can.Component.extend({
tag: "my-list",
template: can.stache("{{#each items}}<li><content/></li>{{/each}}"),
viewModel: {
items: new can.List([{
name: "one"
}, {
name: "two"
}])
}
});
var lis = template()
.firstChild.getElementsByTagName("li");
equal(innerHTML(lis[0]), "one", "first li has correct content");
equal(innerHTML(lis[1]), "two", "second li has correct content");
});
test("don't update computes unnecessarily", function () {
var sourceAge = 30,
timesComputeIsCalled = 0;
var age = can.compute(function (newVal) {
timesComputeIsCalled++;
if (timesComputeIsCalled === 1) {
ok(true, "reading initial value to set as years");
} else if (timesComputeIsCalled === 2) {
equal(newVal, 31, "updating value to 31");
} else if (timesComputeIsCalled === 3) {
ok(true, "called back another time after set to get the value");
} else {
ok(false, "You've called the callback " + timesComputeIsCalled + " times");
}
if (arguments.length) {
sourceAge = newVal;
} else {
return sourceAge;
}
});
can.Component.extend({
tag: "age-er"
});
var template = can.stache("<age-er years='{age}'></age-er>");
template({
age: age
});
age(31);
});
test("component does not respect can.compute passed via attributes (#540)", function () {
var data = {
compute: can.compute(30)
};
can.Component.extend({
tag: "my-component",
template: can.stache("<span>{{blocks}}</span>")
});
var template = can.stache("<my-component blocks='{compute}'></my-component>");
var frag = template(data);
equal( innerHTML(frag.firstChild.firstChild), "30");
});
test("defined view models (#563)", function () {
var HelloWorldModel = can.Map.extend({
visible: true,
toggle: function () {
this.attr("visible", !this.attr("visible"));
}
});
can.Component.extend({
tag: "my-helloworld",
template: can.stache("<h1>{{#if visible}}visible{{else}}invisible{{/if}}</h1>"),
viewModel: HelloWorldModel
});
var template = can.stache("<my-helloworld></my-helloworld>");
var frag = template({});
equal( innerHTML(frag.firstChild.firstChild), "visible");
});
test("viewModel not rebound correctly (#550)", function () {
var nameChanges = 0;
can.Component.extend({
tag: "viewmodel-rebinder",
events: {
"{name} change": function () {
nameChanges++;
}
}
});
var template = can.stache("<viewmodel-rebinder></viewmodel-rebinder>");
var frag = template();
var viewModel = can.viewModel(can.$(frag.firstChild));