OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library polymer.test.bind_mdv_test; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:html'; | |
9 import 'package:custom_element/polyfill.dart'; | |
10 import 'package:template_binding/template_binding.dart'; | |
11 import 'package:observe/observe.dart'; | |
12 import 'package:observe/src/microtask.dart' show runMicrotask; | |
13 import 'package:polymer/platform.dart' as Platform; | |
14 import 'package:unittest/html_config.dart'; | |
15 import 'package:unittest/unittest.dart'; | |
16 | |
17 main() { | |
18 useHtmlConfiguration(); | |
19 | |
20 var registered = loadCustomElementPolyfill().then((_) { | |
21 document.register('my-div', MyDivElement); | |
22 }); | |
23 | |
24 setUp(() => registered); | |
25 | |
26 group('bindModel', bindModelTests); | |
27 } | |
28 | |
29 bindModelTests() { | |
30 var div; | |
31 | |
32 setUp(() { | |
33 div = new MyDivElement(); | |
34 document.body.append(div); | |
35 }); | |
36 | |
37 tearDown(() { | |
38 div.remove(); | |
39 }); | |
40 | |
41 parseAndBindHTML(html, model) => | |
42 templateBind(new Element.tag('template') | |
43 ..setInnerHtml(html, treeSanitizer: const NullTreeSanitizer())) | |
44 .createInstance(model); | |
45 | |
46 test('bindModel', () { | |
47 var fragment = parseAndBindHTML('<div id="a" foo="{{bar}}"></div>', div); | |
48 div.append(fragment); | |
49 var a = div.query('#a'); | |
50 | |
51 div.bar = 5; | |
52 return onAttributeChange(a).then((_) { | |
53 expect(a.attributes['foo'], '5'); | |
54 div.bar = 8; | |
55 return onAttributeChange(a).then((_) { | |
56 expect(a.attributes['foo'], '8'); | |
57 }); | |
58 }); | |
59 }); | |
60 | |
61 test('bind input', () { | |
62 var fragment = parseAndBindHTML('<input value="{{bar}}" />', div); | |
63 div.append(fragment); | |
64 var a = div.query('input'); | |
65 | |
66 div.bar = 'hello'; | |
67 // TODO(sorvell): fix this when observe-js lets us explicitly listen for | |
68 // a change on input.value | |
69 Observable.dirtyCheck(); | |
Siggi Cherem (dart-lang)
2013/10/24 00:50:43
is this needed if you are using endOfMicrotask bel
Jennifer Messerly
2013/10/24 00:58:59
yeah ... the end of microtask runs faster than pol
| |
70 Platform.endOfMicrotask(expectAsync0(() { | |
71 expect(a.value, 'hello'); | |
72 })); | |
73 }); | |
74 } | |
75 | |
76 class MyDivElement extends HtmlElement with Observable { | |
77 factory MyDivElement() => new Element.tag('my-div'); | |
78 MyDivElement.created() : super.created(); | |
79 @observable var bar; | |
80 } | |
81 | |
82 class NullTreeSanitizer implements NodeTreeSanitizer { | |
83 const NullTreeSanitizer(); | |
84 void sanitizeTree(Node node) {} | |
85 } | |
86 | |
87 Future onAttributeChange(Element node) { | |
88 var completer = new Completer(); | |
89 new MutationObserver((records, observer) { | |
90 observer.disconnect(); | |
91 completer.complete(); | |
92 })..observe(node, attributes: true); | |
93 Platform.flush(); | |
94 return completer.future; | |
95 } | |
OLD | NEW |