| 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 node_bindings_test; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:html'; | |
| 9 import 'package:mdv_observe/mdv_observe.dart'; | |
| 10 import 'package:unittest/html_config.dart'; | |
| 11 import 'package:unittest/unittest.dart'; | |
| 12 import 'mdv_observe_utils.dart'; | |
| 13 | |
| 14 // Note: this file ported from | |
| 15 // https://github.com/toolkitchen/mdv/blob/master/tests/node_bindings.js | |
| 16 | |
| 17 main() { | |
| 18 useHtmlConfiguration(); | |
| 19 group('Node Bindings', nodeBindingTests); | |
| 20 } | |
| 21 | |
| 22 sym(x) => new Symbol(x); | |
| 23 | |
| 24 nodeBindingTests() { | |
| 25 var testDiv; | |
| 26 | |
| 27 setUp(() { | |
| 28 document.body.append(testDiv = new DivElement()); | |
| 29 }); | |
| 30 | |
| 31 tearDown(() { | |
| 32 testDiv.remove(); | |
| 33 testDiv = null; | |
| 34 }); | |
| 35 | |
| 36 dispatchEvent(type, target) { | |
| 37 target.dispatchEvent(new Event(type, cancelable: false)); | |
| 38 } | |
| 39 | |
| 40 test('Text', () { | |
| 41 var text = new Text('hi'); | |
| 42 var model = toSymbolMap({'a': 1}); | |
| 43 text.bind('text', model, 'a'); | |
| 44 expect(text.text, '1'); | |
| 45 | |
| 46 model[sym('a')] = 2; | |
| 47 deliverChangeRecords(); | |
| 48 expect(text.text, '2'); | |
| 49 | |
| 50 text.unbind('text'); | |
| 51 model[sym('a')] = 3; | |
| 52 deliverChangeRecords(); | |
| 53 expect(text.text, '2'); | |
| 54 | |
| 55 // TODO(rafaelw): Throw on binding to unavailable property? | |
| 56 }); | |
| 57 | |
| 58 test('Element', () { | |
| 59 var element = new DivElement(); | |
| 60 var model = toSymbolMap({'a': 1, 'b': 2}); | |
| 61 element.bind('hidden?', model, 'a'); | |
| 62 element.bind('id', model, 'b'); | |
| 63 | |
| 64 expect(element.attributes, contains('hidden')); | |
| 65 expect(element.attributes['hidden'], ''); | |
| 66 expect(element.id, '2'); | |
| 67 | |
| 68 model[sym('a')] = null; | |
| 69 deliverChangeRecords(); | |
| 70 expect(element.attributes, isNot(contains('hidden')), | |
| 71 reason: 'null is false-y'); | |
| 72 | |
| 73 model[sym('a')] = false; | |
| 74 deliverChangeRecords(); | |
| 75 expect(element.attributes, isNot(contains('hidden'))); | |
| 76 | |
| 77 model[sym('a')] = 'foo'; | |
| 78 model[sym('b')] = 'x'; | |
| 79 deliverChangeRecords(); | |
| 80 expect(element.attributes, contains('hidden')); | |
| 81 expect(element.attributes['hidden'], ''); | |
| 82 expect(element.id, 'x'); | |
| 83 }); | |
| 84 | |
| 85 test('Text Input', () { | |
| 86 var input = new InputElement(); | |
| 87 var model = toSymbolMap({'x': 42}); | |
| 88 input.bind('value', model, 'x'); | |
| 89 expect(input.value, '42'); | |
| 90 | |
| 91 model[sym('x')] = 'Hi'; | |
| 92 expect(input.value, '42', reason: 'changes delivered async'); | |
| 93 deliverChangeRecords(); | |
| 94 expect(input.value, 'Hi'); | |
| 95 | |
| 96 input.value = 'changed'; | |
| 97 dispatchEvent('input', input); | |
| 98 expect(model[sym('x')], 'changed'); | |
| 99 | |
| 100 input.unbind('value'); | |
| 101 | |
| 102 input.value = 'changed again'; | |
| 103 dispatchEvent('input', input); | |
| 104 expect(model[sym('x')], 'changed'); | |
| 105 | |
| 106 input.bind('value', model, 'x'); | |
| 107 model[sym('x')] = null; | |
| 108 deliverChangeRecords(); | |
| 109 expect(input.value, ''); | |
| 110 }); | |
| 111 | |
| 112 test('Radio Input', () { | |
| 113 var input = new InputElement(); | |
| 114 input.type = 'radio'; | |
| 115 var model = toSymbolMap({'x': true}); | |
| 116 input.bind('checked', model, 'x'); | |
| 117 expect(input.checked, true); | |
| 118 | |
| 119 model[sym('x')] = false; | |
| 120 expect(input.checked, true); | |
| 121 deliverChangeRecords(); | |
| 122 expect(input.checked, false,reason: 'model change should update checked'); | |
| 123 | |
| 124 input.checked = true; | |
| 125 dispatchEvent('change', input); | |
| 126 expect(model[sym('x')], true, reason: 'input.checked should set model'); | |
| 127 | |
| 128 input.unbind('checked'); | |
| 129 | |
| 130 input.checked = false; | |
| 131 dispatchEvent('change', input); | |
| 132 expect(model[sym('x')], true, | |
| 133 reason: 'disconnected binding should not fire'); | |
| 134 }); | |
| 135 | |
| 136 test('Checkbox Input', () { | |
| 137 var input = new InputElement(); | |
| 138 testDiv.append(input); | |
| 139 input.type = 'checkbox'; | |
| 140 var model = toSymbolMap({'x': true}); | |
| 141 input.bind('checked', model, 'x'); | |
| 142 expect(input.checked, true); | |
| 143 | |
| 144 model[sym('x')] = false; | |
| 145 expect(input.checked, true, reason: 'changes delivered async'); | |
| 146 deliverChangeRecords(); | |
| 147 expect(input.checked, false); | |
| 148 | |
| 149 input.click(); | |
| 150 expect(model[sym('x')], true); | |
| 151 deliverChangeRecords(); | |
| 152 | |
| 153 input.click(); | |
| 154 expect(model[sym('x')], false); | |
| 155 }); | |
| 156 } | |
| OLD | NEW |