| 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:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 expect(binding.value, 'hi'); | 40 expect(binding.value, 'hi'); |
| 41 }); | 41 }); |
| 42 }); | 42 }); |
| 43 | 43 |
| 44 test('should update text content when data changes', () { | 44 test('should update text content when data changes', () { |
| 45 var model = new NotifyModel('abcde'); | 45 var model = new NotifyModel('abcde'); |
| 46 var template = templateBind(new Element.html( | 46 var template = templateBind(new Element.html( |
| 47 '<template><span>{{x}}</span></template>')); | 47 '<template><span>{{x}}</span></template>')); |
| 48 testDiv.append(template.createInstance(model, new PolymerExpressions())); | 48 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
| 49 | 49 |
| 50 var el; |
| 50 return new Future(() { | 51 return new Future(() { |
| 51 var el = testDiv.query("span"); | 52 el = testDiv.query("span"); |
| 52 expect(el.text, 'abcde'); | 53 expect(el.text, 'abcde'); |
| 53 expect(model.x, 'abcde'); | 54 expect(model.x, 'abcde'); |
| 54 model.x = '___'; | 55 model.x = '___'; |
| 55 | 56 }).then(_nextMicrotask).then((_) { |
| 56 return new Future(() { | 57 expect(model.x, '___'); |
| 57 expect(model.x, '___'); | 58 expect(el.text, '___'); |
| 58 expect(el.text, '___'); | |
| 59 }); | |
| 60 }); | 59 }); |
| 61 }); | 60 }); |
| 62 | 61 |
| 63 test('should log eval exceptions', () { | 62 test('should log eval exceptions', () { |
| 64 var model = new NotifyModel('abcde'); | 63 var model = new NotifyModel('abcde'); |
| 65 var completer = new Completer(); | 64 var completer = new Completer(); |
| 66 runZoned(() { | 65 runZoned(() { |
| 67 var template = templateBind(new Element.html( | 66 var template = templateBind(new Element.html( |
| 68 '<template><span>{{foo}}</span></template>')); | 67 '<template><span>{{foo}}</span></template>')); |
| 69 testDiv.append(template.createInstance(model, | 68 testDiv.append(template.createInstance(model, |
| 70 new PolymerExpressions())); | 69 new PolymerExpressions())); |
| 71 | 70 |
| 72 return new Future(() {}); | 71 return _nextMicrotask(null); |
| 73 }, onError: (e) { | 72 }, onError: (e) { |
| 74 expect('$e', startsWith("Error evaluating expression 'foo':")); | 73 expect('$e', startsWith("Error evaluating expression 'foo':")); |
| 75 completer.complete(true); | 74 completer.complete(true); |
| 76 }); | 75 }); |
| 77 return completer.future; | 76 return completer.future; |
| 78 }); | 77 }); |
| 79 | 78 |
| 80 test('should preserve the cursor position', () { | 79 test('should preserve the cursor position', () { |
| 81 var model = new NotifyModel('abcde'); | 80 var model = new NotifyModel('abcde'); |
| 82 var template = templateBind(new Element.html( | 81 var template = templateBind(new Element.html( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 111 expect(el.value, 'abc de'); | 110 expect(el.value, 'abc de'); |
| 112 | 111 |
| 113 // But propagating observable values through reassign the value and | 112 // But propagating observable values through reassign the value and |
| 114 // selection will be preserved. | 113 // selection will be preserved. |
| 115 expect(el.selectionStart, 4); | 114 expect(el.selectionStart, 4); |
| 116 expect(el.selectionEnd, 4); | 115 expect(el.selectionEnd, 4); |
| 117 | 116 |
| 118 subscription.cancel(); | 117 subscription.cancel(); |
| 119 }); | 118 }); |
| 120 }); | 119 }); |
| 120 |
| 121 |
| 122 test('detects changes to ObservableMap keys/values', () { |
| 123 var map = new ObservableMap.from({'a': 1, 'b': 2}); |
| 124 var template = templateBind(new Element.html('<template>' |
| 125 '<template repeat="{{k in x.keys}}">{{k}}:{{x[k]}},</template>' |
| 126 '</template>')); |
| 127 var model = new NotifyModel(map); |
| 128 testDiv.append(template.createInstance(model, new PolymerExpressions())); |
| 129 |
| 130 return new Future(() { |
| 131 expect(testDiv.text, 'a:1,b:2,'); |
| 132 map.remove('b'); |
| 133 map['c'] = 3; |
| 134 }).then(_nextMicrotask).then((_) { |
| 135 expect(testDiv.text, 'a:1,c:3,'); |
| 136 map['a'] = 4; |
| 137 }).then(_nextMicrotask).then((_) { |
| 138 expect(testDiv.text, 'a:4,c:3,'); |
| 139 }); |
| 140 }); |
| 121 }); | 141 }); |
| 122 }); | 142 }); |
| 123 | 143 |
| 144 _nextMicrotask(_) => new Future(() {}); |
| 145 |
| 124 @reflectable | 146 @reflectable |
| 125 class NotifyModel extends ChangeNotifier { | 147 class NotifyModel extends ChangeNotifier { |
| 126 var _x; | 148 var _x; |
| 127 NotifyModel([this._x]); | 149 NotifyModel([this._x]); |
| 128 | 150 |
| 129 get x => _x; | 151 get x => _x; |
| 130 set x(value) { | 152 set x(value) { |
| 131 _x = notifyPropertyChange(#x, _x, value); | 153 _x = notifyPropertyChange(#x, _x, value); |
| 132 } | 154 } |
| 133 } | 155 } |
| OLD | NEW |