/
handlebars_test.js
2346 lines (1768 loc) · 67.7 KB
/
handlebars_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
// ==========================================================================
// Project: Ember Handlebars Views
// Copyright: ©2011 Strobe Inc. and contributors.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
/*globals TemplateTests:true MyApp:true App:true */
var getPath = Ember.getPath, setPath = Ember.setPath, get = Ember.get, set = Ember.set;
var forEach = Ember.EnumerableUtils.forEach;
var firstGrandchild = function(view) {
return get(get(view, 'childViews').objectAt(0), 'childViews').objectAt(0);
};
var nthChild = function(view, nth) {
return get(view, 'childViews').objectAt(nth || 0);
};
var firstChild = nthChild;
var originalLog, logCalls;
(function() {
Ember.$.fn.caretPosition = function() {
var ctrl = this[0];
var CaretPos = 0;
// IE Support
if (document.selection) {
ctrl.focus();
var Sel = document.selection.createRange ();
Sel.moveStart ('character', -ctrl.value.length);
CaretPos = Sel.text.length;
}
// Firefox support
else if (ctrl.selectionStart || ctrl.selectionStart === '0') {
CaretPos = ctrl.selectionStart;
}
return (CaretPos);
};
Ember.$.fn.setCaretPosition = function(pos) {
var ctrl = this[0];
if(ctrl.setSelectionRange) {
ctrl.focus();
ctrl.setSelectionRange(pos,pos);
} else if (ctrl.createTextRange) {
var range = ctrl.createTextRange();
range.collapse(true);
range.moveEnd('character', pos);
range.moveStart('character', pos);
range.select();
}
};
})();
var view;
var appendView = function() {
Ember.run(function() { view.appendTo('#qunit-fixture'); });
};
var additionalTeardown;
/**
This module specifically tests integration with Handlebars and Ember-specific
Handlebars extensions.
If you add additional template support to Ember.View, you should create a new
file in which to test.
*/
module("Ember.View - handlebars integration", {
setup: function() {
window.TemplateTests = Ember.Namespace.create();
},
teardown: function() {
if (view) {
Ember.run(function() {
view.destroy();
});
view = null;
}
window.TemplateTests = undefined;
}
});
test("template view should call the function of the associated template", function() {
view = Ember.View.create({
templateName: 'test_template',
templates: Ember.Object.create({
test_template: Ember.Handlebars.compile("<h1 id='twas-called'>template was called</h1>")
})
});
appendView();
ok(view.$('#twas-called').length, "the named template was called");
});
test("template view should call the function of the associated template with itself as the context", function() {
view = Ember.View.create({
templateName: 'test_template',
_personName: "Tom DAAAALE",
_i: 0,
personName: Ember.computed(function() {
this._i++;
return this._personName + this._i;
}).cacheable(),
templates: Ember.Object.create({
test_template: Ember.Handlebars.compile("<h1 id='twas-called'>template was called for {{personName}}. Yea {{personName}}</h1>")
})
});
appendView();
equal("template was called for Tom DAAAALE1. Yea Tom DAAAALE1", view.$('#twas-called').text(), "the named template was called with the view as the data source");
});
test("should allow values from normal JavaScript hash objects to be used", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('{{#with person}}{{firstName}} {{lastName}} (and {{pet.name}}){{/with}}'),
person: {
firstName: 'Señor',
lastName: 'CFC',
pet: {
name: 'Fido'
}
}
});
appendView();
equal(view.$().text(), "Señor CFC (and Fido)", "prints out values from a hash");
});
test("htmlSafe should return an instance of Handlebars.SafeString", function() {
var safeString = Ember.String.htmlSafe("you need to be more <b>bold</b>");
ok(safeString instanceof Handlebars.SafeString, "should return SafeString");
});
test("should escape HTML in normal mustaches", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('{{output}}'),
output: "you need to be more <b>bold</b>"
});
Ember.run(function() { view.appendTo('#qunit-fixture'); });
equal(view.$('b').length, 0, "does not create an element");
equal(view.$().text(), 'you need to be more <b>bold</b>', "inserts entities, not elements");
Ember.run(function() { set(view, 'output', "you are so <i>super</i>"); });
equal(view.$().text(), 'you are so <i>super</i>', "updates with entities, not elements");
equal(view.$('i').length, 0, "does not create an element when value is updated");
});
test("should not escape HTML in triple mustaches", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('{{{output}}}'),
output: "you need to be more <b>bold</b>"
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('b').length, 1, "creates an element");
Ember.run(function() {
set(view, 'output', "you are so <i>super</i>");
});
equal(view.$('i').length, 1, "creates an element when value is updated");
});
test("should not escape HTML if string is a Handlebars.SafeString", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('{{output}}'),
output: new Handlebars.SafeString("you need to be more <b>bold</b>")
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('b').length, 1, "creates an element");
Ember.run(function() {
set(view, 'output', new Handlebars.SafeString("you are so <i>super</i>"));
});
equal(view.$('i').length, 1, "creates an element when value is updated");
});
TemplateTests = {};
test("child views can be inserted using the {{view}} Handlebars helper", function() {
var templates = Ember.Object.create({
nester: Ember.Handlebars.compile("<h1 id='hello-world'>Hello {{world}}</h1>{{view \"TemplateTests.LabelView\"}}"),
nested: Ember.Handlebars.compile("<div id='child-view'>Goodbye {{cruel}} {{world}}</div>")
});
TemplateTests.LabelView = Ember.View.extend({
tagName: "aside",
world: "world?",
templateName: 'nested',
templates: templates
});
view = Ember.View.create({
world: "world!",
templateName: 'nester',
templates: templates
});
if (Ember.VIEW_PRESERVES_CONTEXT) {
view.set('cruel', "cruel");
} else {
TemplateTests.LabelView.reopen({ cruel: "cruel" });
}
appendView();
ok(view.$("#hello-world:contains('Hello world!')").length, "The parent view renders its contents");
if (Ember.VIEW_PRESERVES_CONTEXT) {
ok(view.$("#child-view:contains('Goodbye cruel world!')").length === 1, "The child view renders its content once");
ok(view.$().text().match(/Hello world!.*Goodbye cruel world\!/), "parent view should appear before the child view");
} else {
ok(view.$("#child-view:contains('Goodbye cruel world?')").length === 1, "The child view renders its content once");
ok(view.$().text().match(/Hello world!.*Goodbye cruel world\?/), "parent view should appear before the child view");
}
});
test("should accept relative paths to views", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('Hey look, at {{view "myCool.view"}}'),
myCool: Ember.Object.create({
view: Ember.View.extend({
template: Ember.Handlebars.compile("my cool view")
})
})
});
appendView();
equal(view.$().text(), "Hey look, at my cool view");
});
test("child views can be inserted inside a bind block", function() {
var templates = Ember.Object.create({
nester: Ember.Handlebars.compile("<h1 id='hello-world'>Hello {{world}}</h1>{{view \"TemplateTests.BQView\"}}"),
nested: Ember.Handlebars.compile("<div id='child-view'>Goodbye {{#with content}}{{blah}} {{view \"TemplateTests.OtherView\"}}{{/with}} {{world}}</div>"),
other: Ember.Handlebars.compile("cruel")
});
TemplateTests.BQView = Ember.View.extend({
tagName: "blockquote",
cruel: "cruel",
world: "world?",
templateName: 'nested',
templates: templates
});
TemplateTests.OtherView = Ember.View.extend({
templates: templates,
templateName: 'other'
});
view = Ember.View.create({
world: "world!",
templateName: 'nester',
templates: templates
});
if (Ember.VIEW_PRESERVES_CONTEXT) {
view.set('content', Ember.Object.create({ blah: "wot" }));
} else {
TemplateTests.BQView.reopen({ content: Ember.Object.create({ blah: "wot" }) });
}
appendView();
ok(view.$("#hello-world:contains('Hello world!')").length, "The parent view renders its contents");
if (Ember.VIEW_PRESERVES_CONTEXT) {
ok(view.$("blockquote").text().match(/Goodbye.*wot.*cruel.*world\!/), "The child view renders its content once");
ok(view.$().text().match(/Hello world!.*Goodbye.*wot.*cruel.*world\!/), "parent view should appear before the child view");
} else {
ok(view.$("blockquote").text().match(/Goodbye.*wot.*cruel.*world\?/), "The child view renders its content once");
ok(view.$().text().match(/Hello world!.*Goodbye.*wot.*cruel.*world\?/), "parent view should appear before the child view");
}
});
test("Ember.View should bind properties in the parent context", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('<h1 id="first">{{#with content}}{{wham}}-{{../blam}}{{/with}}</h1>'),
content: Ember.Object.create({
wham: 'bam'
}),
blam: "shazam"
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('#first').text(), "bam-shazam", "renders parent properties");
});
test("Ember.View should bind properties in the grandparent context", function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('<h1 id="first">{{#with content}}{{#with thankYou}}{{value}}-{{../wham}}-{{../../blam}}{{/with}}{{/with}}</h1>'),
content: Ember.Object.create({
wham: 'bam',
thankYou: Ember.Object.create({
value: "ma'am"
})
}),
blam: "shazam"
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('#first').text(), "ma'am-bam-shazam", "renders grandparent properties");
});
test("Ember.View should update when a property changes and the bind helper is used", function() {
var templates = Ember.Object.create({
foo: Ember.Handlebars.compile('<h1 id="first">{{#with content}}{{bind "wham"}}{{/with}}</h1>')
});
view = Ember.View.create({
templateName: 'foo',
templates: templates,
content: Ember.Object.create({
wham: 'bam',
thankYou: "ma'am"
})
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('#first').text(), "bam", "precond - view renders Handlebars template");
Ember.run(function() { set(get(view, 'content'), 'wham', 'bazam'); });
equal(view.$('#first').text(), "bazam", "view updates when a bound property changes");
});
test("Ember.View should update when a property changes and no bind helper is used", function() {
var templates = Ember.Object.create({
foo: Ember.Handlebars.compile('<h1 id="first">{{#with content}}{{wham}}{{/with}}</h1>')
});
view = Ember.View.create({
templateName: 'foo',
templates: templates,
content: Ember.Object.create({
wham: 'bam',
thankYou: "ma'am"
})
});
Ember.run(function() {
view.appendTo('#qunit-fixture');
});
equal(view.$('#first').text(), "bam", "precond - view renders Handlebars template");
Ember.run(function() { set(get(view, 'content'), 'wham', 'bazam'); });
equal(view.$('#first').text(), "bazam", "view updates when a bound property changes");
});
test("Ember.View should update when the property used with the #with helper changes", function() {
var templates = Ember.Object.create({
foo: Ember.Handlebars.compile('<h1 id="first">{{#with content}}{{wham}}{{/with}}</h1>')
});
view = Ember.View.create({
templateName: 'foo',
templates: templates,
content: Ember.Object.create({
wham: 'bam',
thankYou: "ma'am"
})
});
appendView();
equal(view.$('#first').text(), "bam", "precond - view renders Handlebars template");
Ember.run(function() {
set(view, 'content', Ember.Object.create({
wham: 'bazam'
}));
});
equal(view.$('#first').text(), "bazam", "view updates when a bound property changes");
});
test("should not update when a property is removed from the view", function() {
var templates = Ember.Object.create({
foo: Ember.Handlebars.compile('<h1 id="first">{{#bind "content"}}{{#bind "foo"}}{{bind "baz"}}{{/bind}}{{/bind}}</h1>')
});
view = Ember.View.create({
templateName: 'foo',
templates: templates,
content: Ember.Object.create({
foo: Ember.Object.create({
baz: "unicorns"
})
})
});
appendView();
equal(view.$('#first').text(), "unicorns", "precond - renders the bound value");
var oldContent = get(view, 'content');
Ember.run(function() {
set(view, 'content', Ember.Object.create({
foo: Ember.Object.create({
baz: "ninjas"
})
}));
});
equal(view.$('#first').text(), 'ninjas', "updates to new content value");
Ember.run(function() {
setPath(oldContent, 'foo.baz', 'rockstars');
});
Ember.run(function() {
setPath(oldContent, 'foo.baz', 'ewoks');
});
equal(view.$('#first').text(), "ninjas", "does not update removed object");
});
test("Handlebars templates update properties if a content object changes", function() {
var templates;
templates = Ember.Object.create({
menu: Ember.Handlebars.compile('<h1>Today\'s Menu</h1>{{#bind "coffee"}}<h2>{{color}} coffee</h2><span id="price">{{bind "price"}}</span>{{/bind}}')
});
Ember.run(function() {
view = Ember.View.create({
templateName: 'menu',
templates: templates,
coffee: Ember.Object.create({
color: 'brown',
price: '$4'
})
});
});
appendView();
equal(view.$('h2').text(), "brown coffee", "precond - renders color correctly");
equal(view.$('#price').text(), '$4', "precond - renders price correctly");
Ember.run(function() {
set(view, 'coffee', Ember.Object.create({
color: "mauve",
price: "$4.50"
}));
});
equal(view.$('h2').text(), "mauve coffee", "should update name field when content changes");
equal(view.$('#price').text(), "$4.50", "should update price field when content changes");
Ember.run(function() {
set(view, 'coffee', Ember.Object.create({
color: "mauve",
price: "$5.50"
}));
});
equal(view.$('h2').text(), "mauve coffee", "should update name field when content changes");
equal(view.$('#price').text(), "$5.50", "should update price field when content changes");
Ember.run(function() {
setPath(view, 'coffee.price', "$5");
});
equal(view.$('#price').text(), "$5", "should update price field when price property is changed");
Ember.run(function() {
view.destroy();
});
});
test("Template updates correctly if a path is passed to the bind helper", function() {
var templates;
templates = Ember.Object.create({
menu: Ember.Handlebars.compile('<h1>{{bind "coffee.price"}}</h1>')
});
view = Ember.View.create({
templateName: 'menu',
templates: templates,
coffee: Ember.Object.create({
price: '$4'
})
});
appendView();
equal(view.$('h1').text(), "$4", "precond - renders price");
Ember.run(function() {
setPath(view, 'coffee.price', "$5");
});
equal(view.$('h1').text(), "$5", "updates when property changes");
Ember.run(function() {
set(view, 'coffee', { price: "$6" });
});
equal(view.$('h1').text(), "$6", "updates when parent property changes");
});
// test("Template updates correctly if a path is passed to the bind helper and the context object is an Ember.ObjectController", function() {
// var templates;
// templates = Ember.Object.create({
// menu: Ember.Handlebars.compile('<h1>{{bind "coffee.price"}}</h1>')
// });
// var controller = Ember.ObjectController.create();
// var realObject = Ember.Object.create({
// price: "$4"
// });
// set(controller, 'content', realObject);
// var view = Ember.View.create({
// templateName: 'menu',
// templates: templates,
// coffee: controller
// });
// view.createElement();
// equal(view.$('h1').text(), "$4", "precond - renders price");
// set(realObject, 'price', "$5");
// equal(view.$('h1').text(), "$5", "updates when property is set on real object");
// Ember.run(function() {
// set(controller, 'price', "$6" );
// });
// equal(view.$('h1').text(), "$6", "updates when property is set on object controller");
// });
test("should update the block when object passed to #if helper changes", function() {
var templates;
templates = Ember.Object.create({
menu: Ember.Handlebars.compile('<h1>{{#if inception}}{{INCEPTION}}{{/if}}</h1>')
});
view = Ember.View.create({
templateName: 'menu',
templates: templates,
INCEPTION: "BOOOOOOOONG doodoodoodoodooodoodoodoo",
inception: 'OOOOoooooOOOOOOooooooo'
});
appendView();
equal(view.$('h1').text(), "BOOOOOOOONG doodoodoodoodooodoodoodoo", "renders block if a string");
var tests = [false, null, undefined, [], '', 0];
forEach(tests, function(val) {
Ember.run(function() {
set(view, 'inception', val);
});
equal(view.$('h1').text(), '', Ember.String.fmt("hides block when conditional is '%@'", [String(val)]));
Ember.run(function() {
set(view, 'inception', true);
});
equal(view.$('h1').text(), "BOOOOOOOONG doodoodoodoodooodoodoodoo", "precond - renders block when conditional is true");
});
});
test("should update the block when object passed to #unless helper changes", function() {
var templates;
templates = Ember.Object.create({
advice: Ember.Handlebars.compile('<h1>{{#unless onDrugs}}{{doWellInSchool}}{{/unless}}</h1>')
});
view = Ember.View.create({
templateName: 'advice',
templates: templates,
onDrugs: true,
doWellInSchool: "Eat your vegetables"
});
appendView();
equal(view.$('h1').text(), "", "hides block if true");
var tests = [false, null, undefined, [], '', 0];
forEach(tests, function(val) {
Ember.run(function() {
set(view, 'onDrugs', val);
});
equal(view.$('h1').text(), 'Eat your vegetables', Ember.String.fmt("renders block when conditional is '%@'; %@", [String(val), Ember.typeOf(val)]));
Ember.run(function() {
set(view, 'onDrugs', true);
});
equal(view.$('h1').text(), "", "precond - hides block when conditional is true");
});
});
test("should update the block when object passed to #if helper changes and an inverse is supplied", function() {
var templates;
templates = Ember.Object.create({
menu: Ember.Handlebars.compile('<h1>{{#if inception}}{{INCEPTION}}{{else}}{{SAD}}{{/if}}</h1>')
});
view = Ember.View.create({
templateName: 'menu',
templates: templates,
INCEPTION: "BOOOOOOOONG doodoodoodoodooodoodoodoo",
inception: false,
SAD: 'BOONG?'
});
appendView();
equal(view.$('h1').text(), "BOONG?", "renders alternate if false");
Ember.run(function() { set(view, 'inception', true); });
var tests = [false, null, undefined, [], '', 0];
forEach(tests, function(val) {
Ember.run(function() {
set(view, 'inception', val);
});
equal(view.$('h1').text(), 'BOONG?', Ember.String.fmt("renders alternate if %@", [String(val)]));
Ember.run(function() {
set(view, 'inception', true);
});
equal(view.$('h1').text(), "BOOOOOOOONG doodoodoodoodooodoodoodoo", "precond - renders block when conditional is true");
});
});
// test("Should insert a localized string if the {{loc}} helper is used", function() {
// Ember.stringsFor('en', {
// 'Brazil': 'Brasilia'
// });
// templates = Ember.Object.create({
// 'loc': Ember.Handlebars.compile('<h1>Country: {{loc "Brazil"}}')
// });
// var view = Ember.View.create({
// templateName: 'loc',
// templates: templates,
// country: 'Brazil'
// });
// view.createElement();
// equal(view.$('h1').text(), 'Country: Brasilia', "returns localized value");
// });
test("Template views return a no-op function if their template cannot be found", function() {
view = Ember.View.create({
templateName: 'cantBeFound'
});
raises(function() {
var template = get(view, 'template');
ok(Ember.typeOf(template) === 'function', 'template should be a function');
equal(template(), '', 'should return an empty string');
});
});
test("Template views add an elementId to child views created using the view helper", function() {
var templates = Ember.Object.create({
parent: Ember.Handlebars.compile('<div>{{view "TemplateTests.ChildView"}}</div>'),
child: Ember.Handlebars.compile("I can't believe it's not butter.")
});
TemplateTests.ChildView = Ember.View.extend({
templates: templates,
templateName: 'child'
});
view = Ember.View.create({
templates: templates,
templateName: 'parent'
});
appendView();
var childView = getPath(view, 'childViews.firstObject');
equal(view.$().children().first().children().first().attr('id'), get(childView, 'elementId'));
});
test("views set the template of their children to a passed block", function() {
var templates = Ember.Object.create({
parent: Ember.Handlebars.compile('<h1>{{#view "TemplateTests.NoTemplateView"}}<span>It worked!</span>{{/view}}</h1>')
});
TemplateTests.NoTemplateView = Ember.View.extend();
view = Ember.View.create({
templates: templates,
templateName: 'parent'
});
appendView();
ok(view.$('h1:has(span)').length === 1, "renders the passed template inside the parent template");
});
if (Ember.VIEW_PRESERVES_CONTEXT) {
test("views render their template in the context of the parent view's context", function() {
var templates = Ember.Object.create({
parent: Ember.Handlebars.compile('<h1>{{#with content}}{{#view}}{{firstName}} {{lastName}}{{/view}}{{/with}}</h1>')
});
view = Ember.View.create({
templates: templates,
templateName: 'parent',
content: {
firstName: "Lana",
lastName: "del Heeeyyyyyy"
}
});
appendView();
equal(view.$('h1').text(), "Lana del Heeeyyyyyy", "renders properties from parent context");
});
test("views make a view keyword available that allows template to reference view context", function() {
var templates = Ember.Object.create({
parent: Ember.Handlebars.compile('<h1>{{#with content}}{{#view subview}}{{view.firstName}} {{lastName}}{{/view}}{{/with}}</h1>')
});
view = Ember.View.create({
templates: templates,
templateName: 'parent',
content: {
subview: Ember.View.extend({
firstName: "Brodele"
}),
firstName: "Lana",
lastName: "del Heeeyyyyyy"
}
});
appendView();
equal(view.$('h1').text(), "Brodele del Heeeyyyyyy", "renders properties from parent context");
});
} else {
test("should pass hash arguments to the view object", function() {
TemplateTests.bindTestObject = Ember.Object.create({
bar: 'bat'
});
TemplateTests.HashArgTemplateView = Ember.View.extend({
});
Ember.run(function() {
view = Ember.View.create({
template: Ember.Handlebars.compile('{{#view TemplateTests.HashArgTemplateView fooBinding="TemplateTests.bindTestObject.bar"}}{{foo}}{{/view}}')
});
appendView();
});
equal(view.$().text(), "bat", "prints initial bound value");
Ember.run(function() {
set(TemplateTests.bindTestObject, 'bar', 'brains');
});
equal(view.$().text(), "brains", "prints updated bound value");
});
}
test("should warn if setting a template on a view with a templateName already specified", function() {
view = Ember.View.create({
childView: Ember.View.extend({
templateName: 'foo'
}),
template: Ember.Handlebars.compile('{{#view childView}}test{{/view}}')
});
raises(function() {
appendView();
}, Error, "raises if conflicting template and templateName are provided");
Ember.run(function() {
view.destroy();
});
view = Ember.View.create({
childView: Ember.View.extend(),
template: Ember.Handlebars.compile('{{#view childView templateName="foo"}}test{{/view}}')
});
raises(function() {
appendView();
}, Error, "raises if conflicting template and templateName are provided via a Handlebars template");
});
test("Child views created using the view helper should have their parent view set properly", function() {
TemplateTests = {};
var template = '{{#view "Ember.View"}}{{#view "Ember.View"}}{{view "Ember.View"}}{{/view}}{{/view}}';
view = Ember.View.create({
template: Ember.Handlebars.compile(template)
});
appendView();
var childView = firstGrandchild(view);
equal(childView, get(firstChild(childView), 'parentView'), 'parent view is correct');
});
test("Child views created using the view helper should have their IDs registered for events", function() {
TemplateTests = {};
var template = '{{view "Ember.View"}}{{view "Ember.View" id="templateViewTest"}}';
view = Ember.View.create({
template: Ember.Handlebars.compile(template)
});
appendView();
var childView = firstChild(view);
var id = childView.$()[0].id;
equal(Ember.View.views[id], childView, 'childView without passed ID is registered with Ember.View.views so that it can properly receive events from RootResponder');
childView = nthChild(view, 1);
id = childView.$()[0].id;
equal(id, 'templateViewTest', 'precond -- id of childView should be set correctly');
equal(Ember.View.views[id], childView, 'childView with passed ID is registered with Ember.View.views so that it can properly receive events from RootResponder');
});
test("Child views created using the view helper and that have a viewName should be registered as properties on their parentView", function() {
TemplateTests = {};
var template = '{{#view Ember.View}}{{view Ember.View viewName="ohai"}}{{/view}}';
view = Ember.View.create({
template: Ember.Handlebars.compile(template)
});
appendView();
var parentView = firstChild(view),
childView = firstGrandchild(view);
equal(get(parentView, 'ohai'), childView);
});
test("Collection views that specify an example view class have their children be of that class", function() {
TemplateTests.ExampleViewCollection = Ember.CollectionView.extend({
itemViewClass: Ember.View.extend({
isCustom: true
}),
content: Ember.A(['foo'])
});
var parentView = Ember.View.create({
template: Ember.Handlebars.compile('{{#collection "TemplateTests.ExampleViewCollection"}}OHAI{{/collection}}')
});
Ember.run(function() {
parentView.append();
});
ok(firstGrandchild(parentView).isCustom, "uses the example view class");
Ember.run(function() {
parentView.destroy();
});
});
test("itemViewClass works in the #collection helper", function() {
TemplateTests.ExampleController = Ember.ArrayProxy.create({
content: Ember.A(['alpha'])
});
TemplateTests.ExampleItemView = Ember.View.extend({
isAlsoCustom: true
});
var parentView = Ember.View.create({
template: Ember.Handlebars.compile('{{#collection contentBinding="TemplateTests.ExampleController" itemViewClass="TemplateTests.ExampleItemView"}}beta{{/collection}}')
});
Ember.run(function() {
parentView.append();
});
ok(firstGrandchild(parentView).isAlsoCustom, "uses the example view class specified in the #collection helper");
Ember.run(function() {
parentView.destroy();
});
});
test("itemViewClass works in the #collection helper relatively", function() {
TemplateTests.ExampleController = Ember.ArrayProxy.create({
content: Ember.A(['alpha'])
});
TemplateTests.ExampleItemView = Ember.View.extend({
isAlsoCustom: true
});
TemplateTests.CollectionView = Ember.CollectionView.extend({
possibleItemView: TemplateTests.ExampleItemView
});
var parentView = Ember.View.create({
template: Ember.Handlebars.compile('{{#collection TemplateTests.CollectionView contentBinding="TemplateTests.ExampleController" itemViewClass="possibleItemView"}}beta{{/collection}}')
});
Ember.run(function() {
parentView.append();
});
ok(firstGrandchild(parentView).isAlsoCustom, "uses the example view class specified in the #collection helper");
Ember.run(function() {
parentView.destroy();
});
});