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

Side by Side Diff: third_party/pkg/angular/test/directive/ng_model_spec.dart

Issue 148453003: Updating Angular version (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 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
1 library ng_model_spec; 1 library ng_model_spec;
2 2
3 import '../_specs.dart'; 3 import '../_specs.dart';
4 import 'dart:html' as dom; 4 import 'dart:html' as dom;
5 5
6 main() => 6 main() =>
7 describe('ng-model', () { 7 describe('ng-model', () {
8 TestBed _; 8 TestBed _;
9 9
10 beforeEach(inject((TestBed tb) => _ = tb)); 10 beforeEach(inject((TestBed tb) => _ = tb));
(...skipping 23 matching lines...) Expand all
34 _.compile('<input type="text" ng-model="model" probe="p">'); 34 _.compile('<input type="text" ng-model="model" probe="p">');
35 Probe probe = _.rootScope.p; 35 Probe probe = _.rootScope.p;
36 var ngModel = probe.directive(NgModel); 36 var ngModel = probe.directive(NgModel);
37 InputElement inputElement = probe.element; 37 InputElement inputElement = probe.element;
38 38
39 inputElement.value = 'abc'; 39 inputElement.value = 'abc';
40 _.triggerEvent(inputElement, 'change'); 40 _.triggerEvent(inputElement, 'change');
41 expect(_.rootScope.model).toEqual('abc'); 41 expect(_.rootScope.model).toEqual('abc');
42 42
43 inputElement.value = 'def'; 43 inputElement.value = 'def';
44 var input = probe.directive(InputTextDirective); 44 var input = probe.directive(InputTextLikeDirective);
45 input.processValue(); 45 input.processValue();
46 expect(_.rootScope.model).toEqual('def'); 46 expect(_.rootScope.model).toEqual('def');
47
48 })); 47 }));
49 48
50 it('should write to input only if value is different', inject(() { 49 it('should write to input only if value is different', inject(() {
51 var scope = _.rootScope; 50 var scope = _.rootScope;
52 var model = new NgModel(scope, new NodeAttrs(new DivElement()));
53 var element = new dom.InputElement(); 51 var element = new dom.InputElement();
52 var model = new NgModel(scope, new NodeAttrs(new DivElement()), element, n ew NgNullForm());
54 dom.querySelector('body').append(element); 53 dom.querySelector('body').append(element);
55 var input = new InputTextDirective(element, model, scope); 54 var input = new InputTextLikeDirective(element, model, scope);
56 55
57 element.value = 'abc'; 56 element.value = 'abc';
58 element.selectionStart = 1; 57 element.selectionStart = 1;
59 element.selectionEnd = 2; 58 element.selectionEnd = 2;
60 59
61 model.render('abc'); 60 model.render('abc');
62 61
63 expect(element.value).toEqual('abc'); 62 expect(element.value).toEqual('abc');
64 // No update. selectionStart/End is unchanged. 63 // No update. selectionStart/End is unchanged.
65 expect(element.selectionStart).toEqual(1); 64 expect(element.selectionStart).toEqual(1);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 _.compile('<input type="password" ng-model="model" probe="p">'); 98 _.compile('<input type="password" ng-model="model" probe="p">');
100 Probe probe = _.rootScope.p; 99 Probe probe = _.rootScope.p;
101 var ngModel = probe.directive(NgModel); 100 var ngModel = probe.directive(NgModel);
102 InputElement inputElement = probe.element; 101 InputElement inputElement = probe.element;
103 102
104 inputElement.value = 'abc'; 103 inputElement.value = 'abc';
105 _.triggerEvent(inputElement, 'change'); 104 _.triggerEvent(inputElement, 'change');
106 expect(_.rootScope.model).toEqual('abc'); 105 expect(_.rootScope.model).toEqual('abc');
107 106
108 inputElement.value = 'def'; 107 inputElement.value = 'def';
109 var input = probe.directive(InputPasswordDirective); 108 var input = probe.directive(InputTextLikeDirective);
110 input.processValue(); 109 input.processValue();
111 expect(_.rootScope.model).toEqual('def'); 110 expect(_.rootScope.model).toEqual('def');
112 111
113 })); 112 }));
114 113
115 it('should write to input only if value is different', inject(() { 114 it('should write to input only if value is different', inject(() {
116 var scope = _.rootScope; 115 var scope = _.rootScope;
117 var model = new NgModel(scope, new NodeAttrs(new DivElement()));
118 var element = new dom.InputElement(); 116 var element = new dom.InputElement();
117 var model = new NgModel(scope, new NodeAttrs(new DivElement()), element, n ew NgNullForm());
119 dom.querySelector('body').append(element); 118 dom.querySelector('body').append(element);
120 var input = new InputPasswordDirective(element, model, scope); 119 var input = new InputTextLikeDirective(element, model, scope);
121 120
122 element.value = 'abc'; 121 element.value = 'abc';
123 element.selectionStart = 1; 122 element.selectionStart = 1;
124 element.selectionEnd = 2; 123 element.selectionEnd = 2;
125 124
126 model.render('abc'); 125 model.render('abc');
127 126
128 expect(element.value).toEqual('abc'); 127 expect(element.value).toEqual('abc');
129 expect(element.selectionStart).toEqual(1); 128 expect(element.selectionStart).toEqual(1);
130 expect(element.selectionEnd).toEqual(2); 129 expect(element.selectionEnd).toEqual(2);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 var element = _.compile('<input type="checkbox" ng-model="model">'); 176 var element = _.compile('<input type="checkbox" ng-model="model">');
178 177
179 element.checked = true; 178 element.checked = true;
180 _.triggerEvent(element, 'change'); 179 _.triggerEvent(element, 'change');
181 expect(scope['model']).toBe(true); 180 expect(scope['model']).toBe(true);
182 181
183 element.checked = false; 182 element.checked = false;
184 _.triggerEvent(element, 'change'); 183 _.triggerEvent(element, 'change');
185 expect(scope['model']).toBe(false); 184 expect(scope['model']).toBe(false);
186 })); 185 }));
187
188 }); 186 });
189 187
190 describe('type="textarea"', () { 188 describe('type="textarea"', () {
191 it('should update textarea value from model', inject(() { 189 it('should update textarea value from model', inject(() {
192 _.compile('<textarea ng-model="model">'); 190 _.compile('<textarea ng-model="model">');
193 _.rootScope.$digest(); 191 _.rootScope.$digest();
194 192
195 expect((_.rootElement as dom.TextAreaElement).value).toEqual(''); 193 expect((_.rootElement as dom.TextAreaElement).value).toEqual('');
196 194
197 _.rootScope.$apply('model = "misko"'); 195 _.rootScope.$apply('model = "misko"');
(...skipping 14 matching lines...) Expand all
212 _.compile('<textarea ng-model="model" probe="p">'); 210 _.compile('<textarea ng-model="model" probe="p">');
213 Probe probe = _.rootScope.p; 211 Probe probe = _.rootScope.p;
214 var ngModel = probe.directive(NgModel); 212 var ngModel = probe.directive(NgModel);
215 TextAreaElement element = probe.element; 213 TextAreaElement element = probe.element;
216 214
217 element.value = 'abc'; 215 element.value = 'abc';
218 _.triggerEvent(element, 'change'); 216 _.triggerEvent(element, 'change');
219 expect(_.rootScope.model).toEqual('abc'); 217 expect(_.rootScope.model).toEqual('abc');
220 218
221 element.value = 'def'; 219 element.value = 'def';
222 var textarea = probe.directive(TextAreaDirective); 220 var textarea = probe.directive(InputTextLikeDirective);
223 textarea.processValue(); 221 textarea.processValue();
224 expect(_.rootScope.model).toEqual('def'); 222 expect(_.rootScope.model).toEqual('def');
225 223
226 })); 224 }));
227 225
228 // NOTE(deboer): This test passes on Dartium, but fails in the content_shell . 226 // NOTE(deboer): This test passes on Dartium, but fails in the content_shell .
229 // The Dart team is looking into this bug. 227 // The Dart team is looking into this bug.
230 xit('should write to input only if value is different', inject(() { 228 xit('should write to input only if value is different', inject(() {
231 var scope = _.rootScope; 229 var scope = _.rootScope;
232 var model = new NgModel(scope, new NodeAttrs(new DivElement()));
233 var element = new dom.TextAreaElement(); 230 var element = new dom.TextAreaElement();
231 var model = new NgModel(scope, new NodeAttrs(new DivElement()), element);
234 dom.querySelector('body').append(element); 232 dom.querySelector('body').append(element);
235 var input = new TextAreaDirective(element, model, scope); 233 var input = new InputTextLikeDirective(element, model, scope);
236 234
237 element.value = 'abc'; 235 element.value = 'abc';
238 element.selectionStart = 1; 236 element.selectionStart = 1;
239 element.selectionEnd = 2; 237 element.selectionEnd = 2;
240 238
241 model.render('abc'); 239 model.render('abc');
242 240
243 expect(element.value).toEqual('abc'); 241 expect(element.value).toEqual('abc');
244 expect(element.selectionStart).toEqual(1); 242 expect(element.selectionStart).toEqual(1);
245 expect(element.selectionEnd).toEqual(2); 243 expect(element.selectionEnd).toEqual(2);
246 244
247 model.render('xyz'); 245 model.render('xyz');
248 246
249 // Setting the value on a textarea doesn't update the selection the way it 247 // Setting the value on a textarea doesn't update the selection the way it
250 // does on input elements. This stays unchanged. 248 // does on input elements. This stays unchanged.
251 expect(element.value).toEqual('xyz'); 249 expect(element.value).toEqual('xyz');
252 expect(element.selectionStart).toEqual(0); 250 expect(element.selectionStart).toEqual(0);
253 expect(element.selectionEnd).toEqual(0); 251 expect(element.selectionEnd).toEqual(0);
254 })); 252 }));
255 }); 253 });
256 254
257 describe('type="number"', () {
258 it('should update input value from model', inject(() {
259 _.compile('<input type="number" ng-model="model">');
260 _.rootScope.$digest();
261
262 expect((_.rootElement as dom.InputElement).value).toEqual('');
263
264 // Invalid number
265 _.rootScope.$apply('model = "foo"');
266 expect((_.rootElement as dom.InputElement).value).toEqual('');
267
268 // Valid number
269 _.rootScope.$apply('model = 1234');
270 expect((_.rootElement as dom.InputElement).value).toEqual('1234');
271 }));
272
273 it('should not render null or NaN values', inject(() {
274 _.compile('<input type="number" ng-model="model">');
275 _.rootScope.$digest();
276
277 expect((_.rootElement as dom.InputElement).value).toEqual('');
278
279 _.rootScope.$apply('model = null');
280 expect((_.rootElement as dom.InputElement).value).toEqual('');
281
282 _.rootScope['model'] = 0/0;
283 _.rootScope.$digest();
284 expect((_.rootElement as dom.InputElement).value).toEqual('');
285 }));
286
287 it('should update model from the input value', inject(() {
288 _.compile('<input type="number" ng-model="model" probe="p">');
289 Probe probe = _.rootScope.p;
290 var ngModel = probe.directive(NgModel);
291 InputElement inputElement = probe.element;
292
293 // Valid value
294 inputElement.value = '12345';
295 _.triggerEvent(inputElement, 'change');
296 expect(_.rootScope.model).toEqual(12345);
297
298 // Invalid value
299 inputElement.value = 'foo';
300 var input = probe.directive(InputNumberDirective);
301 input.processValue();
302 expect(_.rootScope.model.isNaN).toBe(true);
303 }));
304 });
305
306 describe('type="email"', () {
307 it('should update input value from model', inject(() {
308 _.compile('<input type="email" ng-model="model">');
309 _.rootScope.$digest();
310
311 expect((_.rootElement as dom.InputElement).value).toEqual('');
312
313 // Invalid e-mail
314 _.rootScope.$apply('model = "foo"');
315 expect((_.rootElement as dom.InputElement).value).toEqual('');
316
317 // Valid e-mail
318 _.rootScope.$apply('model = "foo@example.com"');
319 expect((_.rootElement as dom.InputElement).value).toEqual('foo@example.com ');
320 }));
321
322 it('should render null as the empty string', inject(() {
323 _.compile('<input type="email" ng-model="model">');
324 _.rootScope.$digest();
325
326 expect((_.rootElement as dom.InputElement).value).toEqual('');
327
328 _.rootScope.$apply('model = null');
329 expect((_.rootElement as dom.InputElement).value).toEqual('');
330 }));
331
332 it('should update model from the input value', inject(() {
333 _.compile('<input type="email" ng-model="model" probe="p">');
334 Probe probe = _.rootScope.p;
335 var ngModel = probe.directive(NgModel);
336 InputElement inputElement = probe.element;
337
338 // Valid value
339 inputElement.value = 'foo@example.com';
340 _.triggerEvent(inputElement, 'change');
341 expect(_.rootScope.model).toEqual('foo@example.com');
342
343 // Invalid value
344 inputElement.value = 'foo';
345 var input = probe.directive(InputEmailDirective);
346 input.processValue();
347 expect(_.rootScope.model).toEqual(null);
348 }));
349 });
350
351 describe('type="url"', () {
352 it('should update input value from model', inject(() {
353 _.compile('<input type="url" ng-model="model">');
354 _.rootScope.$digest();
355
356 expect((_.rootElement as dom.InputElement).value).toEqual('');
357
358 // Invalid e-mail
359 _.rootScope.$apply('model = "foo"');
360 expect((_.rootElement as dom.InputElement).value).toEqual('');
361
362 // Valid e-mail
363 _.rootScope.$apply('model = "http://www.google.com"');
364 expect((_.rootElement as dom.InputElement).value).toEqual(
365 'http://www.google.com');
366 }));
367
368 it('should render null as the empty string', inject(() {
369 _.compile('<input type="url" ng-model="model">');
370 _.rootScope.$digest();
371
372 expect((_.rootElement as dom.InputElement).value).toEqual('');
373
374 _.rootScope.$apply('model = null');
375 expect((_.rootElement as dom.InputElement).value).toEqual('');
376 }));
377
378 it('should update model from the input value', inject(() {
379 _.compile('<input type="url" ng-model="model" probe="p">');
380 Probe probe = _.rootScope.p;
381 var ngModel = probe.directive(NgModel);
382 InputElement inputElement = probe.element;
383
384 // Valid value
385 inputElement.value = 'http://www.google.com';
386 _.triggerEvent(inputElement, 'change');
387 expect(_.rootScope.model).toEqual('http://www.google.com');
388
389 // Invalid value
390 inputElement.value = 'foo';
391 var input = probe.directive(InputUrlDirective);
392 input.processValue();
393 expect(_.rootScope.model).toEqual(null);
394 }));
395 });
396
397 describe('type="radio"', () { 255 describe('type="radio"', () {
398 it('should update input value from model', inject(() { 256 it('should update input value from model', inject(() {
399 _.compile('<input type="radio" name="color" value="red" ng-model="color" p robe="r">' + 257 _.compile('<input type="radio" name="color" value="red" ng-model="color" p robe="r">' +
400 '<input type="radio" name="color" value="green" ng-model="color" probe="g">' + 258 '<input type="radio" name="color" value="green" ng-model="color" probe="g">' +
401 '<input type="radio" name="color" value="blue" ng-model="color" probe="b">'); 259 '<input type="radio" name="color" value="blue" ng-model="color" probe="b">');
402 _.rootScope.$digest(); 260 _.rootScope.$digest();
403 261
404 RadioButtonInputElement redBtn = _.rootScope.r.element; 262 RadioButtonInputElement redBtn = _.rootScope.r.element;
405 RadioButtonInputElement greenBtn = _.rootScope.g.element; 263 RadioButtonInputElement greenBtn = _.rootScope.g.element;
406 RadioButtonInputElement blueBtn = _.rootScope.b.element; 264 RadioButtonInputElement blueBtn = _.rootScope.b.element;
(...skipping 24 matching lines...) Expand all
431 expect(greenBtn.checked).toBe(false); 289 expect(greenBtn.checked).toBe(false);
432 expect(blueBtn.checked).toBe(false); 290 expect(blueBtn.checked).toBe(false);
433 291
434 _.triggerEvent(greenBtn, 'click'); 292 _.triggerEvent(greenBtn, 'click');
435 expect(_.rootScope['color']).toEqual('green'); 293 expect(_.rootScope['color']).toEqual('green');
436 expect(redBtn.checked).toBe(false); 294 expect(redBtn.checked).toBe(false);
437 expect(greenBtn.checked).toBe(true); 295 expect(greenBtn.checked).toBe(true);
438 expect(blueBtn.checked).toBe(false); 296 expect(blueBtn.checked).toBe(false);
439 })); 297 }));
440 }); 298 });
299
300 describe('contenteditable', () {
301 it('should update content from model', inject(() {
302 _.compile('<p contenteditable ng-model="model">');
303 _.rootScope.$digest();
304
305 expect(_.rootElement.text).toEqual('');
306
307 _.rootScope.$apply('model = "misko"');
308 expect(_.rootElement.text).toEqual('misko');
309 }));
310
311 it('should update model from the input value', inject(() {
312 _.compile('<p contenteditable ng-model="model">');
313 Element element = _.rootElement;
314
315 element.innerHtml = 'abc';
316 _.triggerEvent(element, 'change');
317 expect(_.rootScope.model).toEqual('abc');
318
319 element.innerHtml = 'def';
320 var input = ngInjector(element).get(ContentEditableDirective);
321 input.processValue();
322 expect(_.rootScope.model).toEqual('def');
323 }));
324 });
325
326 describe('pristine / dirty', () {
327 it('should be set to pristine by default', inject((Scope scope) {
328 _.compile('<input type="text" ng-model="my_model" probe="i" />');
329 Probe probe = _.rootScope.i;
330 var model = probe.directive(NgModel);
331
332 expect(model.pristine).toEqual(true);
333 expect(model.dirty).toEqual(false);
334 }));
335
336 it('should add and remove the correct CSS classes when set to dirty and to p ristine', inject((Scope scope) {
337 _.compile('<input type="text" ng-model="my_model" probe="i" />');
338 Probe probe = _.rootScope.i;
339 var model = probe.directive(NgModel);
340 InputElement element = probe.element;
341
342 model.dirty = true;
343 expect(model.pristine).toEqual(false);
344 expect(model.dirty).toEqual(true);
345 expect(element.classes.contains('ng-pristine')).toBe(false);
346 expect(element.classes.contains('ng-dirty')).toBe(true);
347
348 model.pristine = true;
349 expect(model.pristine).toEqual(true);
350 expect(model.dirty).toEqual(false);
351 expect(element.classes.contains('ng-pristine')).toBe(true);
352 expect(element.classes.contains('ng-dirty')).toBe(false);
353 }));
354 });
355
356 describe('valid / invalid', () {
357 it('should add and remove the correct flags when set to valid and to invalid ', inject((Scope scope) {
358 _.compile('<input type="text" ng-model="my_model" probe="i" />');
359 Probe probe = _.rootScope.i;
360 var model = probe.directive(NgModel);
361 InputElement element = probe.element;
362
363 model.invalid = true;
364 expect(model.valid).toEqual(false);
365 expect(model.invalid).toEqual(true);
366 expect(element.classes.contains('ng-valid')).toBe(false);
367 expect(element.classes.contains('ng-invalid')).toBe(true);
368
369 model.valid = true;
370 expect(model.valid).toEqual(true);
371 expect(model.invalid).toEqual(false);
372 expect(element.classes.contains('ng-invalid')).toBe(false);
373 expect(element.classes.contains('ng-valid')).toBe(true);
374 }));
375
376 it('should set the validity with respect to all existing validations when se tValidity() is used', inject((Scope scope) {
377 _.compile('<input type="text" ng-model="my_model" probe="i" />');
378 Probe probe = _.rootScope.i;
379 var model = probe.directive(NgModel);
380
381 model.setValidity("required", false);
382 expect(model.valid).toEqual(false);
383 expect(model.invalid).toEqual(true);
384
385 model.setValidity("format", false);
386 expect(model.valid).toEqual(false);
387 expect(model.invalid).toEqual(true);
388
389 model.setValidity("format", true);
390 expect(model.valid).toEqual(false);
391 expect(model.invalid).toEqual(true);
392
393 model.setValidity("required", true);
394 expect(model.valid).toEqual(true);
395 expect(model.invalid).toEqual(false);
396 }));
397
398 it('should register each error only once when invalid', inject((Scope scope) {
399 _.compile('<input type="text" ng-model="my_model" probe="i" />');
400 Probe probe = _.rootScope.i;
401 var model = probe.directive(NgModel);
402
403 model.setValidity("distinct-error", false);
404 expect(model.valid).toEqual(false);
405 expect(model.invalid).toEqual(true);
406
407 model.setValidity("distinct-error", false);
408 expect(model.valid).toEqual(false);
409 expect(model.invalid).toEqual(true);
410
411 model.setValidity("distinct-error", true);
412 expect(model.valid).toEqual(true);
413 expect(model.invalid).toEqual(false);
414 }));
415 });
441 416
442 }); 417 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698