| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 import 'dart:async'; | |
| 6 import 'dart:html'; | |
| 7 import 'package:unittest/unittest.dart'; | |
| 8 import 'package:unittest/html_config.dart'; | |
| 9 import 'package:polymer/polymer.dart'; | |
| 10 | |
| 11 | |
| 12 class XAttrPublish extends PolymerElement { | |
| 13 XAttrPublish.created() : super.created(); | |
| 14 | |
| 15 @PublishedProperty(reflect: false) | |
| 16 String nog = ''; | |
| 17 } | |
| 18 | |
| 19 class XFoo extends PolymerElement { | |
| 20 XFoo.created() : super.created(); | |
| 21 | |
| 22 @PublishedProperty(reflect: true) var foo = ''; | |
| 23 @PublishedProperty(reflect: true) String baz = ''; | |
| 24 | |
| 25 @PublishedProperty(reflect: true) var def1, def2; | |
| 26 } | |
| 27 | |
| 28 class XBar extends XFoo { | |
| 29 XBar.created() : super.created(); | |
| 30 | |
| 31 @PublishedProperty(reflect: true) int zot = 3; | |
| 32 @PublishedProperty(reflect: true) bool zim = false; | |
| 33 @PublishedProperty(reflect: true) String str = 'str'; | |
| 34 @PublishedProperty(reflect: true) Object obj; | |
| 35 } | |
| 36 | |
| 37 class XZot extends XBar { | |
| 38 // Dart note: trying to make this roughly equivalent to the JS | |
| 39 @PublishedProperty(reflect: false) int get zot => super.zot; | |
| 40 @PublishedProperty(reflect: false) set zot(int x) { super.zot = x; } | |
| 41 | |
| 42 XZot.created() : super.created() { | |
| 43 zot = 2; | |
| 44 str = 'str2'; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 class XCompose extends PolymerElement { | |
| 49 XCompose.created() : super.created(); | |
| 50 | |
| 51 @observable bool zim = false; | |
| 52 } | |
| 53 | |
| 54 Future onAttributeChange(Element node) { | |
| 55 var completer = new Completer(); | |
| 56 new MutationObserver((records, observer) { | |
| 57 observer.disconnect(); | |
| 58 completer.complete(); | |
| 59 })..observe(node, attributes: true); | |
| 60 scheduleMicrotask(Observable.dirtyCheck); | |
| 61 return completer.future; | |
| 62 } | |
| 63 | |
| 64 main() => initPolymer().run(() { | |
| 65 useHtmlConfiguration(); | |
| 66 | |
| 67 // Most tests use @CustomTag, here we test out the impertive register: | |
| 68 Polymer.register('x-foo', XFoo); | |
| 69 Polymer.register('x-bar', XBar); | |
| 70 Polymer.register('x-zot', XZot); | |
| 71 Polymer.register('x-compose', XCompose); | |
| 72 Polymer.register('x-attr-publish', XAttrPublish); | |
| 73 | |
| 74 setUp(() => Polymer.onReady); | |
| 75 | |
| 76 test('property to attribute reflection', () { | |
| 77 var xbasic = querySelector('x-basic'); | |
| 78 expect(xbasic.getAttribute('nog'), null, reason: | |
| 79 'property published with `attributes` has correct initial value'); | |
| 80 xbasic.setAttribute('nog', 'hi'); | |
| 81 expect(xbasic.getAttribute('nog'), 'hi', reason: | |
| 82 'deserialization of property published via `attributes`'); | |
| 83 | |
| 84 var xattrpublish = querySelector('x-attr-publish'); | |
| 85 expect(xattrpublish.nog, '', reason: | |
| 86 'property published with `attributes` has correct initial value'); | |
| 87 xattrpublish.setAttribute('nog', 'hi'); | |
| 88 expect(xattrpublish.nog, 'hi', reason: | |
| 89 'deserialization of property published via `attributes`'); | |
| 90 | |
| 91 var xcompose = querySelector('x-compose'); | |
| 92 var xfoo = querySelector('x-foo'); | |
| 93 expect(xfoo.foo, '', reason: | |
| 94 'property published with info object has correct initial value'); | |
| 95 var xbar = querySelector('x-bar'); | |
| 96 expect(xbar.zim, false, reason: | |
| 97 'property published with info object has correct initial value'); | |
| 98 expect(xbar.foo, '', reason: | |
| 99 'property published with info object has correct initial value'); | |
| 100 var xzot = querySelector('x-zot'); | |
| 101 xfoo.foo = 5; | |
| 102 xfoo.attributes['def1'] = '15'; | |
| 103 xfoo.def2 = 15; | |
| 104 | |
| 105 // Dart note: our test here is more async than JS until we change | |
| 106 // Polymer.dart bindings to work like observe.js "bindToInstance". | |
| 107 return onAttributeChange(xfoo).then((_) { | |
| 108 expect(xcompose.$['bar'].attributes.containsKey('zim'), false, | |
| 109 reason: 'attribute bound to property updates when binding is made'); | |
| 110 | |
| 111 expect(xfoo.attributes['def2'], '15', | |
| 112 reason: 'default valued published property reflects to attr'); | |
| 113 | |
| 114 expect(xfoo.def1, 15, | |
| 115 reason: 'attr updates default valued published property'); | |
| 116 | |
| 117 expect('${xfoo.foo}', xfoo.attributes['foo'], | |
| 118 reason: 'attribute reflects property as string'); | |
| 119 xfoo.attributes['foo'] = '27'; | |
| 120 // TODO(jmesserly): why is JS leaving this as a String? From the code it | |
| 121 // looks like it should use the type of 5 and parse it as a number. | |
| 122 expect('${xfoo.foo}', xfoo.attributes['foo'], | |
| 123 reason: 'property reflects attribute'); | |
| 124 // | |
| 125 xfoo.baz = 'Hello'; | |
| 126 }).then((_) => onAttributeChange(xfoo)).then((_) { | |
| 127 expect(xfoo.baz, xfoo.attributes['baz'], | |
| 128 reason: 'attribute reflects property'); | |
| 129 // | |
| 130 xbar.foo = 'foo!'; | |
| 131 xbar.zot = 27; | |
| 132 xbar.zim = true; | |
| 133 xbar.str = 'str!'; | |
| 134 xbar.obj = {'hello': 'world'}; | |
| 135 }).then((_) => onAttributeChange(xbar)).then((_) { | |
| 136 expect(xbar.foo, xbar.attributes['foo'], | |
| 137 reason: 'inherited published property is reflected'); | |
| 138 expect('${xbar.zot}', xbar.attributes['zot'], | |
| 139 reason: 'attribute reflects property as number'); | |
| 140 expect(xbar.attributes['zim'], '', reason: | |
| 141 'attribute reflects true valued boolean property as ' | |
| 142 'having attribute'); | |
| 143 expect(xbar.str, xbar.attributes['str'], | |
| 144 reason: 'attribute reflects property as published string'); | |
| 145 expect(xbar.attributes.containsKey('obj'), false, | |
| 146 reason: 'attribute does not reflect object property'); | |
| 147 xbar.attributes['zim'] = 'false'; | |
| 148 xbar.attributes['foo'] = 'foo!!'; | |
| 149 xbar.attributes['zot'] = '54'; | |
| 150 xbar.attributes['str'] = 'str!!'; | |
| 151 xbar.attributes['obj'] = "{'hello': 'world'}"; | |
| 152 expect(xbar.foo, xbar.attributes['foo'], | |
| 153 reason: 'property reflects attribute as string'); | |
| 154 expect(xbar.zot, 54, | |
| 155 reason: 'property reflects attribute as number'); | |
| 156 expect(xbar.zim, false, | |
| 157 reason: 'property reflects attribute as boolean'); | |
| 158 expect(xbar.str, 'str!!', | |
| 159 reason: 'property reflects attribute as published string'); | |
| 160 expect(xbar.obj, {'hello': 'world'}, | |
| 161 reason: 'property reflects attribute as object'); | |
| 162 xbar.zim = false; | |
| 163 }).then((_) => onAttributeChange(xbar)).then((_) { | |
| 164 expect(xbar.attributes.containsKey('zim'), false, reason: | |
| 165 'attribute reflects false valued boolean property as NOT ' | |
| 166 'having attribute'); | |
| 167 xbar.obj = 'hi'; | |
| 168 }).then((_) => onAttributeChange(xbar)).then((_) { | |
| 169 expect(xbar.attributes['obj'], 'hi', reason: | |
| 170 'reflect property based on current type'); | |
| 171 | |
| 172 expect(xzot.str, 'str2'); | |
| 173 expect(xzot.zot, 2); | |
| 174 xzot.str = 'hello'; | |
| 175 xzot.zot = 5; | |
| 176 }).then((_) => onAttributeChange(xzot)).then((_) { | |
| 177 expect(xzot.attributes['str'], xzot.str); | |
| 178 // TODO(jmesserly): the JS test seems backwards of the description text. | |
| 179 // Is it because it doesn't do "WebComponents.flush()"? | |
| 180 expect(xzot.attributes['zot'], '5', | |
| 181 reason: 'extendee reflect false not honored'); | |
| 182 }); | |
| 183 }); | |
| 184 }); | |
| OLD | NEW |