@@ -28,6 +28,14 @@ describe('<md-autocomplete>', function () {
28
28
return scope ;
29
29
}
30
30
31
+ function keydownEvent ( keyCode ) {
32
+ return {
33
+ keyCode : keyCode ,
34
+ stopPropagation : angular . noop ,
35
+ preventDefault : angular . noop
36
+ } ;
37
+ }
38
+
31
39
describe ( 'basic functionality' , function ( ) {
32
40
it ( 'should update selected item and search text' , inject ( function ( $timeout , $mdConstant ) {
33
41
var scope = createScope ( ) ;
@@ -54,16 +62,8 @@ describe('<md-autocomplete>', function () {
54
62
expect ( scope . match ( scope . searchText ) . length ) . toBe ( 1 ) ;
55
63
expect ( ul . find ( 'li' ) . length ) . toBe ( 1 ) ;
56
64
57
- ctrl . keydown ( {
58
- keyCode : $mdConstant . KEY_CODE . DOWN_ARROW ,
59
- preventDefault : angular . noop ,
60
- stopPropagation : angular . noop
61
- } ) ;
62
- ctrl . keydown ( {
63
- keyCode : $mdConstant . KEY_CODE . ENTER ,
64
- preventDefault : angular . noop ,
65
- stopPropagation : angular . noop
66
- } ) ;
65
+ ctrl . keydown ( keydownEvent ( $mdConstant . KEY_CODE . DOWN_ARROW ) ) ;
66
+ ctrl . keydown ( keydownEvent ( $mdConstant . KEY_CODE . ENTER ) ) ;
67
67
$timeout . flush ( ) ;
68
68
69
69
expect ( scope . searchText ) . toBe ( 'foo' ) ;
@@ -88,7 +88,7 @@ describe('<md-autocomplete>', function () {
88
88
expect ( input . attr ( 'id' ) ) . toBe ( scope . inputId ) ;
89
89
} ) ) ;
90
90
91
- it ( 'should allow you to set an input id without floating label' , inject ( function ( ) {
91
+ it ( 'should allow you to set an input id with floating label' , inject ( function ( ) {
92
92
var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
93
93
var template = '\
94
94
<md-autocomplete\
@@ -106,6 +106,32 @@ describe('<md-autocomplete>', function () {
106
106
107
107
expect ( input . attr ( 'id' ) ) . toBe ( scope . inputId ) ;
108
108
} ) ) ;
109
+
110
+ it ( 'should clear value when hitting escape' , inject ( function ( $mdConstant , $timeout ) {
111
+ var scope = createScope ( ) ;
112
+ var template = '\
113
+ <md-autocomplete\
114
+ md-search-text="searchText"\
115
+ md-items="item in match(searchText)"\
116
+ md-item-text="item.display"\
117
+ placeholder="placeholder">\
118
+ <span md-highlight-text="searchText">{{item.display}}</span>\
119
+ </md-autocomplete>' ;
120
+ var element = compile ( template , scope ) ;
121
+ var input = element . find ( 'input' ) ;
122
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
123
+
124
+ expect ( scope . searchText ) . toBe ( '' ) ;
125
+
126
+ scope . $apply ( 'searchText = "test"' ) ;
127
+
128
+ expect ( scope . searchText ) . toBe ( 'test' ) ;
129
+
130
+ $timeout . flush ( ) ;
131
+ scope . $apply ( function ( ) { ctrl . keydown ( keydownEvent ( $mdConstant . KEY_CODE . ESCAPE ) ) ; } ) ;
132
+
133
+ expect ( scope . searchText ) . toBe ( '' ) ;
134
+ } ) ) ;
109
135
} ) ;
110
136
111
137
describe ( 'basic functionality with template' , function ( ) {
@@ -136,16 +162,8 @@ describe('<md-autocomplete>', function () {
136
162
expect ( scope . match ( scope . searchText ) . length ) . toBe ( 1 ) ;
137
163
expect ( ul . find ( 'li' ) . length ) . toBe ( 1 ) ;
138
164
139
- ctrl . keydown ( {
140
- keyCode : $mdConstant . KEY_CODE . DOWN_ARROW ,
141
- preventDefault : angular . noop ,
142
- stopPropagation : angular . noop
143
- } ) ;
144
- ctrl . keydown ( {
145
- keyCode : $mdConstant . KEY_CODE . ENTER ,
146
- preventDefault : angular . noop ,
147
- stopPropagation : angular . noop
148
- } ) ;
165
+ ctrl . keydown ( keydownEvent ( $mdConstant . KEY_CODE . DOWN_ARROW ) ) ;
166
+ ctrl . keydown ( keydownEvent ( $mdConstant . KEY_CODE . ENTER ) ) ;
149
167
$timeout . flush ( ) ;
150
168
151
169
expect ( scope . searchText ) . toBe ( 'foo' ) ;
0 commit comments