| 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 template_binding.test.template_binding_test; | 5 library template_binding.test.template_binding_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 var div = createTestHtml( | 150 var div = createTestHtml( |
| 151 '<template bind="{{ bound }}" if="{{ predicate }}">' | 151 '<template bind="{{ bound }}" if="{{ predicate }}">' |
| 152 'value:{{ value }}' | 152 'value:{{ value }}' |
| 153 '</template>'); | 153 '</template>'); |
| 154 // Dart note: predicate changed from 0->null because 0 isn't falsey in Dart. | 154 // Dart note: predicate changed from 0->null because 0 isn't falsey in Dart. |
| 155 // See https://code.google.com/p/dart/issues/detail?id=11956 | 155 // See https://code.google.com/p/dart/issues/detail?id=11956 |
| 156 // Changed bound from null->1 since null is equivalent to JS undefined, | 156 // Changed bound from null->1 since null is equivalent to JS undefined, |
| 157 // and would cause the template to not be expanded. | 157 // and would cause the template to not be expanded. |
| 158 var m = toObservable({ 'predicate': null, 'bound': 1 }); | 158 var m = toObservable({ 'predicate': null, 'bound': 1 }); |
| 159 var template = div.firstChild; | 159 var template = div.firstChild; |
| 160 templateBind(template).model = m; | 160 bool errorSeen = false; |
| 161 runZoned(() { |
| 162 templateBind(template).model = m; |
| 163 }, onError: (e, s) { |
| 164 expect(e, isNoSuchMethodError); |
| 165 errorSeen = true; |
| 166 }); |
| 161 return new Future(() { | 167 return new Future(() { |
| 162 expect(div.nodes.length, 1); | 168 expect(div.nodes.length, 1); |
| 163 | 169 |
| 164 m['predicate'] = 1; | 170 m['predicate'] = 1; |
| 165 | 171 |
| 166 }).then(endOfMicrotask).then((_) { | 172 expect(errorSeen, isFalse); |
| 167 expect(div.nodes.length, 2); | 173 }).then(nextMicrotask).then((_) { |
| 168 expect(div.lastChild.text, 'value:'); | 174 expect(errorSeen, isTrue); |
| 175 expect(div.nodes.length, 1); |
| 169 | 176 |
| 170 m['bound'] = toObservable({ 'value': 2 }); | 177 m['bound'] = toObservable({ 'value': 2 }); |
| 171 | 178 |
| 172 }).then(endOfMicrotask).then((_) { | 179 }).then(endOfMicrotask).then((_) { |
| 173 expect(div.nodes.length, 2); | 180 expect(div.nodes.length, 2); |
| 174 expect(div.lastChild.text, 'value:2'); | 181 expect(div.lastChild.text, 'value:2'); |
| 175 | 182 |
| 176 m['bound']['value'] = 3; | 183 m['bound']['value'] = 3; |
| 177 | 184 |
| 178 }).then(endOfMicrotask).then((_) { | 185 }).then(endOfMicrotask).then((_) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 test('Bind oneTime-If - predicate true', () { | 234 test('Bind oneTime-If - predicate true', () { |
| 228 var div = createTestHtml( | 235 var div = createTestHtml( |
| 229 '<template bind="{{ bound }}" if="[[ predicate ]]">' | 236 '<template bind="{{ bound }}" if="[[ predicate ]]">' |
| 230 'value:{{ value }}' | 237 'value:{{ value }}' |
| 231 '</template>'); | 238 '</template>'); |
| 232 | 239 |
| 233 // Dart note: changed bound from null->1 since null is equivalent to JS | 240 // Dart note: changed bound from null->1 since null is equivalent to JS |
| 234 // undefined, and would cause the template to not be expanded. | 241 // undefined, and would cause the template to not be expanded. |
| 235 var m = toObservable({ 'predicate': 1, 'bound': 1 }); | 242 var m = toObservable({ 'predicate': 1, 'bound': 1 }); |
| 236 var template = div.firstChild; | 243 var template = div.firstChild; |
| 237 templateBind(template).model = m; | 244 bool errorSeen = false; |
| 245 runZoned(() { |
| 246 templateBind(template).model = m; |
| 247 }, onError: (e, s) { |
| 248 expect(e, isNoSuchMethodError); |
| 249 errorSeen = true; |
| 250 }); |
| 238 | 251 |
| 239 return new Future(() { | 252 return new Future(() { |
| 240 expect(div.nodes.length, 2); | 253 expect(div.nodes.length, 1); |
| 241 expect(div.lastChild.text, 'value:'); | |
| 242 | |
| 243 m['bound'] = toObservable({ 'value': 2 }); | 254 m['bound'] = toObservable({ 'value': 2 }); |
| 244 | 255 expect(errorSeen, isTrue); |
| 245 }).then(endOfMicrotask).then((_) { | 256 }).then(endOfMicrotask).then((_) { |
| 246 expect(div.nodes.length, 2); | 257 expect(div.nodes.length, 2); |
| 247 expect(div.lastChild.text, 'value:2'); | 258 expect(div.lastChild.text, 'value:2'); |
| 248 | 259 |
| 249 m['bound']['value'] = 3; | 260 m['bound']['value'] = 3; |
| 250 | 261 |
| 251 }).then(endOfMicrotask).then((_) { | 262 }).then(endOfMicrotask).then((_) { |
| 252 expect(div.nodes.length, 2); | 263 expect(div.nodes.length, 2); |
| 253 expect(div.lastChild.text, 'value:3'); | 264 expect(div.lastChild.text, 'value:3'); |
| 254 | 265 |
| (...skipping 1937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2192 var instance = outer.createInstance(model, new TestBindingSyntax()); | 2203 var instance = outer.createInstance(model, new TestBindingSyntax()); |
| 2193 expect(outer.content.nodes.first, | 2204 expect(outer.content.nodes.first, |
| 2194 templateBind(instance.nodes.first).ref); | 2205 templateBind(instance.nodes.first).ref); |
| 2195 | 2206 |
| 2196 host.append(instance); | 2207 host.append(instance); |
| 2197 return new Future(() { | 2208 return new Future(() { |
| 2198 expect(host.firstChild.nextNode.text, 'bar:replaced'); | 2209 expect(host.firstChild.nextNode.text, 'bar:replaced'); |
| 2199 }); | 2210 }); |
| 2200 }); | 2211 }); |
| 2201 | 2212 |
| 2213 test('CreateInstance - sync error', () { |
| 2214 var div = createTestHtml('<template>{{foo}}</template>'); |
| 2215 var outer = templateBind(div.nodes.first); |
| 2216 var model = 1; // model is missing 'foo' should throw. |
| 2217 expect(() => outer.createInstance(model, new TestBindingSyntax()), |
| 2218 throwsA(isNoSuchMethodError)); |
| 2219 }); |
| 2220 |
| 2221 test('CreateInstance - async error', () { |
| 2222 var div = createTestHtml( |
| 2223 '<template>' |
| 2224 '<template bind="{{b}}">' |
| 2225 '{{ foo }}:{{ replaceme }}' |
| 2226 '</template>' |
| 2227 '</template>'); |
| 2228 var outer = templateBind(div.nodes.first); |
| 2229 var model = toObservable({'b': 1}); // missing 'foo' should throw. |
| 2230 |
| 2231 bool seen = false; |
| 2232 runZoned(() => outer.createInstance(model, new TestBindingSyntax()), |
| 2233 onError: (e) { |
| 2234 expect(e, isNoSuchMethodError); |
| 2235 seen = true; |
| 2236 }); |
| 2237 return new Future(() { expect(seen, isTrue); }); |
| 2238 }); |
| 2239 |
| 2202 test('Repeat - svg', () { | 2240 test('Repeat - svg', () { |
| 2203 var div = createTestHtml( | 2241 var div = createTestHtml( |
| 2204 '<svg width="400" height="110">' | 2242 '<svg width="400" height="110">' |
| 2205 '<template repeat>' | 2243 '<template repeat>' |
| 2206 '<rect width="{{ width }}" height="{{ height }}" />' | 2244 '<rect width="{{ width }}" height="{{ height }}" />' |
| 2207 '</template>' | 2245 '</template>' |
| 2208 '</svg>'); | 2246 '</svg>'); |
| 2209 | 2247 |
| 2210 var model = toObservable([{ 'width': 10, 'height': 11 }, | 2248 var model = toObservable([{ 'width': 10, 'height': 11 }, |
| 2211 { 'width': 20, 'height': 21 }]); | 2249 { 'width': 20, 'height': 21 }]); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2395 } | 2433 } |
| 2396 } | 2434 } |
| 2397 | 2435 |
| 2398 class Issue18Syntax extends BindingDelegate { | 2436 class Issue18Syntax extends BindingDelegate { |
| 2399 prepareBinding(path, name, node) { | 2437 prepareBinding(path, name, node) { |
| 2400 if (name != 'class') return null; | 2438 if (name != 'class') return null; |
| 2401 | 2439 |
| 2402 return (model, _, oneTime) => new PathObserver(model, path); | 2440 return (model, _, oneTime) => new PathObserver(model, path); |
| 2403 } | 2441 } |
| 2404 } | 2442 } |
| OLD | NEW |