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

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

Issue 124053002: Adding Angular and dependent packages for testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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 library input_select_spec;
2
3 import '../_specs.dart';
4
5 //TODO(misko): re-enabled dissabled tests once we have forms.
6
7 main() {
8 describe('input-select', () {
9
10 describe('ng-value', () {
11 TestBed _;
12 beforeEach(inject((TestBed tb) => _ = tb));
13
14 it('should retrieve using ng-value', () {
15 _.compile(
16 '<select ng-model="robot" probe="p">'
17 '<option ng-repeat="r in robots" ng-value="r">{{r.name}}</option>'
18 '</select>');
19 var r2d2 = { "name":"r2d2"};
20 var c3p0 = {"name":"c3p0"};
21 _.rootScope.robots = [ r2d2, c3p0 ];
22 _.rootScope.$digest();
23 _.selectOption(_.rootElement, 'c3p0');
24 expect(_.rootScope.robot).toEqual(c3p0);
25
26 _.rootScope.robot = r2d2;
27 _.rootScope.$digest();
28 expect(_.rootScope.robot).toEqual(r2d2);
29 expect(_.rootElement).toEqualSelect([['r2d2'], 'c3p0']);
30 });
31
32 it('should retrieve using ng-value', () {
33 _.compile(
34 '<select ng-model="robot" probe="p" multiple>'
35 '<option ng-repeat="r in robots" ng-value="r">{{r.name}}</option>'
36 '</select>');
37 var r2d2 = { "name":"r2d2"};
38 var c3p0 = {"name":"c3p0"};
39 _.rootScope.robots = [ r2d2, c3p0 ];
40 _.rootScope.$digest();
41 _.selectOption(_.rootElement, 'c3p0');
42 expect(_.rootScope.robot).toEqual([c3p0]);
43
44 _.rootScope.robot = [r2d2];
45 _.rootScope.$digest();
46 expect(_.rootScope.robot).toEqual([r2d2]);
47 expect(_.rootElement).toEqualSelect([['r2d2'], 'c3p0']);
48 });
49 });
50
51 TestBed _;
52
53 beforeEach(inject((TestBed tb) => _ = tb));
54
55 describe('select-one', () {
56 it('should compile children of a select without a ngModel, but not create a model for it',
57 () {
58 _.compile(
59 '<select>'
60 '<option selected="true">{{a}}</option>'
61 '<option value="">{{b}}</option>'
62 '<option>C</option>'
63 '</select>');
64 _.rootScope.$apply(() {
65 _.rootScope['a'] = 'foo';
66 _.rootScope['b'] = 'bar';
67 });
68
69 expect(_.rootElement.text).toEqual('foobarC');
70 });
71
72 it('should not interfere with selection via selected attr if ngModel direc tive is not present',
73 () {
74 _.compile(
75 '<select>'
76 '<option>not me</option>'
77 '<option selected>me!</option>'
78 '<option>nah</option>'
79 '</select>');
80 _.rootScope.$digest();
81
82 expect(_.rootElement).toEqualSelect(['not me', ['me!'], 'nah']);
83 });
84
85 it('should work with repeated value options', () {
86 _.compile(
87 '<select ng-model="robot" probe="p">'
88 '<option ng-repeat="r in robots">{{r}}</option>'
89 '</select>');
90
91 _.rootScope['robots'] = ['c3p0', 'r2d2'];
92 _.rootScope['robot'] = 'r2d2';
93 _.rootScope.$apply();
94
95 var select = _.rootScope['p'].directive(InputSelectDirective);
96 expect(_.rootElement).toEqualSelect(['c3p0', ['r2d2']]);
97
98 _.rootElement.querySelectorAll('option')[0].selected = true;
99 _.triggerEvent(_.rootElement, 'change');
100
101
102 expect(_.rootElement).toEqualSelect([['c3p0'], 'r2d2']);
103 expect(_.rootScope['robot']).toEqual('c3p0');
104
105 _.rootScope.$apply(() {
106 _.rootScope['robots'].insert(0, 'wallee');
107 });
108 expect(_.rootElement).toEqualSelect(['wallee', ['c3p0'], 'r2d2']);
109 expect(_.rootScope['robot']).toEqual('c3p0');
110
111 _.rootScope.$apply(() {
112 _.rootScope['robots'] = ['c3p0+', 'r2d2+'];
113 _.rootScope['robot'] = 'r2d2+';
114 });
115
116 expect(_.rootElement).toEqualSelect(['c3p0+', ['r2d2+']]);
117 expect(_.rootScope['robot']).toEqual('r2d2+');
118 });
119
120 describe('empty option', () {
121 it('should select the empty option when model is undefined', () {
122 _.compile(
123 '<select ng-model="robot">' +
124 '<option value="">--select--</option>' +
125 '<option value="x">robot x</option>' +
126 '<option value="y">robot y</option>' +
127 '</select>');
128 _.rootScope.$digest();
129
130 expect(_.rootElement).toEqualSelect([[''], 'x', 'y']);
131 });
132
133 it('should support defining an empty option anywhere in the option list' , () {
134 _.compile(
135 '<select ng-model="robot">' +
136 '<option value="x">robot x</option>' +
137 '<option value="">--select--</option>' +
138 '<option value="y">robot y</option>' +
139 '</select>');
140 _.rootScope.$digest();
141
142 expect(_.rootElement).toEqualSelect(['x', [''], 'y']);
143 });
144
145 it('should set the model to empty string when empty option is selected', () {
146 _.rootScope['robot'] = 'x';
147 _.compile(
148 '<select ng-model="robot" probe="p">' +
149 '<option value="">--select--</option>' +
150 '<option value="x">robot x</option>' +
151 '<option value="y">robot y</option>' +
152 '</select>');
153 _.rootScope.$digest();
154
155 var select = _.rootScope['p'].directive(InputSelectDirective);
156
157 expect(_.rootElement).toEqualSelect(['', ['x'], 'y']);
158
159 _.selectOption(_.rootElement, '--select--');
160
161 expect(_.rootElement).toEqualSelect([[''], 'x', 'y']);
162 expect(_.rootScope['robot']).toEqual(null);
163 });
164
165 describe('interactions with repeated options', () {
166 it('should select empty option when model is undefined', () {
167 _.rootScope['robots'] = ['c3p0', 'r2d2'];
168 _.compile(
169 '<select ng-model="robot">' +
170 '<option value="">--select--</option>' +
171 '<option ng-repeat="r in robots">{{r}}</option>' +
172 '</select>');
173 _.rootScope.$digest();
174 expect(_.rootElement).toEqualSelect([[''], 'c3p0', 'r2d2']);
175 });
176
177 it('should set model to empty string when selected', () {
178 _.rootScope['robots'] = ['c3p0', 'r2d2'];
179 _.compile(
180 '<select ng-model="robot" probe="p">' +
181 '<option value="">--select--</option>' +
182 '<option ng-repeat="r in robots">{{r}}</option>' +
183 '</select>');
184 _.rootScope.$digest();
185 var select = _.rootScope['p'].directive(InputSelectDirective);
186
187 _.selectOption(_.rootElement, 'c3p0');
188 expect(_.rootElement).toEqualSelect(['', ['c3p0'], 'r2d2']);
189 expect( _.rootScope['robot']).toEqual('c3p0');
190
191 _.selectOption(_.rootElement, '--select--');
192
193 expect(_.rootElement).toEqualSelect([[''], 'c3p0', 'r2d2']);
194 expect( _.rootScope['robot']).toEqual(null);
195 });
196
197 it('should not break if both the select and repeater models change at once', () {
198 _.compile(
199 '<select ng-model="robot">' +
200 '<option value="">--select--</option>' +
201 '<option ng-repeat="r in robots">{{r}}</option>' +
202 '</select>');
203 _.rootScope.$apply(() {
204 _.rootScope['robots'] = ['c3p0', 'r2d2'];
205 _.rootScope['robot'] = 'c3p0';
206 });
207
208 expect(_.rootElement).toEqualSelect(['', ['c3p0'], 'r2d2']);
209
210 _.rootScope.$apply(() {
211 _.rootScope['robots'] = ['wallee'];
212 _.rootScope['robot'] = '';
213 });
214
215 expect(_.rootElement).toEqualSelect([[''], 'wallee']);
216 });
217 });
218
219 describe('unknown option', () {
220
221 it("should insert&select temporary unknown option when no options-mode l match", () {
222 _.compile(
223 '<select ng-model="robot">' +
224 '<option>c3p0</option>' +
225 '<option>r2d2</option>' +
226 '</select>');
227 _.rootScope.$digest();
228 expect(_.rootElement).toEqualSelect([['?'], 'c3p0', 'r2d2']);
229
230 _.rootScope.$apply(() {
231 _.rootScope['robot'] = 'r2d2';
232 });
233 expect(_.rootElement).toEqualSelect(['c3p0', ['r2d2']]);
234
235
236 _.rootScope.$apply(() {
237 _.rootScope['robot'] = "wallee";
238 });
239 expect(_.rootElement).toEqualSelect([['?'], 'c3p0', 'r2d2']);
240 });
241
242 it("should NOT insert temporary unknown option when model is undefined and empty " +
243 "options is present", () {
244 _.compile(
245 '<select ng-model="robot">' +
246 '<option value="">--select--</option>' +
247 '<option>c3p0</option>' +
248 '<option>r2d2</option>' +
249 '</select>');
250 _.rootScope.$digest();
251
252 expect(_.rootElement).toEqualSelect([[''], 'c3p0', 'r2d2']);
253 expect(_.rootScope['robot']).toEqual(null);
254
255 _.rootScope.$apply(() {
256 _.rootScope['robot'] = 'wallee';
257 });
258 expect(_.rootElement).toEqualSelect([['?'], '', 'c3p0', 'r2d2']);
259
260 _.rootScope.$apply(() {
261 _.rootScope['robot'] = 'r2d2';
262 });
263 expect(_.rootElement).toEqualSelect(['', 'c3p0', ['r2d2']]);
264
265 _.rootScope.$apply(() {
266 _.rootScope['robot'] = null;
267 });
268 expect(_.rootElement).toEqualSelect([[''], 'c3p0', 'r2d2']);
269 });
270
271 it("should insert&select temporary unknown option when no options-mode l match, empty " +
272 "option is present and model is defined", () {
273 _.rootScope['robot'] = 'wallee';
274 _.compile(
275 '<select ng-model="robot">' +
276 '<option value="">--select--</option>' +
277 '<option>c3p0</option>' +
278 '<option>r2d2</option>' +
279 '</select>');
280 _.rootScope.$digest();
281
282 expect(_.rootElement).toEqualSelect([['?'], '', 'c3p0', 'r2d2']);
283
284 _.rootScope.$apply(() {
285 _.rootScope['robot'] = 'r2d2';
286 });
287 expect(_.rootElement).toEqualSelect(['', 'c3p0', ['r2d2']]);
288 });
289
290 describe('interactions with repeated options', () {
291 it('should work with repeated options', () {
292 _.rootScope['robots'] = [];
293 _.compile(
294 '<select ng-model="robot">' +
295 '<option ng-repeat="r in robots">{{r}}</option>' +
296 '</select>');
297 _.rootScope.$apply(() {
298 _.rootScope['robots'] = [];
299 });
300
301 expect(_.rootElement).toEqualSelect([['?']]);
302 expect(_.rootScope['robot']).toEqual(null);
303
304 _.rootScope.$apply(() {
305 _.rootScope['robot'] = 'r2d2';
306 });
307 expect(_.rootElement).toEqualSelect([['?']]);
308 expect(_.rootScope['robot']).toEqual('r2d2');
309
310 _.rootScope.$apply(() {
311 _.rootScope['robots'] = ['c3p0', 'r2d2'];
312 });
313 expect(_.rootElement).toEqualSelect(['c3p0', ['r2d2']]);
314 expect(_.rootScope['robot']).toEqual('r2d2');
315 });
316
317 it('should work with empty option and repeated options', () {
318 _.compile(
319 '<select ng-model="robot">' +
320 '<option value="">--select--</option>' +
321 '<option ng-repeat="r in robots">{{r}}</option>' +
322 '</select>');
323 _.rootScope.$apply(() {
324 _.rootScope['robots'] = [];
325 });
326
327 expect(_.rootElement).toEqualSelect([['']]);
328 expect(_.rootScope['robot']).toEqual(null);
329
330 _.rootScope.$apply(() {
331 _.rootScope['robot'] = 'r2d2';
332 });
333 expect(_.rootElement).toEqualSelect([['?'], '']);
334 expect(_.rootScope['robot']).toEqual('r2d2');
335
336 _.rootScope.$apply(() {
337 _.rootScope['robots'] = ['c3p0', 'r2d2'];
338 });
339 expect(_.rootElement).toEqualSelect(['', 'c3p0', ['r2d2']]);
340 expect(_.rootScope['robot']).toEqual('r2d2');
341 });
342
343 it('should insert unknown element when repeater shrinks and selected option is ' +
344 'unavailable', () {
345
346 _.compile(
347 '<select ng-model="robot">' +
348 '<option ng-repeat="r in robots">{{r}}</option>' +
349 '</select>');
350 _.rootScope.$apply(() {
351 _.rootScope['robots'] = ['c3p0', 'r2d2'];
352 _.rootScope['robot'] = 'r2d2';
353 });
354 expect(_.rootElement).toEqualSelect(['c3p0', ['r2d2']]);
355 expect(_.rootScope['robot']).toEqual('r2d2');
356
357 _.rootScope.$apply(() {
358 _.rootScope['robots'].remove('r2d2');
359 });
360 expect(_.rootScope['robot']).toEqual('r2d2');
361 expect(_.rootElement).toEqualSelect([['?'], 'c3p0']);
362
363 _.rootScope.$apply(() {
364 _.rootScope['robots'].insert(0, 'r2d2');
365 });
366 expect(_.rootElement).toEqualSelect([['r2d2'], 'c3p0']);
367 expect(_.rootScope['robot']).toEqual('r2d2');
368
369 _.rootScope.$apply(() {
370 _.rootScope['robots'].clear();
371 });
372
373 expect(_.rootElement).toEqualSelect([['?']]);
374 expect(_.rootScope['robot']).toEqual('r2d2');
375 });
376 });
377 });
378 });
379 });
380
381
382 describe('select from angular.js', () {
383 TestBed _;
384 beforeEach(inject((TestBed tb) => _ = tb));
385
386 var scope, formElement, element;
387
388 compile(html) {
389 _.compile('<form name="form">' + html + '</form>');
390 element = _.rootElement.querySelector('select');
391 scope.$apply();
392 }
393
394 beforeEach(inject((Scope rootScope) {
395 scope = rootScope;
396 formElement = element = null;
397 }));
398
399
400 afterEach(() {
401 scope.$destroy(); //disables unknown option work during destruction
402 });
403
404
405 describe('select-one', () {
406
407 it('should compile children of a select without a ngModel, but not creat e a model for it',
408 () {
409 compile('<select>' +
410 '<option selected="true">{{a}}</option>' +
411 '<option value="">{{b}}</option>' +
412 '<option>C</option>' +
413 '</select>');
414 scope.$apply(() {
415 scope.a = 'foo';
416 scope.b = 'bar';
417 });
418
419 expect(element.text).toEqual('foobarC');
420 });
421
422
423 it('should not interfere with selection via selected attr if ngModel dir ective is not present',
424 () {
425 compile('<select>' +
426 '<option>not me</option>' +
427 '<option selected>me!</option>' +
428 '<option>nah</option>' +
429 '</select>');
430 expect(element).toEqualSelect(['not me', ['me!'], 'nah']);
431 });
432
433
434 xit('should require', () {
435 compile(
436 '<select name="select" ng-model="selection" required ng-change="chan ge()">' +
437 '<option value=""></option>' +
438 '<option value="c">C</option>' +
439 '</select>');
440
441 scope.change = () {
442 scope.log += 'change;';
443 };
444
445 scope.$apply(() {
446 scope.log = '';
447 scope.selection = 'c';
448 });
449
450 expect(scope.form.select.$error.required).toEqual(false);;
451 expect(element).toEqualValid();
452 expect(element).toEqualPristine();
453
454 scope.$apply(() {
455 scope.selection = '';
456 });
457
458 expect(scope.form.select.$error.required).toEqual(true);;
459 expect(element).toEqualInvalid();
460 expect(element).toEqualPristine();
461 expect(scope.log).toEqual('');
462
463 element[0].value = 'c';
464 _.triggerEvent(element, 'change');
465 expect(element).toEqualValid();
466 expect(element).toEqualDirty();
467 expect(scope.log).toEqual('change;');
468 });
469
470
471 xit('should not be invalid if no require', () {
472 compile(
473 '<select name="select" ng-model="selection">' +
474 '<option value=""></option>' +
475 '<option value="c">C</option>' +
476 '</select>');
477
478 expect(element).toEqualValid();
479 expect(element).toEqualPristine();
480 });
481
482
483 describe('empty option', () {
484
485 it('should select the empty option when model is undefined', () {
486 compile('<select ng-model="robot">' +
487 '<option value="">--select--</option>' +
488 '<option value="x">robot x</option>' +
489 '<option value="y">robot y</option>' +
490 '</select>');
491
492 expect(element).toEqualSelect([[''], 'x', 'y']);
493 });
494
495
496 it('should support defining an empty option anywhere in the option lis t', () {
497 compile('<select ng-model="robot">' +
498 '<option value="x">robot x</option>' +
499 '<option value="">--select--</option>' +
500 '<option value="y">robot y</option>' +
501 '</select>');
502
503 expect(element).toEqualSelect(['x', [''], 'y']);
504 });
505 });
506 });
507
508
509 describe('select-multiple', () {
510
511 it('should support type="select-multiple"', () {
512 compile(
513 '<select ng-model="selection" multiple>' +
514 '<option>A</option>' +
515 '<option>B</option>' +
516 '</select>');
517
518 scope.$apply(() {
519 scope.selection = ['A'];
520 });
521
522 expect(element).toEqualSelect([['A'], 'B']);
523
524 scope.$apply(() {
525 scope.selection.add('B');
526 });
527
528 expect(element).toEqualSelect([['A'], ['B']]);
529 });
530
531 it('should work with optgroups', () {
532 compile('<select ng-model="selection" multiple>' +
533 '<optgroup label="group1">' +
534 '<option>A</option>' +
535 '<option>B</option>' +
536 '</optgroup>' +
537 '</select>');
538
539 expect(element).toEqualSelect(['A', 'B']);
540 expect(scope.selection).toEqual(null);
541
542 scope.$apply(() {
543 scope.selection = ['A'];
544 });
545 expect(element).toEqualSelect([['A'], 'B']);
546
547 scope.$apply(() {
548 scope.selection.add('B');
549 });
550 expect(element).toEqualSelect([['A'], ['B']]);
551 });
552
553 xit('should require', () {
554 compile(
555 '<select name="select" ng-model="selection" multiple required>' +
556 '<option>A</option>' +
557 '<option>B</option>' +
558 '</select>');
559
560 scope.$apply(() {
561 scope.selection = [];
562 });
563
564 expect(scope.form.select.$error.required).toEqual(true);;
565 expect(element).toEqualInvalid();
566 expect(element).toEqualPristine();
567
568 scope.$apply(() {
569 scope.selection = ['A'];
570 });
571
572 expect(element).toEqualValid();
573 expect(element).toEqualPristine();
574
575 element[0].value = 'B';
576 _.triggerEvent(element, 'change');
577 expect(element).toEqualValid();
578 expect(element).toEqualDirty();
579 });
580 });
581
582
583 describe('ngOptions', () {
584 createSelect(attrs, [blank, unknown, ngRepeat, text, ngValue]) {
585 var html = '<select';
586 attrs.forEach((key, value) {
587 if (value is bool) {
588 if (value != null) html += ' $key';
589 } else {
590 html += ' $key="$value"';
591 }
592 });
593 html += '>' +
594 (blank != null ? (blank is String ? blank : '<option value="">blank< /option>') : '') +
595 (unknown != null ? (unknown is String ? unknown : '<option value="?" >unknown</option>') : '') +
596 (ngRepeat != null ? '<option ng-repeat="$ngRepeat" ng-value="$ngValu e">{{$text}}</option>' : '') +
597 '</select>';
598
599 compile(html);
600 }
601
602 createSingleSelect([blank, unknown]) {
603 createSelect({
604 'ng-model':'selected'
605 }, blank, unknown, 'value in values', 'value.name', 'value');
606 }
607
608 createMultiSelect([blank, unknown]) {
609 createSelect({
610 'ng-model':'selected',
611 'multiple':true
612 }, blank, unknown, 'value in values', 'value.name', 'value');
613 }
614
615
616 it('should render a list', () {
617 createSingleSelect();
618
619 scope.$apply(() {
620 scope.values = [{'name': 'A'}, {'name': 'B'}, {'name': 'C'}];
621 scope.selected = scope.values[0];
622 });
623
624 var options = element.querySelectorAll('option');
625 expect(options.length).toEqual(3);
626 expect(element).toEqualSelect([['A'], 'B', 'C']);
627 });
628
629 it('should render zero as a valid display value', () {
630 createSingleSelect();
631
632 scope.$apply(() {
633 scope.values = [{'name': '0'}, {'name': '1'}, {'name': '2'}];
634 scope.selected = scope.values[0];
635 });
636
637 var options = element.querySelectorAll('option');
638 expect(options.length).toEqual(3);
639 expect(element).toEqualSelect([['0'], '1', '2']);
640 });
641
642 it('should grow list', () {
643 createSingleSelect();
644
645 scope.$apply(() {
646 scope.values = [];
647 });
648
649 expect(element.querySelectorAll('option').length).toEqual(1); // becau se we add special empty option
650 expect(element.querySelectorAll('option')[0].text).toEqual('');
651 expect(element.querySelectorAll('option')[0].value).toEqual('?');
652
653 scope.$apply(() {
654 scope.values.add({'name':'A'});
655 scope.selected = scope.values[0];
656 });
657
658 expect(element.querySelectorAll('option').length).toEqual(1);
659 expect(element).toEqualSelect([['A']]);
660
661 scope.$apply(() {
662 scope.values.add({'name':'B'});
663 });
664
665 expect(element.querySelectorAll('option').length).toEqual(2);
666 expect(element).toEqualSelect([['A'], 'B']);
667 });
668
669
670 it('should shrink list', () {
671 createSingleSelect();
672
673 scope.$apply(() {
674 scope.values = [{'name':'A'}, {'name':'B'}, {'name':'C'}];
675 scope.selected = scope.values[0];
676 });
677
678 expect(element.querySelectorAll('option').length).toEqual(3);
679
680 scope.$apply(() {
681 scope.values.removeLast();
682 });
683
684 expect(element.querySelectorAll('option').length).toEqual(2);
685 expect(element).toEqualSelect([['A'], 'B']);
686
687 scope.$apply(() {
688 scope.values.removeLast();
689 });
690
691 expect(element.querySelectorAll('option').length).toEqual(1);
692 expect(element).toEqualSelect([['A']]);
693
694 scope.$apply(() {
695 scope.values.removeLast();
696 scope.selected = null;
697 });
698
699 expect(element.querySelectorAll('option').length).toEqual(1); // we ad d back the special empty option
700 });
701
702
703 it('should shrink and then grow list', () {
704 createSingleSelect();
705
706 scope.$apply(() {
707 scope.values = [{'name':'A'}, {'name':'B'}, {'name':'C'}];
708 scope.selected = scope.values[0];
709 });
710
711 expect(element.querySelectorAll('option').length).toEqual(3);
712
713 scope.$apply(() {
714 scope.values = [{'name': '1'}, {'name': '2'}];
715 scope.selected = scope.values[0];
716 });
717
718 expect(element.querySelectorAll('option').length).toEqual(2);
719
720 scope.$apply(() {
721 scope.values = [{'name': 'A'}, {'name': 'B'}, {'name': 'C'}];
722 scope.selected = scope.values[0];
723 });
724
725 expect(element.querySelectorAll('option').length).toEqual(3);
726 });
727
728
729 it('should update list', () {
730 createSingleSelect();
731
732 scope.$apply(() {
733 scope.values = [{'name': 'A'}, {'name': 'B'}, {'name': 'C'}];
734 scope.selected = scope.values[0];
735 });
736 expect(element).toEqualSelect([['A'], 'B', 'C']);
737 scope.$apply(() {
738 scope.values = [{'name': 'B'}, {'name': 'C'}, {'name': 'D'}];
739 scope.selected = scope.values[0];
740 });
741
742 var options = element.querySelectorAll('option');
743 expect(options.length).toEqual(3);
744 expect(element).toEqualSelect([['B'], 'C', 'D']);
745 });
746
747
748 it('should preserve existing options', () {
749 createSingleSelect(true);
750
751 scope.$apply(() {
752 scope.values = [];
753 });
754
755 expect(element.querySelectorAll('option').length).toEqual(1);
756
757 scope.$apply(() {
758 scope.values = [{'name': 'A'}];
759 scope.selected = scope.values[0];
760 });
761
762 expect(element.querySelectorAll('option').length).toEqual(2);
763 expect(element.querySelectorAll('option')[0].text).toEqual('blank');
764 expect(element.querySelectorAll('option')[1].text).toEqual('A');
765
766 scope.$apply(() {
767 scope.values = [];
768 scope.selected = null;
769 });
770
771 expect(element.querySelectorAll('option').length).toEqual(1);
772 expect(element.querySelectorAll('option')[0].text).toEqual('blank');
773 });
774
775 describe('binding', () {
776
777 it('should bind to scope value', () {
778 createSingleSelect();
779
780 scope.$apply(() {
781 scope.values = [{'name': 'A'}, {'name': 'B'}];
782 scope.selected = scope.values[0];
783 });
784
785 expect(element).toEqualSelect([['A'], 'B']);
786
787 scope.$apply(() {
788 scope.selected = scope.values[1];
789 });
790
791 expect(element).toEqualSelect(['A', ['B']]);
792 });
793
794
795 // TODO(misko): re-enable once we support group by
796 xit('should bind to scope value and group', () {
797 createSelect({
798 'ng-model': 'selected',
799 'ng-options': 'item.name group by item.group for item in values'
800 });
801
802 scope.$apply(() {
803 scope.values = [{'name': 'A'},
804 {'name': 'B', group: 'first'},
805 {'name': 'C', group: 'second'},
806 {'name': 'D', group: 'first'},
807 {'name': 'E', group: 'second'}];
808 scope.selected = scope.values[3];
809 });
810
811 expect(element).toEqualSelect(['A', 'B', ['D'], 'C', 'E']);
812
813 var first = element.querySelectorAll('optgroup')[0];
814 var b = first.querySelectorAll('option')[0];
815 var d = first.querySelectorAll('option')[1];
816 expect(first.attr('label')).toEqual('first');
817 expect(b.text).toEqual('B');
818 expect(d.text).toEqual('D');
819
820 var second = element.querySelectorAll('optgroup')[1];
821 var c = second.querySelectorAll('option')[0];
822 var e = second.querySelectorAll('option')[1];
823 expect(second.attr('label')).toEqual('second');
824 expect(c.text).toEqual('C');
825 expect(e.text).toEqual('E');
826
827 scope.$apply(() {
828 scope.selected = scope.values[0];
829 });
830
831 expect(element.value).toEqual('0');
832 });
833
834
835 it('should bind to scope value through experession', () {
836 createSelect({'ng-model': 'selected'}, null, null, 'item in values', 'item.name', 'item.id');
837
838 scope.$apply(() {
839 scope.values = [{'id': 10, 'name': 'A'}, {'id': 20, 'name': 'B'}];
840 scope.selected = scope.values[0]['id'];
841 });
842
843 expect(element).toEqualSelect([['A'], 'B']);
844
845 scope.$apply(() {
846 scope.selected = scope.values[1]['id'];
847 });
848
849 expect(element).toEqualSelect(['A', ['B']]);
850 });
851
852
853 it('should insert a blank option if bound to null', () {
854 createSingleSelect();
855
856 scope.$apply(() {
857 scope.values = [{'name': 'A'}];
858 scope.selected = null;
859 });
860
861 expect(element.querySelectorAll('option').length).toEqual(2);
862 expect(element).toEqualSelect([['?'], 'A']);
863 expect(element.querySelectorAll('option')[0].value).toEqual('?');
864
865 scope.$apply(() {
866 scope.selected = scope.values[0];
867 });
868
869 expect(element).toEqualSelect([['A']]);
870 expect(element.querySelectorAll('option').length).toEqual(1);
871 });
872
873
874 it('should reuse blank option if bound to null', () {
875 createSingleSelect(true);
876
877 scope.$apply(() {
878 scope.values = [{'name': 'A'}];
879 scope.selected = null;
880 });
881
882 expect(element.querySelectorAll('option').length).toEqual(2);
883 expect(element.value).toEqual('');
884 expect(element.querySelectorAll('option')[0].value).toEqual('');
885
886 scope.$apply(() {
887 scope.selected = scope.values[0];
888 });
889
890 expect(element).toEqualSelect(['', ['A']]);
891 expect(element.querySelectorAll('option').length).toEqual(2);
892 });
893
894
895 it('should insert a unknown option if bound to something not in the li st', () {
896 createSingleSelect();
897
898 scope.$apply(() {
899 scope.values = [{'name': 'A'}];
900 scope.selected = {};
901 });
902
903 expect(element.querySelectorAll('option').length).toEqual(2);
904 expect(element.value).toEqual('?');
905 expect(element.querySelectorAll('option')[0].value).toEqual('?');
906
907 scope.$apply(() {
908 scope.selected = scope.values[0];
909 });
910
911 expect(element).toEqualSelect([['A']]);
912 expect(element.querySelectorAll('option').length).toEqual(1);
913 });
914
915
916 it('should select correct input if previously selected option was "?"' , () {
917 createSingleSelect();
918
919 scope.$apply(() {
920 scope.values = [{'name': 'A'}, {'name': 'B'}];
921 scope.selected = {};
922 });
923
924 expect(element.querySelectorAll('option').length).toEqual(3);
925 expect(element.value).toEqual('?');
926 expect(element.querySelectorAll('option')[0].value).toEqual('?');
927
928 _.selectOption(element, 'A');
929 expect(scope.selected).toBe(scope.values[0]);
930 expect(element.querySelectorAll('option')[0].selected).toEqual(true) ;
931 expect(element.querySelectorAll('option')[0].selected).toEqual(true) ;;
932 expect(element.querySelectorAll('option').length).toEqual(2);
933 });
934 });
935
936
937 describe('blank option', () {
938
939 it('should be compiled as template, be watched and updated', () {
940 var option;
941 createSingleSelect('<option value="">blank is {{blankVal}}</option>' );
942
943 scope.$apply(() {
944 scope.blankVal = 'so blank';
945 scope.values = [{'name': 'A'}];
946 });
947
948 // check blank option is first and is compiled
949 expect(element.querySelectorAll('option').length).toEqual(2);
950 option = element.querySelectorAll('option')[0];
951 expect(option.value).toEqual('');
952 expect(option.text).toEqual('blank is so blank');
953
954 scope.$apply(() {
955 scope.blankVal = 'not so blank';
956 });
957
958 // check blank option is first and is compiled
959 expect(element.querySelectorAll('option').length).toEqual(2);
960 option = element.querySelectorAll('option')[0];
961 expect(option.value).toEqual('');
962 expect(option.text).toEqual('blank is not so blank');
963 });
964
965
966 it('should support binding via ngBindTemplate directive', () {
967 var option;
968 createSingleSelect('<option value="" ng-bind="\'blank is \' + blankV al"></option>');
969
970 scope.$apply(() {
971 scope.blankVal = 'so blank';
972 scope.values = [{'name': 'A'}];
973 });
974
975 // check blank option is first and is compiled
976 expect(element.querySelectorAll('option').length).toEqual(2);
977 option = element.querySelectorAll('option')[0];
978 expect(option.value).toEqual('');
979 expect(option.text).toEqual('blank is so blank');
980 });
981
982
983 it('should support biding via ngBind attribute', () {
984 var option;
985 createSingleSelect('<option value="" ng-bind="blankVal"></option>');
986
987 scope.$apply(() {
988 scope.blankVal = 'is blank';
989 scope.values = [{'name': 'A'}];
990 });
991
992 // check blank option is first and is compiled
993 expect(element.querySelectorAll('option').length).toEqual(2);
994 option = element.querySelectorAll('option')[0];
995 expect(option.value).toEqual('');
996 expect(option.text).toEqual('is blank');
997 });
998
999
1000 it('should be rendered with the attributes preserved', () {
1001 var option;
1002 createSingleSelect('<option value="" class="coyote" id="road-runner" ' +
1003 'custom-attr="custom-attr">{{blankVal}}</option>');
1004
1005 scope.$apply(() {
1006 scope.blankVal = 'is blank';
1007 });
1008
1009 // check blank option is first and is compiled
1010 option = element.querySelectorAll('option')[0];
1011 expect(option.classes.contains('coyote')).toEqual(true);;
1012 expect(option.attributes['id']).toEqual('road-runner');
1013 expect(option.attributes['custom-attr']).toEqual('custom-attr');
1014 });
1015
1016 it('should be selected, if it is available and no other option is sele cted', () {
1017 // selectedIndex is used here because $ incorrectly reports element. value
1018 scope.$apply(() {
1019 scope.values = [{'name': 'A'}];
1020 });
1021 createSingleSelect(true);
1022 // ensure the first option (the blank option) is selected
1023 expect(element.selectedIndex).toEqual(0);
1024 scope.$digest();
1025 // ensure the option has not changed following the digest
1026 expect(element.selectedIndex).toEqual(0);
1027 });
1028 });
1029
1030
1031 describe('on change', () {
1032
1033 it('should update model on change', () {
1034 createSingleSelect();
1035
1036 scope.$apply(() {
1037 scope.values = [{'name': 'A'}, {'name': 'B'}];
1038 scope.selected = scope.values[0];
1039 });
1040
1041 expect(element.querySelectorAll('option')[0].selected).toEqual(true) ;
1042
1043 element.querySelectorAll('option')[1].selected = true;
1044 _.triggerEvent(element, 'change');
1045 expect(scope.selected).toEqual(scope.values[1]);
1046 });
1047
1048
1049 it('should update model on change through expression', () {
1050 createSelect({'ng-model': 'selected'}, null, null,
1051 'item in values', 'item.name', 'item.id');
1052
1053 scope.$apply(() {
1054 scope.values = [{'id': 10, 'name': 'A'}, {'id': 20, 'name': 'B'}];
1055 scope.selected = scope.values[0]['id'];
1056 });
1057
1058 expect(element).toEqualSelect([['A'], 'B']);
1059
1060 element.querySelectorAll('option')[1].selected = true;
1061 _.triggerEvent(element, 'change');
1062 expect(scope.selected).toEqual(scope.values[1]['id']);
1063 });
1064
1065
1066 it('should update model to null on change', () {
1067 createSingleSelect(true);
1068
1069 scope.$apply(() {
1070 scope.values = [{'name': 'A'}, {'name': 'B'}];
1071 scope.selected = scope.values[0];
1072 element.value = '0';
1073 });
1074
1075 _.selectOption(element, 'blank');
1076 expect(element).toEqualSelect([[''], 'A', 'B']);
1077
1078 expect(scope.selected).toEqual(null);
1079 });
1080 });
1081
1082
1083 describe('select-many', () {
1084
1085 it('should read multiple selection', () {
1086 createMultiSelect();
1087
1088 scope.$apply(() {
1089 scope.values = [{'name': 'A'}, {'name': 'B'}];
1090 scope.selected = [];
1091 });
1092
1093 expect(element.querySelectorAll('option').length).toEqual(2);
1094 expect(element.querySelectorAll('option')[0].selected).toEqual(false );;
1095 expect(element.querySelectorAll('option')[1].selected).toEqual(false );;
1096
1097 scope.$apply(() {
1098 scope.selected.add(scope.values[1]);
1099 });
1100
1101 expect(element.querySelectorAll('option').length).toEqual(2);
1102 expect(element.querySelectorAll('option')[0].selected).toEqual(false );;
1103 expect(element.querySelectorAll('option')[1].selected).toEqual(true) ;;
1104
1105 scope.$apply(() {
1106 scope.selected.add(scope.values[0]);
1107 });
1108
1109 expect(element.querySelectorAll('option').length).toEqual(2);
1110 expect(element.querySelectorAll('option')[0].selected).toEqual(true) ;;
1111 expect(element.querySelectorAll('option')[1].selected).toEqual(true) ;;
1112 });
1113
1114
1115 it('should update model on change', () {
1116 createMultiSelect();
1117
1118 scope.$apply(() {
1119 scope.values = [{'name': 'A'}, {'name': 'B'}];
1120 scope.selected = [];
1121 });
1122
1123 element.querySelectorAll('option')[0].selected = true;
1124
1125 _.triggerEvent(element, 'change');
1126 expect(scope.selected).toEqual([scope.values[0]]);
1127 });
1128
1129
1130 it('should deselect all options when model is emptied', () {
1131 createMultiSelect();
1132 scope.$apply(() {
1133 scope.values = [{'name': 'A'}, {'name': 'B'}];
1134 scope.selected = [scope.values[0]];
1135 });
1136 expect(element.querySelectorAll('option')[0].selected).toEqual(true) ;
1137
1138 scope.$apply(() {
1139 scope.selected.removeLast();
1140 });
1141
1142 expect(element.querySelectorAll('option')[0].selected).toEqual(false );
1143 });
1144 });
1145
1146
1147 xdescribe('ngRequired', () {
1148
1149 it('should allow bindings on ngRequired', () {
1150 createSelect({
1151 'ng-model': 'value',
1152 'ng-options': 'item.name for item in values',
1153 'ng-required': 'required'
1154 }, true);
1155
1156
1157 scope.$apply(() {
1158 scope.values = [{'name': 'A', 'id': 1}, {'name': 'B', 'id': 2}];
1159 scope.required = false;
1160 });
1161
1162 element.value = '';
1163 _.triggerEvent(element, 'change');
1164 expect(element).toEqualValid();
1165
1166 scope.$apply(() {
1167 scope.required = true;
1168 });
1169 expect(element).toEqualInvalid();
1170
1171 scope.$apply(() {
1172 scope.value = scope.values[0];
1173 });
1174 expect(element).toEqualValid();
1175
1176 element.value = '';
1177 _.triggerEvent(element, 'change');
1178 expect(element).toEqualInvalid();
1179
1180 scope.$apply(() {
1181 scope.required = false;
1182 });
1183 expect(element).toEqualValid();
1184 });
1185 });
1186 });
1187
1188
1189 describe('option', () {
1190
1191 it('should populate value attribute on OPTION', () {
1192 compile('<select ng-model="x"><option selected>abc</option></select>') ;
1193 expect(element).toEqualSelect([['?'], 'abc']);
1194 });
1195
1196 it('should ignore value if already exists', () {
1197 compile('<select ng-model="x"><option value="abc">xyz</option></select >');
1198 expect(element).toEqualSelect([['?'], 'abc']);
1199 });
1200
1201 it('should set value even if self closing HTML', () {
1202 scope.x = 'hello';
1203 compile('<select ng-model="x"><option>hello</select>');
1204 expect(element).toEqualSelect([['hello']]);
1205 });
1206
1207 it('should not blow up when option directive is found inside of a datali st',
1208 () {
1209 _.compile('<div>' +
1210 '<datalist><option>some val</option></datalist>' +
1211 '<span>{{foo}}</span>' +
1212 '</div>');
1213
1214 _.rootScope.foo = 'success';
1215 _.rootScope.$digest();
1216 expect(_.rootElement.querySelector('span').text).toEqual('success');
1217 });
1218 });
1219 });
1220 });
1221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698