@@ -11,7 +11,7 @@ describe('<md-autocomplete>', function() {
11
11
return container ;
12
12
}
13
13
14
- function createScope ( items , obj ) {
14
+ function createScope ( items , obj , matchLowercase ) {
15
15
var scope ;
16
16
items = items || [ 'foo' , 'bar' , 'baz' ] . map ( function ( item ) {
17
17
return { display : item } ;
@@ -20,7 +20,7 @@ describe('<md-autocomplete>', function() {
20
20
scope = $rootScope . $new ( ) ;
21
21
scope . match = function ( term ) {
22
22
return items . filter ( function ( item ) {
23
- return item . display . indexOf ( term ) === 0 ;
23
+ return item . display . indexOf ( matchLowercase ? term . toLowerCase ( ) : term ) === 0 ;
24
24
} ) ;
25
25
} ;
26
26
scope . asyncMatch = function ( term ) {
@@ -102,32 +102,58 @@ describe('<md-autocomplete>', function() {
102
102
103
103
element . remove ( ) ;
104
104
} ) ) ;
105
+
105
106
106
- // @TODO - re-enable test
107
- xit ( 'should allow receiving focus on the autocomplete' , function ( ) {
107
+ it ( 'should allow you to set an input id without floating label' , inject ( function ( ) {
108
108
var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
109
- var template = '<md-autocomplete ' +
110
- 'md-input-id="{{inputId}}" ' +
111
- 'md-selected-item="selectedItem" ' +
112
- 'md-search-text="searchText" ' +
113
- 'md-items="item in match(searchText)" ' +
114
- 'md-item-text="item.display" ' +
115
- 'placeholder="placeholder">' +
116
- '<span md-highlight-text="searchText">{{item.display}}</span>' +
117
- '</md-autocomplete>' ;
109
+ var template = '\
110
+ <md-autocomplete\
111
+ md-input-id="{{inputId}}"\
112
+ md-selected-item="selectedItem"\
113
+ md-search-text="searchText"\
114
+ md-items="item in match(searchText)"\
115
+ md-item-text="item.display"\
116
+ placeholder="placeholder">\
117
+ <span md-highlight-text="searchText">{{item.display}}</span>\
118
+ </md-autocomplete>' ;
118
119
var element = compile ( template , scope ) ;
119
- var focusSpy = jasmine . createSpy ( 'focus ') ;
120
+ var input = element . find ( 'input ') ;
120
121
121
- document . body . appendChild ( element [ 0 ] ) ;
122
+ expect ( input . attr ( 'id' ) ) . toBe ( scope . inputId ) ;
122
123
123
- element . on ( 'focus' , focusSpy ) ;
124
+ element . remove ( ) ;
125
+ } ) ) ;
124
126
125
- element . focus ( ) ;
127
+ it ( 'should allow allow using ng-readonly' , inject ( function ( ) {
128
+ var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
129
+ var template = '\
130
+ <md-autocomplete\
131
+ md-input-id="{{inputId}}"\
132
+ md-selected-item="selectedItem"\
133
+ md-search-text="searchText"\
134
+ md-items="item in match(searchText)"\
135
+ md-item-text="item.display"\
136
+ placeholder="placeholder"\
137
+ ng-readonly="readonly">\
138
+ <span md-highlight-text="searchText">{{item.display}}</span>\
139
+ </md-autocomplete>' ;
140
+ var element = compile ( template , scope ) ;
141
+ var input = element . find ( 'input' ) ;
126
142
127
- expect ( focusSpy ) . toHaveBeenCalled ( ) ;
128
- } ) ;
143
+ scope . readonly = true ;
144
+ scope . $digest ( ) ;
129
145
130
- it ( 'should allow you to set an input id without floating label' , inject ( function ( ) {
146
+ expect ( input . attr ( 'readonly' ) ) . toBe ( 'readonly' ) ;
147
+
148
+ scope . readonly = false ;
149
+ scope . $digest ( ) ;
150
+
151
+ expect ( input . attr ( 'readonly' ) ) . toBeUndefined ( ) ;
152
+
153
+ element . remove ( ) ;
154
+ } ) ) ;
155
+
156
+ it ( 'should allow allow using an empty readonly attribute' , inject ( function ( ) {
131
157
var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
132
158
var template = '\
133
159
<md-autocomplete\
@@ -136,13 +162,14 @@ describe('<md-autocomplete>', function() {
136
162
md-search-text="searchText"\
137
163
md-items="item in match(searchText)"\
138
164
md-item-text="item.display"\
139
- placeholder="placeholder">\
165
+ placeholder="placeholder"\
166
+ readonly>\
140
167
<span md-highlight-text="searchText">{{item.display}}</span>\
141
168
</md-autocomplete>' ;
142
169
var element = compile ( template , scope ) ;
143
170
var input = element . find ( 'input' ) ;
144
171
145
- expect ( input . attr ( 'id ' ) ) . toBe ( scope . inputId ) ;
172
+ expect ( input . attr ( 'readonly ' ) ) . toBe ( 'readonly' ) ;
146
173
147
174
element . remove ( ) ;
148
175
} ) ) ;
@@ -168,6 +195,102 @@ describe('<md-autocomplete>', function() {
168
195
element . remove ( ) ;
169
196
} ) ) ;
170
197
198
+ it ( 'should forward the `md-select-on-focus` attribute to the input' , inject ( function ( ) {
199
+ var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
200
+ var template =
201
+ '<md-autocomplete ' +
202
+ 'md-input-id="{{inputId}}" ' +
203
+ 'md-selected-item="selectedItem" ' +
204
+ 'md-search-text="searchText" ' +
205
+ 'md-items="item in match(searchText)" ' +
206
+ 'md-item-text="item.display" ' +
207
+ 'md-select-on-focus="" ' +
208
+ 'tabindex="3"' +
209
+ 'placeholder="placeholder">' +
210
+ '<span md-highlight-text="searchText">{{item.display}}</span>' +
211
+ '</md-autocomplete>' ;
212
+
213
+ var element = compile ( template , scope ) ;
214
+ var input = element . find ( 'input' ) ;
215
+
216
+ expect ( input . attr ( 'md-select-on-focus' ) ) . toBe ( "" ) ;
217
+
218
+ element . remove ( ) ;
219
+ } ) ) ;
220
+
221
+ it ( 'should forward the tabindex to the input' , inject ( function ( ) {
222
+ var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
223
+ var template =
224
+ '<md-autocomplete ' +
225
+ 'md-input-id="{{inputId}}" ' +
226
+ 'md-selected-item="selectedItem" ' +
227
+ 'md-search-text="searchText" ' +
228
+ 'md-items="item in match(searchText)" ' +
229
+ 'md-item-text="item.display" ' +
230
+ 'tabindex="3"' +
231
+ 'placeholder="placeholder">' +
232
+ '<span md-highlight-text="searchText">{{item.display}}</span>' +
233
+ '</md-autocomplete>' ;
234
+
235
+ var element = compile ( template , scope ) ;
236
+ var input = element . find ( 'input' ) ;
237
+
238
+ expect ( input . attr ( 'tabindex' ) ) . toBe ( '3' ) ;
239
+
240
+ element . remove ( ) ;
241
+ } ) ) ;
242
+
243
+ it ( 'should always set the tabindex of the autcomplete to `-1`' , inject ( function ( ) {
244
+ var scope = createScope ( null , { inputId : 'custom-input-id' } ) ;
245
+ var template =
246
+ '<md-autocomplete ' +
247
+ 'md-input-id="{{inputId}}" ' +
248
+ 'md-selected-item="selectedItem" ' +
249
+ 'md-search-text="searchText" ' +
250
+ 'md-items="item in match(searchText)" ' +
251
+ 'md-item-text="item.display" ' +
252
+ 'tabindex="3"' +
253
+ 'placeholder="placeholder">' +
254
+ '<span md-highlight-text="searchText">{{item.display}}</span>' +
255
+ '</md-autocomplete>' ;
256
+
257
+ var element = compile ( template , scope ) ;
258
+
259
+ expect ( element . attr ( 'tabindex' ) ) . toBe ( '-1' ) ;
260
+
261
+ element . remove ( ) ;
262
+ } ) ) ;
263
+
264
+ it ( 'should not show a loading progress when the items object is invalid' , inject ( function ( ) {
265
+ var scope = createScope ( null , {
266
+ match : function ( ) {
267
+ // Return an invalid object, which is not an array, neither a promise.
268
+ return { }
269
+ }
270
+ } ) ;
271
+
272
+ var template =
273
+ '<md-autocomplete ' +
274
+ 'md-input-id="{{inputId}}" ' +
275
+ 'md-selected-item="selectedItem" ' +
276
+ 'md-search-text="searchText" ' +
277
+ 'md-items="item in match(searchText)" ' +
278
+ 'md-item-text="item.display" ' +
279
+ 'tabindex="3"' +
280
+ 'placeholder="placeholder">' +
281
+ '<span md-highlight-text="searchText">{{item.display}}</span>' +
282
+ '</md-autocomplete>' ;
283
+
284
+ var element = compile ( template , scope ) ;
285
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
286
+
287
+ scope . $apply ( 'searchText = "test"' ) ;
288
+
289
+ expect ( ctrl . loading ) . toBe ( false ) ;
290
+
291
+ element . remove ( ) ;
292
+ } ) ) ;
293
+
171
294
it ( 'should clear value when hitting escape' , inject ( function ( $mdConstant , $timeout ) {
172
295
var scope = createScope ( ) ;
173
296
var template = '\
@@ -860,6 +983,33 @@ describe('<md-autocomplete>', function() {
860
983
861
984
element . remove ( ) ;
862
985
} ) ) ;
986
+
987
+ it ( 'should select matching item using case insensitive' , inject ( function ( $timeout ) {
988
+ var scope = createScope ( null , null , true ) ;
989
+ var template =
990
+ '<md-autocomplete ' +
991
+ 'md-select-on-match ' +
992
+ 'md-selected-item="selectedItem" ' +
993
+ 'md-search-text="searchText" ' +
994
+ 'md-items="item in match(searchText)" ' +
995
+ 'md-item-text="item.display" ' +
996
+ 'placeholder="placeholder" ' +
997
+ 'md-match-case-insensitive="true">' +
998
+ '<span md-highlight-text="searchText">{{item.display}}</span>' +
999
+ '</md-autocomplete>' ;
1000
+ var element = compile ( template , scope ) ;
1001
+
1002
+ expect ( scope . searchText ) . toBe ( '' ) ;
1003
+ expect ( scope . selectedItem ) . toBe ( null ) ;
1004
+
1005
+ element . scope ( ) . searchText = 'FoO' ;
1006
+ $timeout . flush ( ) ;
1007
+
1008
+ expect ( scope . selectedItem ) . not . toBe ( null ) ;
1009
+ expect ( scope . selectedItem . display ) . toBe ( 'foo' ) ;
1010
+
1011
+ element . remove ( ) ;
1012
+ } ) ) ;
863
1013
} ) ;
864
1014
865
1015
describe ( 'when required' , function ( ) {
@@ -892,6 +1042,74 @@ describe('<md-autocomplete>', function() {
892
1042
893
1043
element . remove ( ) ;
894
1044
} ) ;
1045
+
1046
+ it ( 'should validate an empty `required` as true' , function ( ) {
1047
+ var scope = createScope ( ) ;
1048
+ var template = '\
1049
+ <md-autocomplete\
1050
+ md-selected-item="selectedItem"\
1051
+ md-search-text="searchText"\
1052
+ md-items="item in match(searchText)"\
1053
+ md-item-text="item.display"\
1054
+ md-min-length="0" \
1055
+ required\
1056
+ placeholder="placeholder">\
1057
+ <span md-highlight-text="searchText">{{item.display}}</span>\
1058
+ </md-autocomplete>' ;
1059
+ var element = compile ( template , scope ) ;
1060
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
1061
+
1062
+ expect ( ctrl . isRequired ) . toBe ( true ) ;
1063
+ } ) ;
1064
+
1065
+ it ( 'should correctly validate an interpolated `ng-required` value' , function ( ) {
1066
+ var scope = createScope ( ) ;
1067
+ var template = '\
1068
+ <md-autocomplete\
1069
+ md-selected-item="selectedItem"\
1070
+ md-search-text="searchText"\
1071
+ md-items="item in match(searchText)"\
1072
+ md-item-text="item.display"\
1073
+ md-min-length="0" \
1074
+ ng-required="interpolateRequired"\
1075
+ placeholder="placeholder">\
1076
+ <span md-highlight-text="searchText">{{item.display}}</span>\
1077
+ </md-autocomplete>' ;
1078
+ var element = compile ( template , scope ) ;
1079
+ var ctrl = element . controller ( 'mdAutocomplete' ) ;
1080
+
1081
+ expect ( ctrl . isRequired ) . toBe ( false ) ;
1082
+
1083
+ scope . interpolateRequired = false ;
1084
+ scope . $apply ( ) ;
1085
+
1086
+ expect ( ctrl . isRequired ) . toBe ( false ) ;
1087
+
1088
+ scope . interpolateRequired = true ;
1089
+ scope . $apply ( ) ;
1090
+
1091
+ expect ( ctrl . isRequired ) . toBe ( true ) ;
1092
+ } ) ;
1093
+
1094
+ it ( 'should forward the md-no-asterisk attribute' , function ( ) {
1095
+ var scope = createScope ( ) ;
1096
+ var template = '\
1097
+ <md-autocomplete\
1098
+ md-selected-item="selectedItem"\
1099
+ md-search-text="searchText"\
1100
+ md-items="item in match(searchText)"\
1101
+ md-item-text="item.display"\
1102
+ md-min-length="0" \
1103
+ required\
1104
+ md-no-asterisk="true"\
1105
+ md-floating-label="Asterisk Label">\
1106
+ <span md-highlight-text="searchText">{{item.display}}</span>\
1107
+ </md-autocomplete>' ;
1108
+ var element = compile ( template , scope ) ;
1109
+ var input = element . find ( 'input' ) ;
1110
+
1111
+ expect ( input . attr ( 'md-no-asterisk' ) ) . toBe ( 'true' ) ;
1112
+ } ) ;
895
1113
} ) ;
896
1114
897
1115
describe ( 'md-highlight-text' , function ( ) {
0 commit comments