forked from gmac/backbone.epoxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
1169 lines (893 loc) · 34.4 KB
/
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
// Epoxy.Model
// -----------
describe("Backbone.Epoxy.Model", function() {
var model;
// Primay model for test suite:
var TestModel = Backbone.Epoxy.Model.extend({
defaults: {
firstName: "Charlie",
lastName: "Brown",
payment: 100,
isSelected: false,
testArray: []
},
computeds: {
// Tests setting a computed property with the direct single-function getter shorthand:
fullName: function() {
return this.get( "firstName" ) +" "+ this.get( "lastName" );
},
// Tests two facets:
// 1) computed dependencies definition order (defined before/after a dependency).
// 2) computed dependencies building ontop of one another.
paymentLabel: function() {
return this.get( "fullName" ) +" paid "+ this.get( "paymentCurrency" );
},
// Tests defining a read/write computed property with getters and setters:
paymentCurrency: {
get: function() {
return "$"+ this.get( "payment" );
},
set: function( value ) {
return value ? {payment: parseInt(value.replace("$", ""), 10)} : value;
}
},
// Tests defining a computed property with unreachable values...
// first/last names are accessed conditionally, therefore cannot be automatically detected.
// field dependencies may be declared manually to address this (ugly though);
// a better solution would be to collect both "first" and "last" as local vars,
// then release the locally established values conditionally.
unreachable: {
deps: ["firstName", "lastName", "isSelected"],
get: function() {
return this.get("isSelected") ? this.get("lastName") : this.get("firstName");
}
}
},
initialize: function() {
}
});
// Secondary model, established for some relationship testing:
var ForeignModel = Backbone.Epoxy.Model.extend({
defaults: {
avgPayment: 200
}
});
// Setup
beforeEach(function() {
model = new TestModel();
});
// Teardown
afterEach(function() {
model.clearComputeds();
model = null;
});
it("should construct model with class options defined.", function() {
var obj = {};
var model = new Backbone.Epoxy.Model({}, {
computeds: obj
});
expect( model.computeds ).to.equal( obj );
});
it("should construct model with class options defined.", function() {
var obj = {};
var model = new Backbone.Epoxy.Model({}, {
computeds: obj
});
expect( model.computeds ).to.equal( obj );
});
it("should allow Epoxy model configuration to mixin with another Backbone Model.", function() {
var MixinModel = Backbone.Model.extend({
defaults: {
avgPayment: 500
},
initialize: function() {
this.initComputeds();
},
computeds: {
avgPaymentDsp: function() {
return "$"+this.get( "avgPayment" );
}
}
});
Backbone.Epoxy.Model.mixin( MixinModel.prototype );
var model = new MixinModel();
expect( model.get("avgPaymentDsp") ).to.equal( "$500" );
});
it("should use .get() and .set() to modify native properties.", function() {
model.set( "isSelected", true );
expect( model.get("isSelected") ).to.equal( true );
});
it("should get native model attributes using '.toJSON()'.", function() {
var json = model.toJSON();
expect( _.size(json) ).to.equal( 5 );
});
it("should get native and computed model attributes using '.toJSON({computed:true})'.", function() {
var json = model.toJSON({computed:true});
expect( _.size(json) ).to.equal( 9 );
expect( json.fullName ).to.equal( "Charlie Brown" );
});
it("should allow direct management of array attributes using the '.modifyArray' method.", function() {
expect(model.get( "testArray" )).to.have.length( 0 );
model.modifyArray("testArray", "push", "beachball");
expect(model.get( "testArray" )).to.have.length( 1 );
});
it("should defer all action when using '.modifyArray' on a non-array object.", function() {
model.modifyArray("isSelected", "push", "beachball");
expect( model.get( "isSelected" ) ).to.equal( false );
});
it("should assume computed properties defined as functions to be getters.", function() {
var obsGetter = model._c.fullName._get;
var protoGetter = TestModel.prototype.computeds.fullName;
expect( obsGetter === protoGetter ).to.equal( true );
});
it("should use '.computeds' to automatically construct computed properties.", function() {
var hasFullName = model.hasComputed("fullName");
var hasDonation = model.hasComputed("paymentCurrency");
expect( hasFullName && hasDonation ).to.equal( true );
});
it("should allow computed properties to be constructed out of dependency order (dependents may preceed their dependencies).", function() {
expect( model.get("paymentLabel") ).to.equal( "Charlie Brown paid $100" );
});
it("should allow computed properties to be defined with manual dependency declarations.", function() {
// Test initial reachable value:
expect( model.get("unreachable") ).to.equal( "Charlie" );
// Change conditional value to point at the originally unreachable value:
model.set("isSelected", true);
expect( model.get("unreachable") ).to.equal( "Brown" );
// Change unreachable value
model.set("lastName", "Black");
expect( model.get("unreachable") ).to.equal( "Black" );
});
it("should inject manual dependency declarations as getter arguments.", function() {
model.addComputed("getterInjection", function(first, last) {
return first +" "+ last;
}, "firstName", "lastName");
expect( model.get("getterInjection") ).to.equal( "Charlie Brown" );
});
it("should use .addComputed() to define computed properties.", function() {
model.addComputed("nameReverse", function() {
return this.get("lastName") +", "+ this.get("firstName");
});
expect( model.get("nameReverse") ).to.equal( "Brown, Charlie" );
});
it("should use .addComputed() to define properties with passed dependencies.", function() {
model.addComputed("unreachable", function() {
return this.get("payment") > 50 ? this.get("firstName") : this.get("lastName");
}, "payment", "firstName", "lastName");
// Test initial reachable value:
expect( model.get("unreachable") ).to.equal( "Charlie" );
// Change conditional value to point at the originally unreachable value:
model.set("payment", 0);
expect( model.get("unreachable") ).to.equal( "Brown" );
// Change unreachable value
model.set("lastName", "Black");
expect( model.get("unreachable") ).to.equal( "Black" );
});
it("should use .addComputed() to define new properties from a params object.", function() {
model.addComputed("addedProp", {
deps: ["payment", "firstName", "lastName"],
get: function() {
return this.get("payment") > 50 ? this.get("firstName") : this.get("lastName");
},
set: function( value ) {
return {payment: value};
}
});
// Test initial reachable value:
expect( model.get("addedProp") ).to.equal( "Charlie" );
// Change conditional value to point at the originally unreachable value:
model.set("payment", 0);
expect( model.get("addedProp") ).to.equal( "Brown" );
// Change unreachable value
model.set("lastName", "Black");
expect( model.get("addedProp") ).to.equal( "Black" );
// Set computed value
model.set("addedProp", 123);
expect( model.get("payment") ).to.equal( 123 );
});
it("should use .get() to access both model attributes and computed properties.", function() {
var firstName = (model.get("firstName") === "Charlie");
var fullName = (model.get("fullName") === "Charlie Brown");
expect( firstName && fullName ).to.equal( true );
});
it("should automatically map and bind computed property dependencies.", function() {
var fullPre = (model.get( "fullName" ) === "Charlie Brown");
model.set( "lastName", "Black" );
var fullPost = (model.get( "fullName" ) === "Charlie Black");
expect( fullPre && fullPost ).to.equal( true );
});
it("should automatically map and bind computed property dependencies on foreign Epoxy models.", function() {
var averages = new ForeignModel();
model.addComputed("percentAvgPayment", function() {
return this.get("payment") / averages.get("avgPayment");
});
expect( model.get("percentAvgPayment") ).to.equal( 0.5 );
averages.set("avgPayment", 400);
expect( model.get("percentAvgPayment") ).to.equal( 0.25 );
averages.clearComputeds();
});
it("should manage extended graphs of computed dependencies.", function() {
expect( model.get("paymentLabel") ).to.equal( "Charlie Brown paid $100" );
model.set("payment", 150);
expect( model.get("paymentLabel") ).to.equal( "Charlie Brown paid $150" );
});
it("should use .set() to modify normal model attributes.", function() {
model.set("payment", 150);
expect( model.get("payment") ).to.equal( 150 );
expect( model.get("paymentCurrency") ).to.equal( "$150" );
});
it("should use .set() for virtual computed properties to pass values along to the model.", function() {
expect( model.get("payment") ).to.equal( 100 );
model.set("paymentCurrency", "$200");
expect( model.get("payment") ).to.equal( 200 );
expect( model.get("paymentCurrency") ).to.equal( "$200" );
});
it("should throw .set() error when modifying read-only computed properties.", function() {
function testForError() {
model.set("fullName", "Charlie Black");
}
expect( testForError ).to.throw();
});
it("should use .set() to allow computed properties to cross-set one another.", function() {
model.addComputed("crossSetter", {
get: function() {
return this.get("isSelected");
},
set: function( value ) {
return {isSelected: value};
}
});
expect( model.get("crossSetter") ).to.equal( false );
model.set("crossSetter", true );
expect( model.get("isSelected") ).to.equal( true );
});
it("should throw .set() error in response to circular setter references.", function() {
model.addComputed("loopSetter1", {
get: function() {
return "Nothing";
},
set: function( value ) {
return {loopSetter2: false};
}
});
model.addComputed("loopSetter2", {
get: function() {
return "Nothing";
},
set: function( value ) {
return {loopSetter1: false};
}
});
function circularRef() {
model.set("loopSetter1", true );
}
expect( circularRef ).to.throw();
});
});
// Epoxy.View
// ----------
describe("Backbone.Epoxy.View", function() {
// Collection test components:
var CollectionView = Backbone.Epoxy.View.extend({
el: "<li><span class='name-dsp' data-bind='text:name'></span> <button class='name-remove'>x</button></li>"
});
var TestCollection = Backbone.Collection.extend({
model: Backbone.Model
});
// Test model:
window.dataModel = new (Backbone.Epoxy.Model.extend({
defaults: {
firstName: "Luke",
lastName: "Skywalker",
preference: "b",
active: true,
valOptions: "1",
valDefault: "1",
valEmpty: "1",
valBoth: "1",
valMulti: "1",
valCollect: ""
},
computeds: {
firstNameError: function() {
return !this.get( "firstName" );
},
lastNameError: function() {
return !this.get( "lastName" );
},
errorDisplay: function() {
var first = this.get( "firstName" );
var last = this.get( "lastName" );
return (!first || !last) ? "block" : "none";
}
}
}));
window.viewModel = new (Backbone.Epoxy.Model.extend({
defaults: {
checkList: ["b"],
optionsList: [
{value: "0", label: "Luke Skywalker"},
{value: "1", label: "Han Solo"},
{value: "2", label: "Obi-Wan Kenobi"}
],
optDefault: "default",
optEmpty: "empty"
}
}));
// Basic bindings test view:
var domView = new (Backbone.Epoxy.View.extend({
el: "#dom-view",
model: dataModel,
viewModel: viewModel,
bindings: "data-bind",
bindingHandlers: {
printArray: function( $element, value ) {
$element.text( value.slice().sort().join(", ") );
},
sayYesNo: {
get: function( $element ) {
return {active: $element.val().indexOf("Y") === 0 };
},
set: function( $element, value ) {
$element.val( value ? "Y" : "N" );
}
}
},
computeds: {
checkedCount: function() {
return "Checked items: "+ this.getBinding("checkList").length;
},
nameDisplay: {
deps: ["lastName", "firstName"],
get: function( lastName, firstName ) {
return "<strong>"+ lastName +"</strong>, "+ firstName;
},
set: function( value ) {
this.nameDisplaySetterValue = value;
}
}
}
}));
// Modifiers / Collections testing view:
var modView = new (Backbone.Epoxy.View.extend({
el: "#mod-view",
model: dataModel,
viewModel: viewModel,
collection: new TestCollection(),
itemView: CollectionView,
bindings: "data-bind",
events: {
"click .name-add": "onAddName",
"click .name-remove": "onRemoveName"
},
onAddName: function() {
var input = this.$( ".name-input" );
if ( input.val() ) {
this.collection.add({
name: input.val()
});
input.val("");
}
},
onRemoveName: function( evt ) {
var i = $( evt.target ).closest( "li" ).index();
this.collection.remove( this.collection.at(i) );
}
}));
// Bindings map declaration:
var tmplView = new (Backbone.Epoxy.View.extend({
el: $("#tmpl-view-tmpl").html(),
model: dataModel,
viewModel: viewModel,
bindings: {
".user-first": "text:firstName",
".user-last": "text:lastName"
},
initialize: function() {
$("#tmpl-view-tmpl").after( this.$el );
}
}));
// Setup
beforeEach(function() {
});
// Teardown
afterEach(function() {
var defaults = _.clone( viewModel.defaults );
defaults.checkList = _.clone( defaults.checkList );
defaults.optionsList = _.clone( defaults.optionsList );
dataModel.set( dataModel.defaults );
viewModel.set( defaults );
modView.collection.reset();
});
// Simple visibility check to replace ".is(':visible')", for basic Zepto caompatibility:
function isVisible($el) {
var dsp = $el.css("display");
return dsp !== 'none';
}
it("should construct view with class options defined.", function() {
var model = new Backbone.Model();
var obj = {};
var view = new Backbone.Epoxy.View({
model: model,
viewModel: model,
computeds: obj,
bindings: "my-binding",
bindingFilters: obj,
bindingHandlers: obj,
bindingSources: obj
});
expect( view.model ).to.equal( model );
expect( view.viewModel ).to.equal( model );
expect( view.computeds ).to.equal( obj );
expect( view.bindings ).to.equal( "my-binding" );
expect( view.bindingFilters ).to.equal( obj );
expect( view.bindingHandlers ).to.equal( obj );
expect( view.bindingSources ).to.exist; // << "obj" is copied, so has new identity.
});
it("should allow Epoxy view configuration to mixin with another Backbone View.", function() {
var MixinView = Backbone.View.extend({
el: "<div data-bind='text:ship'></div>",
model: new Backbone.Model({ship:"Deathstar"}),
initialize: function() {
this.applyBindings();
}
});
Backbone.Epoxy.View.mixin( MixinView.prototype );
var view = new MixinView();
expect( view.$el.text() ).to.equal( "Deathstar" );
});
it("should bind view elements to model via binding selector map.", function() {
var $el = $("#tmpl-view .user-first");
expect( $el.text() ).to.equal( "Luke" );
});
it("should bind view elements to model via element attribute query.", function() {
var $el = $("#dom-view .test-text-first");
expect( $el.text() ).to.equal( "Luke" );
});
it("should include top-level view container in bindings searches.", function() {
var view1 = new (Backbone.Epoxy.View.extend({
el: "<span data-bind='text:firstName'></span>",
model: dataModel,
bindings: "data-bind"
}));
var view2 = new (Backbone.Epoxy.View.extend({
el: "<span class='first-name'></span>",
model: dataModel,
bindings: {
".first-name": "text:firstName"
}
}));
expect( view1.$el.text() ).to.equal( "Luke" );
expect( view2.$el.text() ).to.equal( "Luke" );
});
it("should include top-level view container in bindings searches via :el selector.", function() {
var view = new (Backbone.Epoxy.View.extend({
el: "<span></span>",
model: dataModel,
bindings: {
":el": "text:firstName"
}
}));
expect( view.$el.text() ).to.equal( "Luke" );
});
it("should throw error in response to undefined property bindings.", function() {
var ErrorView = Backbone.Epoxy.View.extend({
el: "<div><span data-bind='text:undefinedProp'></span></div>",
model: dataModel,
bindings: "data-bind"
});
function testForError(){
var error = new ErrorView();
}
expect( testForError ).to.throw();
});
it("should allow custom bindings to set data into the view.", function() {
var $els = $(".test-custom-binding");
expect( $els.text() ).to.equal( "b" );
viewModel.set("checkList", ["c","a"]);
expect( $els.text() ).to.equal( "a, c" );
});
it("should allow custom bindings to get data from the view.", function() {
var $el = $(".test-yes-no");
expect( $el.val() ).to.equal( "Y" );
// Change through model, look for view change:
dataModel.set("active", false);
expect( $el.val() ).to.equal( "N" );
// Change through view, look for model change:
$el.val( "Y" ).trigger( "change" );
expect( dataModel.get("active") ).to.equal( true );
});
it("should use '.getBinding()' to read data from binding sources.", function() {
expect( modView.getBinding("firstName") ).to.equal( "Luke" );
});
it("should use '.setBinding()' to write data into binding sources.", function() {
var $el = $(".test-text-first");
modView.setBinding( "firstName", "Leia" );
expect( dataModel.get("firstName") ).to.equal( "Leia" );
expect( $el.text() ).to.equal( "Leia" );
});
it("should generate computed view properties based on '.computeds' hash.", function() {
expect( domView.getBinding("checkedCount") ).to.equal( "Checked items: 1" );
domView.viewModel.modifyArray("checkList", "push", "c");
expect( domView.getBinding("checkedCount") ).to.equal( "Checked items: 2" );
});
it("should allow view bindings to map through computed properties.", function() {
var $el = $(".test-view-computed");
expect( $el.text() ).to.equal( "Checked items: 1" );
domView.viewModel.modifyArray("checkList", "push", "c");
expect( $el.text() ).to.equal( "Checked items: 2" );
});
it("should inject manual dependency declarations as computed getter arguments.", function() {
expect( domView.getBinding("nameDisplay") ).to.equal( "<strong>Skywalker</strong>, Luke" );
});
it("should allow computed view properties to recieve and store data.", function() {
expect( domView.nameDisplaySetterValue ).to.be.undefined;
domView.setBinding("nameDisplay", "hello");
expect( domView.nameDisplaySetterValue ).to.equal( "hello" );
});
it("should allow multiple data sources and their namespaced attributes to be defined through 'bindingSources'.", function() {
var m1 = new Backbone.Model({name: "Luke"});
var m2 = new Backbone.Collection();
var m3 = new Backbone.Model({name: "Han"});
var m4 = new Backbone.Collection();
var v1, v2, v3, v4, v5, v6;
var sourceView = new (Backbone.Epoxy.View.extend({
el: "<div data-bind='b1:$model, b2:$collection, b3:$mod2, b4:$col2, b5:name, b6:mod2_name'></div>",
model: m1,
collection: m2,
bindingSources: {
mod2: m3,
col2: m4
},
bindingHandlers: {
b1: function( $el, value ) {
v1 = value;
},
b2: function( $el, value ) {
v2 = value;
},
b3: function( $el, value ) {
v3 = value;
},
b4: function( $el, value ) {
v4 = value;
},
b5: function( $el, value ) {
v5 = value;
},
b6: function( $el, value ) {
v6 = value;
}
}
}));
expect( v1 ).to.equal( m1 );
expect( v2 ).to.equal( m2 );
expect( v3 ).to.equal( m3 );
expect( v4 ).to.equal( m4 );
expect( v5 ).to.equal( "Luke" );
expect( v6 ).to.equal( "Han" );
});
it("binding 'attr:' should establish a one-way binding with an element's attribute definitions.", function() {
var $el = $(".test-attr-multi");
expect( $el.attr("href") ).to.equal( "b" );
expect( $el.attr("title") ).to.equal( "b" );
dataModel.set("preference", "c");
expect( $el.attr("href") ).to.equal( "c" );
expect( $el.attr("title") ).to.equal( "c" );
});
it("binding 'attr:' should allow string property definitions.", function() {
var $el = $(".test-attr");
expect( $el.attr("data-active") ).to.equal( "true" );
dataModel.set("active", false);
expect( $el.attr("data-active") ).to.equal( "false" );
});
it("binding 'checked:' should establish a two-way binding with a radio group.", function() {
var $a = $(".preference[value='a']");
var $b = $(".preference[value='b']");
expect( $a.prop("checked") ).to.equal( false );
expect( $b.prop("checked") ).to.equal( true );
$a.prop("checked", true).trigger("change");
expect( dataModel.get("preference") ).to.equal( "a" );
});
it("binding 'checked:' should establish a two-way binding between a checkbox and boolean value.", function() {
var $el = $(".test-checked-boolean");
expect( $el.prop("checked") ).to.equal( true );
$el.prop("checked", false).trigger("change");
expect( dataModel.get("active") ).to.equal( false );
});
it("binding 'checked:' should set a checkbox series based on a model array.", function() {
var $els = $(".check-list");
// Default: populate based on intial setting:
expect( !!$els.filter("[value='b']" ).prop("checked") ).to.equal( true );
expect( !!$els.filter("[value='c']" ).prop("checked") ).to.equal( false );
// Add new selection to the checkbox group:
viewModel.set("checkList", ["b", "c"]);
expect( !!$els.filter("[value='b']" ).prop("checked") ).to.equal( true );
expect( !!$els.filter("[value='c']" ).prop("checked") ).to.equal( true );
});
it("binding 'checked:' should respond to model changes performed by '.modifyArray'.", function() {
var $els = $(".check-list");
// Add new selection to the checkbox group:
expect( !!$els.filter("[value='b']" ).prop("checked") ).to.equal( true );
expect( !!$els.filter("[value='c']" ).prop("checked") ).to.equal( false );
viewModel.modifyArray("checkList", "push", "c");
expect( !!$els.filter("[value='b']" ).prop("checked") ).to.equal( true );
expect( !!$els.filter("[value='c']" ).prop("checked") ).to.equal( true );
});
it("binding 'checked:' should get a checkbox series formatted as a model array.", function() {
var $els = $(".check-list");
dataModel.set("checkList", ["b"]);
// Default: populate based on intial setting:
expect( !!$els.filter("[value='b']" ).prop("checked") ).to.equal( true );
$els.filter("[value='a']").prop("checked", true).trigger("change");
expect( viewModel.get("checkList").join(",") ).to.equal( "b,a" );
});
it("binding 'classes:' should establish a one-way binding with an element's class definitions.", function() {
var $el = $(".test-classes").eq(0);
expect( $el.hasClass("error") ).to.equal( false );
expect( $el.hasClass("active") ).to.equal( true );
dataModel.set({
firstName: "",
active: false
});
expect( $el.hasClass("error") ).to.equal( true );
expect( $el.hasClass("active") ).to.equal( false );
});
it("binding 'collection:' should update display in response Backbone.Collection 'reset' events.", function() {
var $el = $(".test-collection");
modView.collection.reset([
{name: "Luke Skywalker"}
]);
expect( $el.children().length ).to.equal( 1 );
modView.collection.reset([
{name: "Hans Solo"},
{name: "Chewy"}
]);
expect( $el.children().length ).to.equal( 2 );
});
it("binding 'collection:' should update display in response Backbone.Collection 'add' events.", function() {
var $el = $(".test-collection");
modView.collection.add({name: "Luke Skywalker"});
expect( $el.children().length ).to.equal( 1 );
modView.collection.add([
{name: "Hans Solo"},
{name: "Chewy"}
]);
expect( $el.children().length ).to.equal( 3 );
});
it("binding 'collection:' should update display in response Backbone.Collection 'remove' events.", function() {
var $el = $(".test-collection");
modView.collection.add({name: "Luke Skywalker"});
expect( $el.children().length ).to.equal( 1 );
modView.collection.remove( modView.collection.at(0) );
expect( $el.children().length ).to.equal( 0 );
});
it("binding 'collection:' should update display in response Backbone.Collection 'sort' events.", function() {
var $el = $(".test-collection");
modView.collection.reset([
{name: "B"},
{name: "A"}
]);
expect( $el.find(":first-child .name-dsp").text() ).to.equal( "B" );
modView.collection.comparator = function( model ) { return model.get("name"); };
modView.collection.sort();
modView.collection.comparator = null;
expect( $el.find(":first-child .name-dsp").text() ).to.equal( "A" );
});
it("binding 'collection:' child views should update display based on their model bindings.", function() {
var modViewWithInitialCollection = new (Backbone.Epoxy.View.extend({
collection: new TestCollection([{name: "A"}]),
itemView: CollectionView,
el: "<div data-bind='collection:$collection'></div>",
}));
expect( modViewWithInitialCollection.$el.find(":first-child .name-dsp").text() ).to.equal( "A" );
modViewWithInitialCollection.collection.first().set({name: "B"})
expect( modViewWithInitialCollection.$el.find(":first-child .name-dsp").text() ).to.equal( "B" );
});
it("binding 'css:' should establish a one-way binding with an element's css styles.", function() {
var $el = $(".test-css");
expect( $el.css("display") ).to.equal( "none" );
dataModel.set( "lastName", "" );
expect( $el.css("display") ).to.equal( "block" );
});
it("binding 'disabled:' should establish a one-way binding with an element's disabled state.", function() {
var $el = $(".test-disabled");
expect( $el.prop("disabled") ).to.be.true;
dataModel.set( "active", false );
expect( $el.prop("disabled") ).to.be.false;
});
it("binding 'enabled:' should establish a one-way binding with an element's inverted disabled state.", function() {
var $el = $(".test-enabled");
expect( $el.prop("disabled") ).to.be.false;
dataModel.set( "active", false );
expect( $el.prop("disabled") ).to.be.true;
});
it("binding 'events:' should configure additional DOM event triggers.", function() {
var $el = $(".test-input-first");
expect( $el.val() ).to.equal( "Luke" );
$el.val( "Anakin" ).trigger("keyup");
expect( dataModel.get("firstName") ).to.equal( "Anakin" );
});
it("binding 'html:' should establish a one-way binding with an element's html contents.", function() {
var $el = $(".test-html");
// Compare markup as case insensitive to accomodate variances in browser DOM styling:
expect( $el.html() ).to.match( /<strong>Skywalker<\/strong>, Luke/i );
dataModel.set("firstName", "Anakin");
expect( $el.html() ).to.match( /<strong>Skywalker<\/strong>, Anakin/i );
});
it("binding 'options:' should bind an array of strings to a select element's options.", function() {
var $el = $(".test-select");
viewModel.set("optionsList", ["Luke", "Leia"]);
expect( $el.children().length ).to.equal( 2 );
expect( $el.find(":first-child").attr("value") ).to.equal( "Luke" );
expect( $el.find(":first-child").text() ).to.equal( "Luke" );
});
it("binding 'options:' should bind an array of label/value pairs to a select element's options.", function() {
var $el = $(".test-select");
viewModel.set("optionsList", [
{label:"Luke", value:"a"},
{label:"Leia", value:"b"}
]);
expect( $el.children().length ).to.equal( 2 );
expect( $el.find(":first-child").attr("value") ).to.equal( "a" );
expect( $el.find(":first-child").text() ).to.equal( "Luke" );
});
it("binding 'options:' should bind a collection of model label/value attributes to a select element's options.", function() {
var $el = $(".test-select-collect");
modView.collection.reset([
{label:"Luke Skywalker", value:"Luke"},
{label:"Han Solo", value:"Han"}
]);
expect( $el.children().length ).to.equal( 2 );
expect( dataModel.get("valCollect") ).to.equal( "Luke" );
});
it("binding 'options:' should update selection when additional items are added/removed.", function() {
var $el = $(".test-select");
viewModel.modifyArray("optionsList", "push", {label:"Leia", value:"3"});
expect( $el.children().length ).to.equal( 4 );
expect( $el.find(":last-child").attr("value") ).to.equal( "3" );
expect( $el.find(":last-child").text() ).to.equal( "Leia" );
});
it("binding 'options:' should preserve previous selection state after binding.", function() {
var $el = $(".test-select");
viewModel.modifyArray("optionsList", "push", {label:"Leia", value:"3"});
expect( $el.children().length ).to.equal( 4 );
expect( $el.val() ).to.equal( "1" );
});
it("binding 'options:' should update the bound model value when the previous selection is no longer available.", function() {
var $el = $(".test-select-default");
expect( dataModel.get("valDefault") ).to.equal( "1" );
viewModel.set("optionsList", []);
expect( dataModel.get("valDefault") ).to.equal( "default" );
});
it("binding 'options:' should update a bound multiselect value when the previous selection is no longer available.", function() {
var $el = $(".test-select-multi");
// Set two options as selected, and confirm they appear within the view:
dataModel.set("valMulti", ["1", "2"]);
expect( $el.val().join(",") ).to.equal( "1,2" );
// Remove one option from the list, then confirm the model captures the revised selection: