1
1
'use strict' ;
2
2
3
3
describe ( 'select' , function ( ) {
4
- var scope , formElement , element , $compile ;
4
+ var scope , formElement , element , $compile , ngModelCtrl , selectCtrl , renderSpy ;
5
5
6
6
function compile ( html ) {
7
7
formElement = jqLite ( '<form name="form">' + html + '</form>' ) ;
@@ -10,10 +10,49 @@ describe('select', function() {
10
10
scope . $apply ( ) ;
11
11
}
12
12
13
+ function compileRepeatedOptions ( ) {
14
+ compile ( '<select ng-model="robot">' +
15
+ '<option value="{{item.value}}" ng-repeat="item in robots">{{item.label}}</option>' +
16
+ '</select>' ) ;
17
+ }
18
+
19
+ function compileGroupedOptions ( ) {
20
+ compile (
21
+ '<select ng-model="mySelect">' +
22
+ '<option ng-repeat="item in values">{{item.name}}</option>' +
23
+ '<optgroup ng-repeat="group in groups" label="{{group.name}}">' +
24
+ '<option ng-repeat="item in group.values">{{item.name}}</option>' +
25
+ '</optgroup>' +
26
+ '</select>' ) ;
27
+ }
28
+
13
29
function unknownValue ( value ) {
14
30
return '? ' + hashKey ( value ) + ' ?' ;
15
31
}
16
32
33
+ beforeEach ( module ( function ( $compileProvider ) {
34
+ $compileProvider . directive ( 'captureSelectCtrl' , function ( ) {
35
+ return {
36
+ require : 'select' ,
37
+ link : {
38
+ pre : function ( scope , element , attrs , ctrl ) {
39
+ selectCtrl = ctrl ;
40
+ renderSpy = jasmine . createSpy ( 'renderSpy' ) ;
41
+ dump ( 'fakePre' , selectCtrl . ngModelCtrl . $render ) ;
42
+ selectCtrl . ngModelCtrl . $render = renderSpy . andCallFake ( selectCtrl . ngModelCtrl . $render ) ;
43
+ spyOn ( selectCtrl , 'writeValue' ) . andCallThrough ( ) ;
44
+ } ,
45
+ post : function ( scope , element , attrs , ctrl ) {
46
+ dump ( 'fakePost' )
47
+ // expect(selectCtrl.ngModelCtrl.$render).not.toHaveBeenCalled();
48
+ // dump('fakePost', selectCtrl.ngModelCtrl.$render)
49
+ // spyOn(selectCtrl.ngModelCtrl, '$render').andCallThrough();
50
+ } ,
51
+ }
52
+ } ;
53
+ } ) ;
54
+ } ) ) ;
55
+
17
56
beforeEach ( inject ( function ( $rootScope , _$compile_ ) {
18
57
scope = $rootScope . $new ( ) ; //create a child scope because the root scope can't be $destroy-ed
19
58
$compile = _$compile_ ;
@@ -47,12 +86,14 @@ describe('select', function() {
47
86
toEqualSelectWithOptions : function ( expected ) {
48
87
var actualValues = { } ;
49
88
var optionGroup ;
89
+ var optionValue ;
50
90
51
91
forEach ( this . actual . find ( 'option' ) , function ( option ) {
52
92
optionGroup = option . parentNode . label || '' ;
53
93
actualValues [ optionGroup ] = actualValues [ optionGroup ] || [ ] ;
54
94
// IE9 doesn't populate the label property from the text property like other browsers
55
- actualValues [ optionGroup ] . push ( option . label || option . text ) ;
95
+ optionValue = option . label || option . text ;
96
+ actualValues [ optionGroup ] . push ( option . selected ? [ optionValue ] : optionValue ) ;
56
97
} ) ;
57
98
58
99
this . message = function ( ) {
@@ -198,6 +239,50 @@ describe('select', function() {
198
239
} ) ;
199
240
200
241
242
+ it ( 'should select options in a group when there is a linebreak before an option' , function ( ) {
243
+ scope . mySelect = 'B' ;
244
+ scope . $apply ( ) ;
245
+
246
+ var select = jqLite (
247
+ '<select ng-model="mySelect">' +
248
+ '<optgroup label="first">' +
249
+ '<option value="A">A</option>' +
250
+ '</optgroup>' +
251
+ '<optgroup label="second">' + '\n' +
252
+ '<option value="B">B</option>' +
253
+ '</optgroup> ' +
254
+ '</select>' ) ;
255
+
256
+ $compile ( select ) ( scope ) ;
257
+ scope . $apply ( ) ;
258
+
259
+ expect ( select ) . toEqualSelectWithOptions ( { 'first' :[ 'A' ] , 'second' : [ [ 'B' ] ] } ) ;
260
+ dealoc ( select ) ;
261
+ } ) ;
262
+
263
+
264
+ it ( 'should only call selectCtrl.writeValue after a digest has occured' , function ( ) {
265
+ scope . mySelect = 'B' ;
266
+ scope . $apply ( ) ;
267
+
268
+ var select = jqLite (
269
+ '<select capture-select-ctrl ng-model="mySelect">' +
270
+ '<optgroup label="first">' +
271
+ '<option value="A">A</option>' +
272
+ '</optgroup>' +
273
+ '<optgroup label="second">' + '\n' +
274
+ '<option value="B">B</option>' +
275
+ '</optgroup> ' +
276
+ '</select>' ) ;
277
+
278
+ $compile ( select ) ( scope ) ;
279
+ expect ( selectCtrl . writeValue ) . not . toHaveBeenCalled ( ) ;
280
+
281
+ scope . $digest ( ) ;
282
+ expect ( selectCtrl . writeValue ) . toHaveBeenCalledOnce ( ) ;
283
+ dealoc ( select ) ;
284
+ } ) ;
285
+
201
286
describe ( 'empty option' , function ( ) {
202
287
203
288
it ( 'should allow empty option to be added and removed dynamically' , function ( ) {
@@ -538,22 +623,6 @@ describe('select', function() {
538
623
539
624
describe ( 'selectController.hasOption' , function ( ) {
540
625
541
- function compileRepeatedOptions ( ) {
542
- compile ( '<select ng-model="robot">' +
543
- '<option value="{{item.value}}" ng-repeat="item in robots">{{item.label}}</option>' +
544
- '</select>' ) ;
545
- }
546
-
547
- function compileGroupedOptions ( ) {
548
- compile (
549
- '<select ng-model="mySelect">' +
550
- '<option ng-repeat="item in values">{{item.name}}</option>' +
551
- '<optgroup ng-repeat="group in groups" label="{{group.name}}">' +
552
- '<option ng-repeat="item in group.values">{{item.name}}</option>' +
553
- '</optgroup>' +
554
- '</select>' ) ;
555
- }
556
-
557
626
describe ( 'flat options' , function ( ) {
558
627
it ( 'should return false for options shifted via ngRepeat' , function ( ) {
559
628
scope . robots = [
@@ -624,7 +693,7 @@ describe('select', function() {
624
693
expect ( selectCtrl . hasOption ( 'A' ) ) . toBe ( true ) ;
625
694
expect ( selectCtrl . hasOption ( 'B' ) ) . toBe ( true ) ;
626
695
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
627
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ 'A' , 'B' , 'C' ] } ) ;
696
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ 'A' , 'B' , [ 'C' ] ] } ) ;
628
697
} ) ;
629
698
} ) ;
630
699
@@ -665,7 +734,7 @@ describe('select', function() {
665
734
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
666
735
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
667
736
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
668
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
737
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
669
738
} ) ;
670
739
671
740
@@ -702,7 +771,7 @@ describe('select', function() {
702
771
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
703
772
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
704
773
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
705
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' ] , 'first' :[ 'B' , 'C' , 'D' ] , 'second' : [ 'E' ] } ) ;
774
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] ] , 'first' :[ 'B' , 'C' , 'D' ] , 'second' : [ 'E' ] } ) ;
706
775
} ) ;
707
776
708
777
@@ -741,7 +810,7 @@ describe('select', function() {
741
810
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
742
811
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
743
812
expect ( selectCtrl . hasOption ( 'F' ) ) . toBe ( false ) ;
744
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
813
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
745
814
} ) ;
746
815
747
816
@@ -778,7 +847,7 @@ describe('select', function() {
778
847
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( false ) ;
779
848
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
780
849
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
781
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'D' ] , 'second' : [ 'E' ] } ) ;
850
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'D' ] , 'second' : [ 'E' ] } ) ;
782
851
} ) ;
783
852
784
853
@@ -813,7 +882,7 @@ describe('select', function() {
813
882
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
814
883
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( false ) ;
815
884
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
816
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'E' ] } ) ;
885
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'E' ] } ) ;
817
886
} ) ;
818
887
819
888
@@ -848,7 +917,7 @@ describe('select', function() {
848
917
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
849
918
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( false ) ;
850
919
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( false ) ;
851
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] } ) ;
920
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] } ) ;
852
921
} ) ;
853
922
} ) ;
854
923
} ) ;
0 commit comments