| 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 bindings_test; | 5 library bindings_test; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 | 9 |
| 10 import 'package:logging/logging.dart'; | |
| 11 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
| 12 import 'package:observe/mirrors_used.dart'; // make test smaller. | 11 import 'package:observe/mirrors_used.dart'; // make test smaller. |
| 13 import 'package:observe/src/dirty_check.dart' show dirtyCheckZone; | 12 import 'package:observe/src/dirty_check.dart' show dirtyCheckZone; |
| 14 import 'package:polymer_expressions/polymer_expressions.dart'; | 13 import 'package:polymer_expressions/polymer_expressions.dart'; |
| 15 import 'package:template_binding/template_binding.dart' show templateBind; | 14 import 'package:template_binding/template_binding.dart' show templateBind; |
| 16 import 'package:unittest/html_config.dart'; | 15 import 'package:unittest/html_config.dart'; |
| 17 import 'package:unittest/unittest.dart'; | 16 import 'package:unittest/unittest.dart'; |
| 18 | 17 |
| 19 main() => dirtyCheckZone().run(() { | 18 main() => dirtyCheckZone().run(() { |
| 20 useHtmlConfiguration(); | 19 useHtmlConfiguration(); |
| 21 | 20 |
| 22 group('bindings', () { | 21 group('bindings', () { |
| 23 var stop = null; | 22 var stop = null; |
| 24 var messages = []; | |
| 25 var testDiv; | 23 var testDiv; |
| 26 setUp(() { | 24 setUp(() { |
| 27 stop = Logger.root.onRecord.listen((r) => messages.add(r)); | |
| 28 document.body.append(testDiv = new DivElement()); | 25 document.body.append(testDiv = new DivElement()); |
| 29 }); | 26 }); |
| 30 | 27 |
| 31 tearDown(() { | 28 tearDown(() { |
| 32 testDiv.remove(); | 29 testDiv.remove(); |
| 33 testDiv = null; | 30 testDiv = null; |
| 34 stop.cancel(); | |
| 35 stop = null; | |
| 36 messages = []; | |
| 37 }); | 31 }); |
| 38 | 32 |
| 39 test('should update binding when data changes', () { | 33 test('should update binding when data changes', () { |
| 40 var model = new NotifyModel(); | 34 var model = new NotifyModel(); |
| 41 var binding = new PolymerExpressions() | 35 var binding = new PolymerExpressions() |
| 42 .prepareBinding('x', null, null)(model, null, false); | 36 .prepareBinding('x', null, null)(model, null, false); |
| 43 expect(binding.value, isNull); | 37 expect(binding.value, isNull); |
| 44 model.x = "hi"; | 38 model.x = "hi"; |
| 45 return new Future(() { | 39 return new Future(() { |
| 46 expect(binding.value, 'hi'); | 40 expect(binding.value, 'hi'); |
| 47 expect(messages.length, 0); | |
| 48 }); | 41 }); |
| 49 }); | 42 }); |
| 50 | 43 |
| 51 test('should update text content when data changes', () { | 44 test('should update text content when data changes', () { |
| 52 var model = new NotifyModel('abcde'); | 45 var model = new NotifyModel('abcde'); |
| 53 var template = templateBind(new Element.html( | 46 var template = templateBind(new Element.html( |
| 54 '<template><span>{{x}}</span></template>')); | 47 '<template><span>{{x}}</span></template>')); |
| 55 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 48 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
| 56 | 49 |
| 57 return new Future(() { | 50 return new Future(() { |
| 58 var el = testDiv.query("span"); | 51 var el = testDiv.query("span"); |
| 59 expect(el.text, 'abcde'); | 52 expect(el.text, 'abcde'); |
| 60 expect(model.x, 'abcde'); | 53 expect(model.x, 'abcde'); |
| 61 model.x = '___'; | 54 model.x = '___'; |
| 62 | 55 |
| 63 return new Future(() { | 56 return new Future(() { |
| 64 expect(model.x, '___'); | 57 expect(model.x, '___'); |
| 65 expect(el.text, '___'); | 58 expect(el.text, '___'); |
| 66 }); | 59 }); |
| 67 }); | 60 }); |
| 68 }); | 61 }); |
| 69 | 62 |
| 70 test('should log eval exceptions', () { | 63 test('should log eval exceptions', () { |
| 71 var model = new NotifyModel('abcde'); | 64 var model = new NotifyModel('abcde'); |
| 72 var template = templateBind(new Element.html( | 65 var completer = new Completer(); |
| 73 '<template><span>{{foo}}</span></template>')); | 66 runZoned(() { |
| 74 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 67 var template = templateBind(new Element.html( |
| 68 '<template><span>{{foo}}</span></template>')); |
| 69 testDiv.append(template.createInstance(model, |
| 70 new PolymerExpressions())); |
| 75 | 71 |
| 76 return new Future(() { | 72 return new Future(() {}); |
| 77 expect(messages.length, 1); | 73 }, onError: (e) { |
| 78 expect(messages[0].message, | 74 expect('$e', startsWith("Error evaluating expression 'foo':")); |
| 79 "Error evaluating expression 'foo': variable 'foo' not found"); | 75 completer.complete(true); |
| 80 }); | 76 }); |
| 77 return completer.future; |
| 81 }); | 78 }); |
| 82 | 79 |
| 83 test('should preserve the cursor position', () { | 80 test('should preserve the cursor position', () { |
| 84 var model = new NotifyModel('abcde'); | 81 var model = new NotifyModel('abcde'); |
| 85 var template = templateBind(new Element.html( | 82 var template = templateBind(new Element.html( |
| 86 '<template><input id="i1" value={{x}}></template>')); | 83 '<template><input id="i1" value={{x}}></template>')); |
| 87 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 84 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
| 88 | 85 |
| 89 return new Future(() { | 86 return new Future(() { |
| 90 var el = testDiv.query("#i1"); | 87 var el = testDiv.query("#i1"); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 @reflectable | 124 @reflectable |
| 128 class NotifyModel extends ChangeNotifier { | 125 class NotifyModel extends ChangeNotifier { |
| 129 var _x; | 126 var _x; |
| 130 NotifyModel([this._x]); | 127 NotifyModel([this._x]); |
| 131 | 128 |
| 132 get x => _x; | 129 get x => _x; |
| 133 set x(value) { | 130 set x(value) { |
| 134 _x = notifyPropertyChange(#x, _x, value); | 131 _x = notifyPropertyChange(#x, _x, value); |
| 135 } | 132 } |
| 136 } | 133 } |
| OLD | NEW |