@@ -12,6 +12,9 @@ describe('<md-select>', function() {
12
12
13
13
var selectMenus = $document . find ( 'md-select-menu' ) ;
14
14
selectMenus . remove ( ) ;
15
+
16
+ var backdrops = $document . find ( 'md-backdrop' ) ;
17
+ backdrops . remove ( ) ;
15
18
} ) ) ;
16
19
17
20
it ( 'should preserve tabindex' , inject ( function ( $document ) {
@@ -40,7 +43,7 @@ describe('<md-select>', function() {
40
43
expect ( container . classList . contains ( 'test' ) ) . toBe ( true ) ;
41
44
} ) ) ;
42
45
43
- it ( 'closes the menu if the element on backdrop click ' , inject ( function ( $document , $rootScope ) {
46
+ it ( 'calls md-on-close when the select menu closes ' , inject ( function ( $document , $rootScope ) {
44
47
var called = false ;
45
48
$rootScope . onClose = function ( ) {
46
49
called = true ;
@@ -49,16 +52,44 @@ describe('<md-select>', function() {
49
52
openSelect ( select ) ;
50
53
51
54
// Simulate click bubble from option to select menu handler
52
- select . triggerHandler ( {
53
- type : 'click' ,
54
- target : angular . element ( $document . find ( 'md-option' ) [ 0 ] )
55
- } ) ;
55
+ clickOption ( 0 ) ;
56
56
57
57
waitForSelectClose ( ) ;
58
58
59
59
expect ( called ) . toBe ( true ) ;
60
60
} ) ) ;
61
61
62
+ it ( 'closes on backdrop click' , inject ( function ( $document ) {
63
+ var select = setupSelect ( 'ng-model="val"' , [ 1 , 2 , 3 ] ) . find ( 'md-select' ) ;
64
+ openSelect ( select ) ;
65
+
66
+ // Simulate click bubble from option to select menu handler
67
+ var backdrop = $document . find ( 'md-backdrop' ) ;
68
+ expect ( backdrop . length ) . toBe ( 1 ) ;
69
+ backdrop . triggerHandler ( 'click' ) ;
70
+
71
+ waitForSelectClose ( ) ;
72
+
73
+ backdrop = $document . find ( 'md-backdrop' ) ;
74
+ expect ( backdrop . length ) . toBe ( 0 ) ;
75
+ } ) ) ;
76
+
77
+ it ( 'should not trigger ng-change without a change when using trackBy' , inject ( function ( $rootScope ) {
78
+ var changed = false ;
79
+ $rootScope . onChange = function ( ) { changed = true ; } ;
80
+ $rootScope . val = { id : 1 , name : 'Bob' } ;
81
+
82
+ var opts = [ { id : 1 , name : 'Bob' } , { id : 2 , name : 'Alice' } ] ;
83
+ var select = setupSelect ( 'ng-model="$root.val" ng-change="onChange()" ng-model-options="{trackBy: \'$value.id\'}"' , opts ) ;
84
+ expect ( changed ) . toBe ( false ) ;
85
+
86
+ openSelect ( select ) ;
87
+ clickOption ( 1 ) ;
88
+ waitForSelectClose ( ) ;
89
+ expect ( $rootScope . val . id ) . toBe ( 2 ) ;
90
+ expect ( changed ) . toBe ( true ) ;
91
+ } ) ) ;
92
+
62
93
it ( 'should set touched only after closing' , inject ( function ( $compile , $rootScope ) {
63
94
var form = $compile ( '<form name="myForm">' +
64
95
'<md-select name="select" ng-model="val">' +
@@ -825,15 +856,20 @@ describe('<md-select>', function() {
825
856
}
826
857
827
858
function openSelect ( el ) {
859
+ if ( el [ 0 ] . nodeName != 'MD-SELECT' ) {
860
+ el = el . find ( 'md-select' ) ;
861
+ }
828
862
try {
829
863
el . triggerHandler ( 'click' ) ;
830
864
waitForSelectOpen ( ) ;
831
865
el . triggerHandler ( 'blur' ) ;
832
- } catch ( e ) { }
866
+ } catch ( e ) { }
833
867
}
834
868
835
869
function closeSelect ( ) {
836
870
inject ( function ( $document ) {
871
+ var backdrop = $document . find ( 'md-backdrop' ) ;
872
+ if ( ! backdrop . length ) throw Error ( 'Attempted to close select with no backdrop present' ) ;
837
873
$document . find ( 'md-backdrop' ) . triggerHandler ( 'click' ) ;
838
874
} ) ;
839
875
waitForSelectClose ( ) ;
@@ -859,4 +895,23 @@ describe('<md-select>', function() {
859
895
} ) ;
860
896
}
861
897
898
+ function clickOption ( index ) {
899
+ inject ( function ( $rootScope , $document ) {
900
+ var openMenu = $document . find ( 'md-select-menu' ) ;
901
+ var opt = $document . find ( 'md-option' ) [ index ] ;
902
+
903
+ if ( ! openMenu . length ) throw Error ( 'No select menu currently open' ) ;
904
+ if ( ! opt ) throw Error ( 'Could not find option at index: ' + index ) ;
905
+ var target = angular . element ( opt ) ;
906
+ angular . element ( openMenu ) . triggerHandler ( {
907
+ type : 'click' ,
908
+ target : target
909
+ } ) ;
910
+ angular . element ( openMenu ) . triggerHandler ( {
911
+ type : 'mouseup' ,
912
+ currentTarget : openMenu [ 0 ]
913
+ } ) ;
914
+ } ) ;
915
+ }
916
+
862
917
} ) ;
0 commit comments