Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: tests/html/node_bindings_test.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: comment tweaks Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 'dart:observe';
10 import '../../pkg/unittest/lib/unittest.dart';
11 import '../../pkg/unittest/lib/html_config.dart';
12
13 // Note: this file ported from
14 // https://github.com/toolkitchen/mdv/blob/master/tests/node_bindings.js
15
16 main() {
17 useHtmlConfiguration();
18 group('Node Bindings', nodeBindingTests);
19 }
20
21 nodeBindingTests() {
22 var testDiv;
23
24 setUp(() {
25 document.body.nodes.add(testDiv = new DivElement());
26 });
27
28 tearDown(() {
29 testDiv.remove();
30 testDiv = null;
31 });
32
33 dispatchEvent(type, target) {
34 target.dispatchEvent(new Event(type, cancelable: false));
35 }
36
37 test('Text', () {
38 var text = new Text('hi');
39 var model = toObservable({'a': 1});
40 text.bind('text', model, 'a');
41 expect(text.text, '1');
42
43 model['a'] = 2;
44 deliverChangeRecords();
45 expect(text.text, '2');
46
47 text.unbind('text');
48 model['a'] = 3;
49 deliverChangeRecords();
50 expect(text.text, '2');
51
52 // TODO(rafaelw): Throw on binding to unavailable property?
53 });
54
55 test('Element', () {
56 var element = new DivElement();
57 var model = toObservable({'a': 1, 'b': 2});
58 element.bind('hidden?', model, 'a');
59 element.bind('id', model, 'b');
60
61 expect(element.attributes, contains('hidden'));
62 expect(element.attributes['hidden'], '');
63 expect(element.id, '2');
64
65 model['a'] = null;
66 deliverChangeRecords();
67 expect(element.attributes, isNot(contains('hidden')),
68 reason: 'null is false-y');
69
70 model['a'] = false;
71 deliverChangeRecords();
72 expect(element.attributes, isNot(contains('hidden')));
73
74 model['a'] = 'foo';
75 model['b'] = 'x';
76 deliverChangeRecords();
77 expect(element.attributes, contains('hidden'));
78 expect(element.attributes['hidden'], '');
79 expect(element.id, 'x');
80 });
81
82 test('Text Input', () {
83 var input = new InputElement();
84 var model = toObservable({'x': 42});
85 input.bind('value', model, 'x');
86 expect(input.value, '42');
87
88 model['x'] = 'Hi';
89 expect(input.value, '42', reason: 'changes delivered async');
90 deliverChangeRecords();
91 expect(input.value, 'Hi');
92
93 input.value = 'changed';
94 dispatchEvent('input', input);
95 expect(model['x'], 'changed');
96
97 input.unbind('value');
98
99 input.value = 'changed again';
100 dispatchEvent('input', input);
101 expect(model['x'], 'changed');
102
103 input.bind('value', model, 'x');
104 model['x'] = null;
105 deliverChangeRecords();
106 expect(input.value, '');
107 });
108
109 test('Radio Input', () {
110 var input = new InputElement();
111 input.type = 'radio';
112 var model = toObservable({'x': true});
113 input.bind('checked', model, 'x');
114 expect(input.checked, true);
115
116 model['x'] = false;
117 expect(input.checked, true);
118 deliverChangeRecords();
119 expect(input.checked, false, reason: 'model change should update checked');
120
121 input.checked = true;
122 dispatchEvent('change', input);
123 expect(model['x'], true, reason: 'input.checked should set model');
124
125 input.unbind('checked');
126
127 input.checked = false;
128 dispatchEvent('change', input);
129 expect(model['x'], true, reason: 'disconnected binding should not fire');
130 });
131
132 test('Checkbox Input', () {
133 var input = new InputElement();
134 testDiv.nodes.add(input);
135 input.type = 'checkbox';
136 var model = toObservable({'x': true});
137 input.bind('checked', model, 'x');
138 expect(input.checked, true);
139
140 model['x'] = false;
141 expect(input.checked, true, reason: 'changes delivered async');
142 deliverChangeRecords();
143 expect(input.checked, false);
144
145 input.click();
146 expect(model['x'], true);
147 deliverChangeRecords();
148
149 input.click();
150 expect(model['x'], false);
151 });
152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698