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,42 @@ 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 ( 'spyOnWriteValue' , function ( ) {
35
+ return {
36
+ require : 'select' ,
37
+ link : {
38
+ pre : function ( scope , element , attrs , ctrl ) {
39
+ selectCtrl = ctrl ;
40
+ renderSpy = jasmine . createSpy ( 'renderSpy' ) ;
41
+ selectCtrl . ngModelCtrl . $render = renderSpy . andCallFake ( selectCtrl . ngModelCtrl . $render ) ;
42
+ spyOn ( selectCtrl , 'writeValue' ) . andCallThrough ( ) ;
43
+ }
44
+ }
45
+ } ;
46
+ } ) ;
47
+ } ) ) ;
48
+
17
49
beforeEach ( inject ( function ( $rootScope , _$compile_ ) {
18
50
scope = $rootScope . $new ( ) ; //create a child scope because the root scope can't be $destroy-ed
19
51
$compile = _$compile_ ;
@@ -47,12 +79,14 @@ describe('select', function() {
47
79
toEqualSelectWithOptions : function ( expected ) {
48
80
var actualValues = { } ;
49
81
var optionGroup ;
82
+ var optionValue ;
50
83
51
84
forEach ( this . actual . find ( 'option' ) , function ( option ) {
52
85
optionGroup = option . parentNode . label || '' ;
53
86
actualValues [ optionGroup ] = actualValues [ optionGroup ] || [ ] ;
54
87
// IE9 doesn't populate the label property from the text property like other browsers
55
- actualValues [ optionGroup ] . push ( option . label || option . text ) ;
88
+ optionValue = option . label || option . text ;
89
+ actualValues [ optionGroup ] . push ( option . selected ? [ optionValue ] : optionValue ) ;
56
90
} ) ;
57
91
58
92
this . message = function ( ) {
@@ -198,6 +232,50 @@ describe('select', function() {
198
232
} ) ;
199
233
200
234
235
+ it ( 'should select options in a group when there is a linebreak before an option' , function ( ) {
236
+ scope . mySelect = 'B' ;
237
+ scope . $apply ( ) ;
238
+
239
+ var select = jqLite (
240
+ '<select ng-model="mySelect">' +
241
+ '<optgroup label="first">' +
242
+ '<option value="A">A</option>' +
243
+ '</optgroup>' +
244
+ '<optgroup label="second">' + '\n' +
245
+ '<option value="B">B</option>' +
246
+ '</optgroup> ' +
247
+ '</select>' ) ;
248
+
249
+ $compile ( select ) ( scope ) ;
250
+ scope . $apply ( ) ;
251
+
252
+ expect ( select ) . toEqualSelectWithOptions ( { 'first' :[ 'A' ] , 'second' : [ [ 'B' ] ] } ) ;
253
+ dealoc ( select ) ;
254
+ } ) ;
255
+
256
+
257
+ it ( 'should only call selectCtrl.writeValue after a digest has occured' , function ( ) {
258
+ scope . mySelect = 'B' ;
259
+ scope . $apply ( ) ;
260
+
261
+ var select = jqLite (
262
+ '<select spy-on-write-value ng-model="mySelect">' +
263
+ '<optgroup label="first">' +
264
+ '<option value="A">A</option>' +
265
+ '</optgroup>' +
266
+ '<optgroup label="second">' + '\n' +
267
+ '<option value="B">B</option>' +
268
+ '</optgroup> ' +
269
+ '</select>' ) ;
270
+
271
+ $compile ( select ) ( scope ) ;
272
+ expect ( selectCtrl . writeValue ) . not . toHaveBeenCalled ( ) ;
273
+
274
+ scope . $digest ( ) ;
275
+ expect ( selectCtrl . writeValue ) . toHaveBeenCalledOnce ( ) ;
276
+ dealoc ( select ) ;
277
+ } ) ;
278
+
201
279
describe ( 'empty option' , function ( ) {
202
280
203
281
it ( 'should allow empty option to be added and removed dynamically' , function ( ) {
@@ -538,22 +616,6 @@ describe('select', function() {
538
616
539
617
describe ( 'selectController.hasOption' , function ( ) {
540
618
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
619
describe ( 'flat options' , function ( ) {
558
620
it ( 'should return false for options shifted via ngRepeat' , function ( ) {
559
621
scope . robots = [
@@ -624,7 +686,7 @@ describe('select', function() {
624
686
expect ( selectCtrl . hasOption ( 'A' ) ) . toBe ( true ) ;
625
687
expect ( selectCtrl . hasOption ( 'B' ) ) . toBe ( true ) ;
626
688
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
627
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ 'A' , 'B' , 'C' ] } ) ;
689
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ 'A' , 'B' , [ 'C' ] ] } ) ;
628
690
} ) ;
629
691
} ) ;
630
692
@@ -665,7 +727,7 @@ describe('select', function() {
665
727
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
666
728
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
667
729
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
668
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
730
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
669
731
} ) ;
670
732
671
733
@@ -702,7 +764,7 @@ describe('select', function() {
702
764
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
703
765
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
704
766
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
705
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' ] , 'first' :[ 'B' , 'C' , 'D' ] , 'second' : [ 'E' ] } ) ;
767
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] ] , 'first' :[ 'B' , 'C' , 'D' ] , 'second' : [ 'E' ] } ) ;
706
768
} ) ;
707
769
708
770
@@ -741,7 +803,7 @@ describe('select', function() {
741
803
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
742
804
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
743
805
expect ( selectCtrl . hasOption ( 'F' ) ) . toBe ( false ) ;
744
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
806
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'D' , 'E' ] } ) ;
745
807
} ) ;
746
808
747
809
@@ -778,7 +840,7 @@ describe('select', function() {
778
840
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( false ) ;
779
841
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( true ) ;
780
842
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
781
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'D' ] , 'second' : [ 'E' ] } ) ;
843
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'D' ] , 'second' : [ 'E' ] } ) ;
782
844
} ) ;
783
845
784
846
@@ -813,7 +875,7 @@ describe('select', function() {
813
875
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
814
876
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( false ) ;
815
877
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( true ) ;
816
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'E' ] } ) ;
878
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] , 'second' : [ 'E' ] } ) ;
817
879
} ) ;
818
880
819
881
@@ -848,7 +910,7 @@ describe('select', function() {
848
910
expect ( selectCtrl . hasOption ( 'C' ) ) . toBe ( true ) ;
849
911
expect ( selectCtrl . hasOption ( 'D' ) ) . toBe ( false ) ;
850
912
expect ( selectCtrl . hasOption ( 'E' ) ) . toBe ( false ) ;
851
- expect ( element ) . toEqualSelectWithOptions ( { '' : [ '' , 'A' ] , 'first' :[ 'B' , 'C' ] } ) ;
913
+ expect ( element ) . toEqualSelectWithOptions ( { '' : [ [ '' ] , 'A' ] , 'first' :[ 'B' , 'C' ] } ) ;
852
914
} ) ;
853
915
} ) ;
854
916
} ) ;
0 commit comments