OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library node_bindings_test; | 5 library node_bindings_test; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'package:mdv/mdv.dart' as mdv; | 9 import 'package:mdv/mdv.dart' as mdv; |
10 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
(...skipping 20 matching lines...) Expand all Loading... |
31 }); | 31 }); |
32 | 32 |
33 tearDown(() { | 33 tearDown(() { |
34 testDiv.remove(); | 34 testDiv.remove(); |
35 testDiv = null; | 35 testDiv = null; |
36 }); | 36 }); |
37 | 37 |
38 observeTest('Text', () { | 38 observeTest('Text', () { |
39 var template = new Element.html('<template bind>{{a}} and {{b}}'); | 39 var template = new Element.html('<template bind>{{a}} and {{b}}'); |
40 testDiv.append(template); | 40 testDiv.append(template); |
41 var model = toSymbolMap({'a': 1, 'b': 2}); | 41 var model = toObservable({'a': 1, 'b': 2}); |
42 template.model = model; | 42 template.model = model; |
43 performMicrotaskCheckpoint(); | 43 performMicrotaskCheckpoint(); |
44 var text = testDiv.nodes[1]; | 44 var text = testDiv.nodes[1]; |
45 expect(text.text, '1 and 2'); | 45 expect(text.text, '1 and 2'); |
46 | 46 |
47 model[#a] = 3; | 47 model['a'] = 3; |
48 performMicrotaskCheckpoint(); | 48 performMicrotaskCheckpoint(); |
49 expect(text.text, '3 and 2'); | 49 expect(text.text, '3 and 2'); |
50 }); | 50 }); |
51 | 51 |
52 observeTest('SimpleBinding', () { | 52 observeTest('SimpleBinding', () { |
53 var el = new DivElement(); | 53 var el = new DivElement(); |
54 var model = toSymbolMap({'a': '1'}); | 54 var model = toObservable({'a': '1'}); |
55 el.bind('foo', model, 'a'); | 55 el.bind('foo', model, 'a'); |
56 performMicrotaskCheckpoint(); | 56 performMicrotaskCheckpoint(); |
57 expect(el.attributes['foo'], '1'); | 57 expect(el.attributes['foo'], '1'); |
58 | 58 |
59 model[#a] = '2'; | 59 model['a'] = '2'; |
60 performMicrotaskCheckpoint(); | 60 performMicrotaskCheckpoint(); |
61 expect(el.attributes['foo'], '2'); | 61 expect(el.attributes['foo'], '2'); |
62 | 62 |
63 model[#a] = 232.2; | 63 model['a'] = 232.2; |
64 performMicrotaskCheckpoint(); | 64 performMicrotaskCheckpoint(); |
65 expect(el.attributes['foo'], '232.2'); | 65 expect(el.attributes['foo'], '232.2'); |
66 | 66 |
67 model[#a] = 232; | 67 model['a'] = 232; |
68 performMicrotaskCheckpoint(); | 68 performMicrotaskCheckpoint(); |
69 expect(el.attributes['foo'], '232'); | 69 expect(el.attributes['foo'], '232'); |
70 | 70 |
71 model[#a] = null; | 71 model['a'] = null; |
72 performMicrotaskCheckpoint(); | 72 performMicrotaskCheckpoint(); |
73 expect(el.attributes['foo'], ''); | 73 expect(el.attributes['foo'], ''); |
74 }); | 74 }); |
75 | 75 |
76 observeTest('SimpleBindingWithDashes', () { | 76 observeTest('SimpleBindingWithDashes', () { |
77 var el = new DivElement(); | 77 var el = new DivElement(); |
78 var model = toSymbolMap({'a': '1'}); | 78 var model = toObservable({'a': '1'}); |
79 el.bind('foo-bar', model, 'a'); | 79 el.bind('foo-bar', model, 'a'); |
80 performMicrotaskCheckpoint(); | 80 performMicrotaskCheckpoint(); |
81 expect(el.attributes['foo-bar'], '1'); | 81 expect(el.attributes['foo-bar'], '1'); |
82 | 82 |
83 model[#a] = '2'; | 83 model['a'] = '2'; |
84 performMicrotaskCheckpoint(); | 84 performMicrotaskCheckpoint(); |
85 expect(el.attributes['foo-bar'], '2'); | 85 expect(el.attributes['foo-bar'], '2'); |
86 }); | 86 }); |
87 | 87 |
88 observeTest('SimpleBindingWithComment', () { | 88 observeTest('SimpleBindingWithComment', () { |
89 var el = new DivElement(); | 89 var el = new DivElement(); |
90 el.innerHtml = '<!-- Comment -->'; | 90 el.innerHtml = '<!-- Comment -->'; |
91 var model = toSymbolMap({'a': '1'}); | 91 var model = toObservable({'a': '1'}); |
92 el.bind('foo-bar', model, 'a'); | 92 el.bind('foo-bar', model, 'a'); |
93 performMicrotaskCheckpoint(); | 93 performMicrotaskCheckpoint(); |
94 expect(el.attributes['foo-bar'], '1'); | 94 expect(el.attributes['foo-bar'], '1'); |
95 | 95 |
96 model[#a] = '2'; | 96 model['a'] = '2'; |
97 performMicrotaskCheckpoint(); | 97 performMicrotaskCheckpoint(); |
98 expect(el.attributes['foo-bar'], '2'); | 98 expect(el.attributes['foo-bar'], '2'); |
99 }); | 99 }); |
100 | 100 |
101 observeTest('PlaceHolderBindingText', () { | 101 observeTest('PlaceHolderBindingText', () { |
102 var model = toSymbolMap({ | 102 var model = toObservable({ |
103 'adj': 'cruel', | 103 'adj': 'cruel', |
104 'noun': 'world' | 104 'noun': 'world' |
105 }); | 105 }); |
106 | 106 |
107 var el = new DivElement(); | 107 var el = new DivElement(); |
108 el.text = 'dummy'; | 108 el.text = 'dummy'; |
109 el.nodes.first.text = 'Hello {{ adj }} {{noun}}!'; | 109 el.nodes.first.text = 'Hello {{ adj }} {{noun}}!'; |
110 var template = new Element.html('<template bind>'); | 110 var template = new Element.html('<template bind>'); |
111 template.content.append(el); | 111 template.content.append(el); |
112 testDiv.append(template); | 112 testDiv.append(template); |
113 template.model = model; | 113 template.model = model; |
114 | 114 |
115 performMicrotaskCheckpoint(); | 115 performMicrotaskCheckpoint(); |
116 el = testDiv.nodes[1].nodes.first; | 116 el = testDiv.nodes[1].nodes.first; |
117 expect(el.text, 'Hello cruel world!'); | 117 expect(el.text, 'Hello cruel world!'); |
118 | 118 |
119 model[#adj] = 'happy'; | 119 model['adj'] = 'happy'; |
120 performMicrotaskCheckpoint(); | 120 performMicrotaskCheckpoint(); |
121 expect(el.text, 'Hello happy world!'); | 121 expect(el.text, 'Hello happy world!'); |
122 }); | 122 }); |
123 | 123 |
124 observeTest('InputElementTextBinding', () { | 124 observeTest('InputElementTextBinding', () { |
125 var model = toSymbolMap({'val': 'ping'}); | 125 var model = toObservable({'val': 'ping'}); |
126 | 126 |
127 var el = new InputElement(); | 127 var el = new InputElement(); |
128 el.bind('value', model, 'val'); | 128 el.bind('value', model, 'val'); |
129 performMicrotaskCheckpoint(); | 129 performMicrotaskCheckpoint(); |
130 expect(el.value, 'ping'); | 130 expect(el.value, 'ping'); |
131 | 131 |
132 el.value = 'pong'; | 132 el.value = 'pong'; |
133 dispatchEvent('input', el); | 133 dispatchEvent('input', el); |
134 expect(model[#val], 'pong'); | 134 expect(model['val'], 'pong'); |
135 | 135 |
136 // Try a deep path. | 136 // Try a deep path. |
137 model = toSymbolMap({'a': {'b': {'c': 'ping'}}}); | 137 model = toObservable({'a': {'b': {'c': 'ping'}}}); |
138 | 138 |
139 el.bind('value', model, 'a.b.c'); | 139 el.bind('value', model, 'a.b.c'); |
140 performMicrotaskCheckpoint(); | 140 performMicrotaskCheckpoint(); |
141 expect(el.value, 'ping'); | 141 expect(el.value, 'ping'); |
142 | 142 |
143 el.value = 'pong'; | 143 el.value = 'pong'; |
144 dispatchEvent('input', el); | 144 dispatchEvent('input', el); |
145 expect(observePath(model, 'a.b.c').value, 'pong'); | 145 expect(observePath(model, 'a.b.c').value, 'pong'); |
146 | 146 |
147 // Start with the model property being absent. | 147 // Start with the model property being absent. |
148 model[#a][#b].remove(#c); | 148 model['a']['b'].remove('c'); |
149 performMicrotaskCheckpoint(); | 149 performMicrotaskCheckpoint(); |
150 expect(el.value, ''); | 150 expect(el.value, ''); |
151 | 151 |
152 el.value = 'pong'; | 152 el.value = 'pong'; |
153 dispatchEvent('input', el); | 153 dispatchEvent('input', el); |
154 expect(observePath(model, 'a.b.c').value, 'pong'); | 154 expect(observePath(model, 'a.b.c').value, 'pong'); |
155 performMicrotaskCheckpoint(); | 155 performMicrotaskCheckpoint(); |
156 | 156 |
157 // Model property unreachable (and unsettable). | 157 // Model property unreachable (and unsettable). |
158 model[#a].remove(#b); | 158 model['a'].remove('b'); |
159 performMicrotaskCheckpoint(); | 159 performMicrotaskCheckpoint(); |
160 expect(el.value, ''); | 160 expect(el.value, ''); |
161 | 161 |
162 el.value = 'pong'; | 162 el.value = 'pong'; |
163 dispatchEvent('input', el); | 163 dispatchEvent('input', el); |
164 expect(observePath(model, 'a.b.c').value, null); | 164 expect(observePath(model, 'a.b.c').value, null); |
165 }); | 165 }); |
166 | 166 |
167 observeTest('InputElementCheckbox', () { | 167 observeTest('InputElementCheckbox', () { |
168 var model = toSymbolMap({'val': true}); | 168 var model = toObservable({'val': true}); |
169 | 169 |
170 var el = new InputElement(); | 170 var el = new InputElement(); |
171 testDiv.append(el); | 171 testDiv.append(el); |
172 el.type = 'checkbox'; | 172 el.type = 'checkbox'; |
173 el.bind('checked', model, 'val'); | 173 el.bind('checked', model, 'val'); |
174 performMicrotaskCheckpoint(); | 174 performMicrotaskCheckpoint(); |
175 expect(el.checked, true); | 175 expect(el.checked, true); |
176 | 176 |
177 model[#val] = false; | 177 model['val'] = false; |
178 performMicrotaskCheckpoint(); | 178 performMicrotaskCheckpoint(); |
179 expect(el.checked, false); | 179 expect(el.checked, false); |
180 | 180 |
181 el.click(); | 181 el.click(); |
182 expect(model[#val], true); | 182 expect(model['val'], true); |
183 | 183 |
184 el.click(); | 184 el.click(); |
185 expect(model[#val], false); | 185 expect(model['val'], false); |
186 | 186 |
187 el.onClick.listen((_) { | 187 el.onClick.listen((_) { |
188 expect(model[#val], true); | 188 expect(model['val'], true); |
189 }); | 189 }); |
190 el.onChange.listen((_) { | 190 el.onChange.listen((_) { |
191 expect(model[#val], true); | 191 expect(model['val'], true); |
192 }); | 192 }); |
193 | 193 |
194 el.dispatchEvent(new MouseEvent('click', view: window)); | 194 el.dispatchEvent(new MouseEvent('click', view: window)); |
195 }); | 195 }); |
196 | 196 |
197 observeTest('InputElementCheckbox - binding updated on click', () { | 197 observeTest('InputElementCheckbox - binding updated on click', () { |
198 var model = toSymbolMap({'val': true}); | 198 var model = toObservable({'val': true}); |
199 | 199 |
200 var el = new InputElement(); | 200 var el = new InputElement(); |
201 testDiv.append(el); | 201 testDiv.append(el); |
202 el.type = 'checkbox'; | 202 el.type = 'checkbox'; |
203 el.bind('checked', model, 'val'); | 203 el.bind('checked', model, 'val'); |
204 performMicrotaskCheckpoint(); | 204 performMicrotaskCheckpoint(); |
205 expect(el.checked, true); | 205 expect(el.checked, true); |
206 | 206 |
207 el.onClick.listen((_) { | 207 el.onClick.listen((_) { |
208 expect(model[#val], false); | 208 expect(model['val'], false); |
209 }); | 209 }); |
210 | 210 |
211 el.dispatchEvent(new MouseEvent('click', view: window)); | 211 el.dispatchEvent(new MouseEvent('click', view: window)); |
212 }); | 212 }); |
213 | 213 |
214 observeTest('InputElementCheckbox - binding updated on change', () { | 214 observeTest('InputElementCheckbox - binding updated on change', () { |
215 var model = toSymbolMap({'val': true}); | 215 var model = toObservable({'val': true}); |
216 | 216 |
217 var el = new InputElement(); | 217 var el = new InputElement(); |
218 testDiv.append(el); | 218 testDiv.append(el); |
219 el.type = 'checkbox'; | 219 el.type = 'checkbox'; |
220 el.bind('checked', model, 'val'); | 220 el.bind('checked', model, 'val'); |
221 performMicrotaskCheckpoint(); | 221 performMicrotaskCheckpoint(); |
222 expect(el.checked, true); | 222 expect(el.checked, true); |
223 | 223 |
224 el.onChange.listen((_) { | 224 el.onChange.listen((_) { |
225 expect(model[#val], false); | 225 expect(model['val'], false); |
226 }); | 226 }); |
227 | 227 |
228 el.dispatchEvent(new MouseEvent('click', view: window)); | 228 el.dispatchEvent(new MouseEvent('click', view: window)); |
229 }); | 229 }); |
230 | 230 |
231 observeTest('InputElementRadio', () { | 231 observeTest('InputElementRadio', () { |
232 var model = toSymbolMap({'val1': true, 'val2': false, 'val3': false, | 232 var model = toObservable({'val1': true, 'val2': false, 'val3': false, |
233 'val4': true}); | 233 'val4': true}); |
234 var RADIO_GROUP_NAME = 'observeTest'; | 234 var RADIO_GROUP_NAME = 'observeTest'; |
235 | 235 |
236 var container = testDiv; | 236 var container = testDiv; |
237 | 237 |
238 var el1 = new InputElement(); | 238 var el1 = new InputElement(); |
239 testDiv.append(el1); | 239 testDiv.append(el1); |
240 el1.type = 'radio'; | 240 el1.type = 'radio'; |
241 el1.name = RADIO_GROUP_NAME; | 241 el1.name = RADIO_GROUP_NAME; |
242 el1.bind('checked', model, 'val1'); | 242 el1.bind('checked', model, 'val1'); |
(...skipping 15 matching lines...) Expand all Loading... |
258 el4.type = 'radio'; | 258 el4.type = 'radio'; |
259 el4.name = 'othergroup'; | 259 el4.name = 'othergroup'; |
260 el4.bind('checked', model, 'val4'); | 260 el4.bind('checked', model, 'val4'); |
261 | 261 |
262 performMicrotaskCheckpoint(); | 262 performMicrotaskCheckpoint(); |
263 expect(el1.checked, true); | 263 expect(el1.checked, true); |
264 expect(el2.checked, false); | 264 expect(el2.checked, false); |
265 expect(el3.checked, false); | 265 expect(el3.checked, false); |
266 expect(el4.checked, true); | 266 expect(el4.checked, true); |
267 | 267 |
268 model[#val1] = false; | 268 model['val1'] = false; |
269 model[#val2] = true; | 269 model['val2'] = true; |
270 performMicrotaskCheckpoint(); | 270 performMicrotaskCheckpoint(); |
271 expect(el1.checked, false); | 271 expect(el1.checked, false); |
272 expect(el2.checked, true); | 272 expect(el2.checked, true); |
273 expect(el3.checked, false); | 273 expect(el3.checked, false); |
274 expect(el4.checked, true); | 274 expect(el4.checked, true); |
275 | 275 |
276 el1.checked = true; | 276 el1.checked = true; |
277 dispatchEvent('change', el1); | 277 dispatchEvent('change', el1); |
278 expect(model[#val1], true); | 278 expect(model['val1'], true); |
279 expect(model[#val2], false); | 279 expect(model['val2'], false); |
280 expect(model[#val3], false); | 280 expect(model['val3'], false); |
281 expect(model[#val4], true); | 281 expect(model['val4'], true); |
282 | 282 |
283 el3.checked = true; | 283 el3.checked = true; |
284 dispatchEvent('change', el3); | 284 dispatchEvent('change', el3); |
285 expect(model[#val1], false); | 285 expect(model['val1'], false); |
286 expect(model[#val2], false); | 286 expect(model['val2'], false); |
287 expect(model[#val3], true); | 287 expect(model['val3'], true); |
288 expect(model[#val4], true); | 288 expect(model['val4'], true); |
289 }); | 289 }); |
290 | 290 |
291 observeTest('InputElementRadioMultipleForms', () { | 291 observeTest('InputElementRadioMultipleForms', () { |
292 var model = toSymbolMap({'val1': true, 'val2': false, 'val3': false, | 292 var model = toObservable({'val1': true, 'val2': false, 'val3': false, |
293 'val4': true}); | 293 'val4': true}); |
294 var RADIO_GROUP_NAME = 'observeTest'; | 294 var RADIO_GROUP_NAME = 'observeTest'; |
295 | 295 |
296 var form1 = new FormElement(); | 296 var form1 = new FormElement(); |
297 testDiv.append(form1); | 297 testDiv.append(form1); |
298 var form2 = new FormElement(); | 298 var form2 = new FormElement(); |
299 testDiv.append(form2); | 299 testDiv.append(form2); |
300 | 300 |
301 var el1 = new InputElement(); | 301 var el1 = new InputElement(); |
302 form1.append(el1); | 302 form1.append(el1); |
(...skipping 20 matching lines...) Expand all Loading... |
323 el4.bind('checked', model, 'val4'); | 323 el4.bind('checked', model, 'val4'); |
324 | 324 |
325 performMicrotaskCheckpoint(); | 325 performMicrotaskCheckpoint(); |
326 expect(el1.checked, true); | 326 expect(el1.checked, true); |
327 expect(el2.checked, false); | 327 expect(el2.checked, false); |
328 expect(el3.checked, false); | 328 expect(el3.checked, false); |
329 expect(el4.checked, true); | 329 expect(el4.checked, true); |
330 | 330 |
331 el2.checked = true; | 331 el2.checked = true; |
332 dispatchEvent('change', el2); | 332 dispatchEvent('change', el2); |
333 expect(model[#val1], false); | 333 expect(model['val1'], false); |
334 expect(model[#val2], true); | 334 expect(model['val2'], true); |
335 | 335 |
336 // Radio buttons in form2 should be unaffected | 336 // Radio buttons in form2 should be unaffected |
337 expect(model[#val3], false); | 337 expect(model['val3'], false); |
338 expect(model[#val4], true); | 338 expect(model['val4'], true); |
339 | 339 |
340 el3.checked = true; | 340 el3.checked = true; |
341 dispatchEvent('change', el3); | 341 dispatchEvent('change', el3); |
342 expect(model[#val3], true); | 342 expect(model['val3'], true); |
343 expect(model[#val4], false); | 343 expect(model['val4'], false); |
344 | 344 |
345 // Radio buttons in form1 should be unaffected | 345 // Radio buttons in form1 should be unaffected |
346 expect(model[#val1], false); | 346 expect(model['val1'], false); |
347 expect(model[#val2], true); | 347 expect(model['val2'], true); |
348 }); | 348 }); |
349 | 349 |
350 observeTest('BindToChecked', () { | 350 observeTest('BindToChecked', () { |
351 var div = new DivElement(); | 351 var div = new DivElement(); |
352 testDiv.append(div); | 352 testDiv.append(div); |
353 var child = new DivElement(); | 353 var child = new DivElement(); |
354 div.append(child); | 354 div.append(child); |
355 var input = new InputElement(); | 355 var input = new InputElement(); |
356 child.append(input); | 356 child.append(input); |
357 input.type = 'checkbox'; | 357 input.type = 'checkbox'; |
358 | 358 |
359 var model = toSymbolMap({'a': {'b': false}}); | 359 var model = toObservable({'a': {'b': false}}); |
360 input.bind('checked', model, 'a.b'); | 360 input.bind('checked', model, 'a.b'); |
361 | 361 |
362 input.click(); | 362 input.click(); |
363 expect(model[#a][#b], true); | 363 expect(model['a']['b'], true); |
364 | 364 |
365 input.click(); | 365 input.click(); |
366 expect(model[#a][#b], false); | 366 expect(model['a']['b'], false); |
367 }); | 367 }); |
368 | 368 |
369 observeTest('Select selectedIndex', () { | 369 observeTest('Select selectedIndex', () { |
370 var select = new SelectElement(); | 370 var select = new SelectElement(); |
371 testDiv.append(select); | 371 testDiv.append(select); |
372 var option0 = select.append(new OptionElement()); | 372 var option0 = select.append(new OptionElement()); |
373 var option1 = select.append(new OptionElement()); | 373 var option1 = select.append(new OptionElement()); |
374 var option2 = select.append(new OptionElement()); | 374 var option2 = select.append(new OptionElement()); |
375 | 375 |
376 var model = toSymbolMap({'val': 2}); | 376 var model = toObservable({'val': 2}); |
377 | 377 |
378 select.bind('selectedIndex', model, 'val'); | 378 select.bind('selectedIndex', model, 'val'); |
379 performMicrotaskCheckpoint(); | 379 performMicrotaskCheckpoint(); |
380 expect(select.selectedIndex, 2); | 380 expect(select.selectedIndex, 2); |
381 | 381 |
382 select.selectedIndex = 1; | 382 select.selectedIndex = 1; |
383 dispatchEvent('change', select); | 383 dispatchEvent('change', select); |
384 expect(model[#val], 1); | 384 expect(model['val'], 1); |
385 }); | 385 }); |
386 | 386 |
387 observeTest('MultipleReferences', () { | 387 observeTest('MultipleReferences', () { |
388 var el = new DivElement(); | 388 var el = new DivElement(); |
389 var template = new Element.html('<template bind>'); | 389 var template = new Element.html('<template bind>'); |
390 template.content.append(el); | 390 template.content.append(el); |
391 testDiv.append(template); | 391 testDiv.append(template); |
392 | 392 |
393 var model = toSymbolMap({'foo': 'bar'}); | 393 var model = toObservable({'foo': 'bar'}); |
394 el.attributes['foo'] = '{{foo}} {{foo}}'; | 394 el.attributes['foo'] = '{{foo}} {{foo}}'; |
395 template.model = model; | 395 template.model = model; |
396 | 396 |
397 performMicrotaskCheckpoint(); | 397 performMicrotaskCheckpoint(); |
398 el = testDiv.nodes[1]; | 398 el = testDiv.nodes[1]; |
399 expect(el.attributes['foo'], 'bar bar'); | 399 expect(el.attributes['foo'], 'bar bar'); |
400 }); | 400 }); |
401 } | 401 } |
OLD | NEW |