| 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 binding_syntax_test; | 5 library template_binding.test.binding_syntax_test; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 import 'package:mdv/mdv.dart' as mdv; | 9 import 'package:template_binding/template_binding.dart'; |
| 10 import 'package:observe/observe.dart'; | 10 import 'package:observe/observe.dart'; |
| 11 import 'package:unittest/html_config.dart'; | 11 import 'package:unittest/html_config.dart'; |
| 12 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
| 13 import 'mdv_test_utils.dart'; | 13 import 'utils.dart'; |
| 14 | 14 |
| 15 // Note: this file ported from | 15 // Note: this file ported from |
| 16 // https://github.com/toolkitchen/mdv/blob/master/tests/syntax.js | 16 // https://github.com/toolkitchen/mdv/blob/master/tests/syntax.js |
| 17 | 17 |
| 18 main() { | 18 main() { |
| 19 mdv.initialize(); | |
| 20 useHtmlConfiguration(); | 19 useHtmlConfiguration(); |
| 21 | 20 |
| 22 group('Syntax FooBarModel', () { | 21 group('Syntax FooBarModel', () { |
| 23 syntaxTests(([f, b]) => new FooBarModel(f, b)); | 22 syntaxTests(([f, b]) => new FooBarModel(f, b)); |
| 24 }); | 23 }); |
| 25 group('Syntax FooBarNotifyModel', () { | 24 group('Syntax FooBarNotifyModel', () { |
| 26 syntaxTests(([f, b]) => new FooBarNotifyModel(f, b)); | 25 syntaxTests(([f, b]) => new FooBarNotifyModel(f, b)); |
| 27 }); | 26 }); |
| 28 } | 27 } |
| 29 | 28 |
| 30 syntaxTests(FooBarModel fooModel([foo, bar])) { | 29 syntaxTests(FooBarModel fooModel([foo, bar])) { |
| 31 var testDiv; | |
| 32 | |
| 33 setUp(() { | 30 setUp(() { |
| 34 document.body.append(testDiv = new DivElement()); | 31 document.body.append(testDiv = new DivElement()); |
| 35 }); | 32 }); |
| 36 | 33 |
| 37 tearDown(() { | 34 tearDown(() { |
| 38 testDiv.remove(); | 35 testDiv.remove(); |
| 39 testDiv = null; | 36 testDiv = null; |
| 40 }); | 37 }); |
| 41 | 38 |
| 42 createTestHtml(s) { | |
| 43 var div = new DivElement(); | |
| 44 div.innerHtml = s; | |
| 45 testDiv.append(div); | |
| 46 | |
| 47 for (var node in div.queryAll('*')) { | |
| 48 if (node.isTemplate) TemplateElement.decorate(node); | |
| 49 } | |
| 50 | |
| 51 return div; | |
| 52 } | |
| 53 | |
| 54 observeTest('Registration', () { | 39 observeTest('Registration', () { |
| 55 var model = fooModel('bar'); | 40 var model = fooModel('bar'); |
| 56 | |
| 57 var testSyntax = new TestBindingSyntax(); | 41 var testSyntax = new TestBindingSyntax(); |
| 58 var div = createTestHtml('<template bind>{{ foo }}' | 42 var div = createTestHtml('<template bind>{{ foo }}' |
| 59 '<template bind>{{ foo }}</template></template>'); | 43 '<template bind>{{ foo }}</template></template>'); |
| 60 recursivelySetTemplateModel(div, model, testSyntax); | 44 recursivelySetTemplateModel(div, model, testSyntax); |
| 61 performMicrotaskCheckpoint(); | 45 performMicrotaskCheckpoint(); |
| 62 expect(div.nodes.length, 4); | 46 expect(div.nodes.length, 4); |
| 63 expect(div.nodes.last.text, 'bar'); | 47 expect(div.nodes.last.text, 'bar'); |
| 64 expect(div.nodes[2].tagName, 'TEMPLATE'); | 48 expect(div.nodes[2].tagName, 'TEMPLATE'); |
| 65 | |
| 66 expect(testSyntax.log, [ | 49 expect(testSyntax.log, [ |
| 67 [model, '', 'bind', 'TEMPLATE'], | 50 [model, '', 'bind', 'TEMPLATE'], |
| 68 [model, 'foo', 'text', null], | 51 [model, 'foo', 'text', null], |
| 69 [model, '', 'bind', 'TEMPLATE'], | 52 [model, '', 'bind', 'TEMPLATE'], |
| 70 [model, 'foo', 'text', null], | 53 [model, 'foo', 'text', null], |
| 71 ]); | 54 ]); |
| 72 }); | 55 }); |
| 73 | 56 |
| 74 observeTest('getInstanceModel', () { | 57 observeTest('getInstanceModel', () { |
| 75 var model = toObservable([fooModel(1), fooModel(2), fooModel(3)]); | 58 var model = toObservable([fooModel(1), fooModel(2), fooModel(3)]); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 performMicrotaskCheckpoint(); | 111 performMicrotaskCheckpoint(); |
| 129 expect(div.nodes.last.text, '4 + 16 + '); | 112 expect(div.nodes.last.text, '4 + 16 + '); |
| 130 }); | 113 }); |
| 131 } | 114 } |
| 132 | 115 |
| 133 // TODO(jmesserly): mocks would be cleaner here. | 116 // TODO(jmesserly): mocks would be cleaner here. |
| 134 | 117 |
| 135 class TestBindingSyntax extends BindingDelegate { | 118 class TestBindingSyntax extends BindingDelegate { |
| 136 var log = []; | 119 var log = []; |
| 137 | 120 |
| 138 getBinding(model, String path, String name, Node node) { | 121 getBinding(model, String path, String name, node) { |
| 139 log.add([model, path, name, node is Element ? node.tagName : null]); | 122 log.add([model, path, name, node is Element ? node.tagName : null]); |
| 140 } | 123 } |
| 141 } | 124 } |
| 142 | 125 |
| 143 class TestModelSyntax extends BindingDelegate { | 126 class TestModelSyntax extends BindingDelegate { |
| 144 var log = []; | 127 var log = []; |
| 145 var altModels = new ListQueue(); | 128 var altModels = new ListQueue(); |
| 146 | 129 |
| 147 getInstanceModel(template, model) { | 130 getInstanceModel(template, model) { |
| 148 log.add([template, model]); | 131 log.add([template, model]); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 159 } | 142 } |
| 160 | 143 |
| 161 // Note: this isn't a very smart whitespace handler. A smarter one would only | 144 // Note: this isn't a very smart whitespace handler. A smarter one would only |
| 162 // trim indentation, not all whitespace. | 145 // trim indentation, not all whitespace. |
| 163 // See "trimOrCompact" in the web_ui Pub package. | 146 // See "trimOrCompact" in the web_ui Pub package. |
| 164 class WhitespaceRemover extends BindingDelegate { | 147 class WhitespaceRemover extends BindingDelegate { |
| 165 int trimmed = 0; | 148 int trimmed = 0; |
| 166 int removed = 0; | 149 int removed = 0; |
| 167 | 150 |
| 168 DocumentFragment getInstanceFragment(Element template) { | 151 DocumentFragment getInstanceFragment(Element template) { |
| 169 var instance = template.createInstance(); | 152 var instance = templateBind(template).createInstance(); |
| 170 var walker = new TreeWalker(instance, NodeFilter.SHOW_TEXT); | 153 var walker = new TreeWalker(instance, NodeFilter.SHOW_TEXT); |
| 171 | 154 |
| 172 var toRemove = []; | 155 var toRemove = []; |
| 173 while (walker.nextNode() != null) { | 156 while (walker.nextNode() != null) { |
| 174 var node = walker.currentNode; | 157 var node = walker.currentNode; |
| 175 var text = node.text.replaceAll('\n', '').trim(); | 158 var text = node.text.replaceAll('\n', '').trim(); |
| 176 if (text.length != node.text.length) { | 159 if (text.length != node.text.length) { |
| 177 if (text.length == 0) { | 160 if (text.length == 0) { |
| 178 toRemove.add(node); | 161 toRemove.add(node); |
| 179 } else { | 162 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 193 class TimesTwoSyntax extends BindingDelegate { | 176 class TimesTwoSyntax extends BindingDelegate { |
| 194 getBinding(model, path, name, node) { | 177 getBinding(model, path, name, node) { |
| 195 path = path.trim(); | 178 path = path.trim(); |
| 196 if (!path.startsWith('2x:')) return null; | 179 if (!path.startsWith('2x:')) return null; |
| 197 | 180 |
| 198 path = path.substring(3); | 181 path = path.substring(3); |
| 199 return new CompoundBinding((values) => values['value'] * 2) | 182 return new CompoundBinding((values) => values['value'] * 2) |
| 200 ..bind('value', model, path); | 183 ..bind('value', model, path); |
| 201 } | 184 } |
| 202 } | 185 } |
| OLD | NEW |