1
1
describe ( "Rivets.binders" , function ( ) {
2
- var context
2
+ var context ;
3
3
4
4
beforeEach ( function ( ) {
5
5
context = {
6
6
publish : function ( ) { }
7
7
}
8
- } )
8
+ } ) ;
9
9
10
10
describe ( "value" , function ( ) {
11
- var el
11
+ var el ;
12
12
13
13
beforeEach ( function ( ) {
14
14
el = document . createElement ( 'input' )
15
- } )
15
+ } ) ;
16
16
17
17
it ( "unbinds the same bound function" , function ( ) {
18
- var boundFn
18
+ var boundFn ;
19
19
20
20
sinon . stub ( el , 'addEventListener' , function ( event , fn ) {
21
21
boundFn = fn
22
- } )
22
+ } ) ;
23
23
24
- rivets . binders . value . bind . call ( context , el )
24
+ rivets . binders . value . bind . call ( context , el ) ;
25
25
26
26
sinon . stub ( el , 'removeEventListener' , function ( event , fn ) {
27
27
fn . should . equal ( boundFn )
28
- } )
28
+ } ) ;
29
29
30
30
rivets . binders . value . unbind . call ( context , el )
31
31
} )
32
- } )
32
+ } ) ;
33
33
34
34
describe ( "each-*" , function ( ) {
35
35
var fragment ;
@@ -56,34 +56,22 @@ describe("Rivets.binders", function() {
56
56
57
57
it ( "reflects changes to the model into the DOM" , function ( ) {
58
58
var view = rivets . bind ( fragment , model ) ;
59
- Should ( fragment . childNodes [ 1 ] . innerText ) . be . exactly ( "0" ) ;
59
+ Should ( fragment . childNodes [ 1 ] . textContent ) . be . exactly ( "0" ) ;
60
60
61
61
model . items [ 0 ] . val = "howdy" ;
62
- Should ( fragment . childNodes [ 1 ] . innerText ) . be . exactly ( "howdy" ) ;
62
+ Should ( fragment . childNodes [ 1 ] . textContent ) . be . exactly ( "howdy" ) ;
63
63
} ) ;
64
64
65
65
it ( "reflects changes to the model into the DOM after unbind/bind" , function ( ) {
66
66
var view = rivets . bind ( fragment , model ) ;
67
- Should ( fragment . childNodes [ 1 ] . innerText ) . be . exactly ( "0" ) ;
67
+ Should ( fragment . childNodes [ 1 ] . textContent ) . be . exactly ( "0" ) ;
68
68
69
69
view . unbind ( ) ;
70
70
view . bind ( ) ;
71
71
model . items [ 0 ] . val = "howdy" ;
72
- Should ( fragment . childNodes [ 1 ] . innerText ) . be . exactly ( "howdy" ) ;
72
+ Should ( fragment . childNodes [ 1 ] . textContent ) . be . exactly ( "howdy" ) ;
73
73
} ) ;
74
74
75
- it ( "lets you pop an item" , function ( ) {
76
- var view = rivets . bind ( fragment , model ) ;
77
- var originalLength = model . items . length ;
78
-
79
- // one child for each element in the model plus 1 for the comment placeholder
80
- Should ( fragment . childNodes . length ) . be . exactly ( model . items . length + 1 ) ;
81
-
82
- model . items . pop ( ) ;
83
- Should ( model . items . length ) . be . exactly ( originalLength - 1 ) ;
84
- Should ( fragment . childNodes . length ) . be . exactly ( model . items . length + 1 ) ;
85
- } )
86
-
87
75
it ( "lets you push an item" , function ( ) {
88
76
var view = rivets . bind ( fragment , model ) ;
89
77
var originalLength = model . items . length ;
@@ -112,6 +100,34 @@ describe("Rivets.binders", function() {
112
100
} ) ;
113
101
} ) ;
114
102
103
+ describe ( "nested-each-*" , function ( ) {
104
+ var fragment ;
105
+ var el ;
106
+ var nestedEl ;
107
+ var model ;
108
+
109
+ beforeEach ( function ( ) {
110
+ fragment = document . createDocumentFragment ( ) ;
111
+ el = document . createElement ( "span" ) ;
112
+ el . setAttribute ( "rv-each-item" , "items" ) ;
113
+ nestedEl = document . createElement ( "span" ) ;
114
+ nestedEl . setAttribute ( "rv-each-nested" , "item.val" ) ;
115
+ nestedEl . textContent = "{%item%}-{%nested%}" ;
116
+ el . appendChild ( nestedEl ) ;
117
+ fragment . appendChild ( el ) ;
118
+
119
+ model = { items : [ { val : [ { val : 0 } , { val : 1 } ] } , { val : [ { val : 2 } , { val : 3 } ] } , { val : [ { val : 4 } , { val : 5 } ] } ] } ;
120
+ } ) ;
121
+
122
+ it . skip ( "lets you get all the indexes" , function ( ) {
123
+ var view = rivets . bind ( el , model ) ;
124
+
125
+ Should ( fragment . childNodes [ 1 ] . childNodes [ 1 ] . textContent ) . be . exactly ( '0-0' ) ;
126
+ Should ( fragment . childNodes [ 1 ] . childNodes [ 2 ] . textContent ) . be . exactly ( '0-1' ) ;
127
+ Should ( fragment . childNodes [ 2 ] . childNodes [ 2 ] . textContent ) . be . exactly ( '1-1' ) ;
128
+ } ) ;
129
+ } ) ;
130
+
115
131
describe ( "if" , function ( ) {
116
132
var fragment ;
117
133
var el ;
@@ -183,5 +199,49 @@ describe("Rivets.binders", function() {
183
199
// 1 for the comment placeholder
184
200
Should ( fragment . childNodes . length ) . be . exactly ( 1 ) ;
185
201
} ) ;
202
+
203
+ it ( "rebindes nested if" , function ( ) {
204
+ var nestedEl = document . createElement ( "div" ) ;
205
+ nestedEl . setAttribute ( "rv-if" , "data.showNested" ) ;
206
+ nestedEl . innerHTML = "{ data.countNested }" ;
207
+ el . appendChild ( nestedEl ) ;
208
+
209
+ var view = rivets . bind ( fragment , model ) ;
210
+
211
+ model . data . countNested = "1" ;
212
+ model . data . showNested = true ;
213
+ Should ( nestedEl . innerHTML ) . be . exactly ( "1" ) ;
214
+ model . data . show = false ;
215
+ model . data . show = true ;
216
+ model . data . countNested = "42" ;
217
+
218
+ Should ( nestedEl . innerHTML ) . be . exactly ( "42" ) ;
219
+ } ) ;
220
+ } ) ;
221
+
222
+ describe ( "Custom binder with no attribute value" , function ( ) {
223
+ var el , model ;
224
+ rivets . binders [ "custom-binder" ] = function ( el , value ) {
225
+ el . innerHTML = "received " + value ;
226
+ } ;
227
+ beforeEach ( function ( ) {
228
+ fragment = document . createDocumentFragment ( ) ;
229
+ el = document . createElement ( "div" ) ;
230
+
231
+ fragment . appendChild ( el ) ;
232
+
233
+ model = { } ;
234
+ } ) ;
235
+
236
+ it ( "receives undefined when html attribute is not specified" , function ( ) {
237
+ el . innerHTML = "<div rv-custom-binder></div>" ;
238
+ var view = rivets . bind ( fragment , model ) ;
239
+ Should ( el . children [ 0 ] . innerHTML ) . be . exactly ( 'received undefined' ) ;
240
+ } ) ;
241
+ it ( "receives undefined when html attribute is not specified" , function ( ) {
242
+ el . innerHTML = "<div rv-custom-binder=''></div>" ;
243
+ var view = rivets . bind ( fragment , model ) ;
244
+ Should ( el . children [ 0 ] . innerHTML ) . be . exactly ( 'received undefined' ) ;
245
+ } ) ;
186
246
} ) ;
187
247
} ) ;
0 commit comments