1
1
describe ( 'md-input-container directive' , function ( ) {
2
+ var $compile , pageScope ;
2
3
3
- beforeEach ( module ( 'material.components.input' ) ) ;
4
- beforeEach ( module ( 'ngAria' ) ) ;
4
+ beforeEach ( module ( 'ngAria' , 'material.components.input' ) ) ;
5
+
6
+ beforeEach ( inject ( function ( $injector ) {
7
+ $compile = $injector . get ( '$compile' ) ;
8
+
9
+ var pageScope = $injector . get ( '$rootScope' ) . $new ( ) ;
10
+ } ) ) ;
5
11
6
12
function setup ( attrs , isForm ) {
7
13
var container ;
8
- inject ( function ( $rootScope , $compile ) {
9
- container = $compile ( ( isForm ? '<form>' : '' ) +
10
- '<md-input-container><input ' + ( attrs || '' ) + '><label></label></md-input-container>' +
11
- ( isForm ? '<form>' : '' ) ) ( $rootScope ) ;
12
- $rootScope . $apply ( ) ;
13
- } ) ;
14
+
15
+ var template =
16
+ '<md-input-container>' +
17
+ '<input ' + ( attrs || '' ) + '>' +
18
+ '<label></label>' +
19
+ '</md-input-container>' ;
20
+
21
+ if ( isForm ) {
22
+ template = '<form>' + template + '</form>' ;
23
+ }
24
+
25
+ container = $compile ( template ) ( pageScope ) ;
26
+
27
+ pageScope . $apply ( ) ;
14
28
return container ;
15
29
}
16
30
17
- it ( 'should by default show error on $touched and $invalid' , inject ( function ( $rootScope ) {
31
+ it ( 'should by default show error on $touched and $invalid' , function ( ) {
18
32
var el = setup ( 'ng-model="foo"' ) ;
19
33
20
34
expect ( el ) . not . toHaveClass ( 'md-input-invalid' ) ;
21
35
22
36
var model = el . find ( 'input' ) . controller ( 'ngModel' ) ;
23
37
model . $touched = model . $invalid = true ;
24
- $rootScope . $apply ( ) ;
38
+ pageScope . $apply ( ) ;
25
39
26
40
expect ( el ) . toHaveClass ( 'md-input-invalid' ) ;
27
41
28
42
model . $touched = model . $invalid = false ;
29
- $rootScope . $apply ( ) ;
43
+ pageScope . $apply ( ) ;
30
44
expect ( el ) . not . toHaveClass ( 'md-input-invalid' ) ;
31
- } ) ) ;
45
+ } ) ;
32
46
33
- it ( 'should show error with given md-is-error expression' , inject ( function ( $rootScope , $compile ) {
34
- var el = $compile ( '<md-input-container md-is-error="$root.isError"><input ng-model="foo"></md-input-container>' ) ( $rootScope ) ;
47
+ it ( 'should show error with given md-is-error expression' , function ( ) {
48
+ var el = $compile (
49
+ '<md-input-container md-is-error="isError">' +
50
+ '<input ng-model="foo">' +
51
+ '</md-input-container>' ) ( pageScope ) ;
35
52
36
- $rootScope . $apply ( ) ;
53
+ pageScope . $apply ( ) ;
37
54
expect ( el ) . not . toHaveClass ( 'md-input-invalid' ) ;
38
55
39
- $rootScope . $apply ( 'isError = true' ) ;
56
+ pageScope . $apply ( 'isError = true' ) ;
40
57
expect ( el ) . toHaveClass ( 'md-input-invalid' ) ;
41
58
42
- $rootScope . $apply ( 'isError = false' ) ;
59
+ pageScope . $apply ( 'isError = false' ) ;
43
60
expect ( el ) . not . toHaveClass ( 'md-input-invalid' ) ;
44
- } ) ) ;
61
+ } ) ;
45
62
46
63
it ( 'should set focus class on container' , function ( ) {
47
64
var el = setup ( ) ;
@@ -76,124 +93,140 @@ describe('md-input-container directive', function() {
76
93
expect ( el ) . not . toHaveClass ( 'md-input-has-value' ) ;
77
94
} ) ;
78
95
79
- it ( 'should set has-value class on container for ng-model input' , inject ( function ( $rootScope ) {
80
- $rootScope . value = 'test' ;
81
- var el = setup ( 'ng-model="$root. value"' ) ;
96
+ it ( 'should set has-value class on container for ng-model input' , function ( ) {
97
+ pageScope . value = 'test' ;
98
+ var el = setup ( 'ng-model="value"' ) ;
82
99
expect ( el ) . toHaveClass ( 'md-input-has-value' ) ;
83
100
84
- $rootScope . $apply ( 'value = "3"' ) ;
101
+ pageScope . $apply ( 'value = "3"' ) ;
85
102
expect ( el ) . toHaveClass ( 'md-input-has-value' ) ;
86
103
87
- $rootScope . $apply ( 'value = null' ) ;
104
+ pageScope . $apply ( 'value = null' ) ;
88
105
expect ( el ) . not . toHaveClass ( 'md-input-has-value' ) ;
89
- } ) ) ;
106
+ } ) ;
90
107
91
- it ( 'should match label to given input id' , inject ( function ( ) {
108
+ it ( 'should match label to given input id' , function ( ) {
92
109
var el = setup ( 'id="foo"' ) ;
93
110
expect ( el . find ( 'label' ) . attr ( 'for' ) ) . toBe ( 'foo' ) ;
94
111
expect ( el . find ( 'input' ) . attr ( 'id' ) ) . toBe ( 'foo' ) ;
95
- } ) ) ;
112
+ } ) ;
96
113
97
- it ( 'should match label to automatic input id' , inject ( function ( ) {
114
+ it ( 'should match label to automatic input id' , function ( ) {
98
115
var el = setup ( ) ;
99
116
expect ( el . find ( 'input' ) . attr ( 'id' ) ) . toBeTruthy ( ) ;
100
117
expect ( el . find ( 'label' ) . attr ( 'for' ) ) . toBe ( el . find ( 'input' ) . attr ( 'id' ) ) ;
101
- } ) ) ;
118
+ } ) ;
102
119
103
120
describe ( 'md-maxlength' , function ( ) {
104
121
function getCharCounter ( el ) {
105
122
return angular . element ( el [ 0 ] . querySelector ( '.md-char-counter' ) ) ;
106
123
}
107
124
108
- it ( 'should work with a constant' , inject ( function ( $rootScope , $compile ) {
109
- var el = $compile ( '<form name="form">' +
110
- ' <md-input-container>' +
111
- ' <input md-maxlength="5" ng-model="foo" name="foo">' +
112
- ' </md-input-container>' +
113
- '</form>' ) ( $rootScope ) ;
114
- $rootScope . $apply ( ) ;
115
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
125
+ it ( 'should work with a constant' , function ( ) {
126
+ var el = $compile (
127
+ '<form name="form">' +
128
+ '<md-input-container>' +
129
+ '<input md-maxlength="5" ng-model="foo" name="foo">' +
130
+ '</md-input-container>' +
131
+ '</form>' ) ( pageScope ) ;
132
+
133
+ pageScope . $apply ( ) ;
134
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
116
135
expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '0/5' ) ;
117
136
118
- $rootScope . $apply ( 'foo = "abcde"' ) ;
119
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
137
+ pageScope . $apply ( 'foo = "abcde"' ) ;
138
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
120
139
expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '5/5' ) ;
121
140
122
- $rootScope . $apply ( 'foo = "abcdef"' ) ;
141
+ pageScope . $apply ( 'foo = "abcdef"' ) ;
123
142
el . find ( 'input' ) . triggerHandler ( 'input' ) ;
124
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBe ( true ) ;
143
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBe ( true ) ;
125
144
expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '6/5' ) ;
126
145
127
- $rootScope . $apply ( 'foo = "abc"' ) ;
146
+ pageScope . $apply ( 'foo = "abc"' ) ;
128
147
el . find ( 'input' ) . triggerHandler ( 'input' ) ;
129
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
148
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
130
149
expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '3/5' ) ;
131
- } ) ) ;
150
+ } ) ;
151
+
152
+ it ( 'should render correct character count when value is a number' , function ( ) {
153
+ var template =
154
+ '<md-input-container>' +
155
+ '<input ng-model="item.numberValue" md-maxlength="6">' +
156
+ '</md-input-container>' ;
157
+ var element = $compile ( template ) ( pageScope ) ;
158
+ pageScope . $apply ( ) ;
159
+
160
+ pageScope . item = { numberValue : 456 } ;
161
+ pageScope . $apply ( ) ;
162
+
163
+ expect ( getCharCounter ( element ) . text ( ) ) . toBe ( '3/6' ) ;
164
+ } ) ;
132
165
133
- it ( 'should add and remove maxlength element & error with expression' , inject ( function ( $rootScope , $compile ) {
166
+ it ( 'should add and remove maxlength element & error with expression' , function ( ) {
134
167
var el = $compile ( '<form name="form">' +
135
168
' <md-input-container>' +
136
169
' <input md-maxlength="max" ng-model="foo" name="foo">' +
137
170
' </md-input-container>' +
138
- '</form>' ) ( $rootScope ) ;
171
+ '</form>' ) ( pageScope ) ;
139
172
140
- $rootScope . $apply ( ) ;
141
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
173
+ pageScope . $apply ( ) ;
174
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
142
175
expect ( getCharCounter ( el ) . length ) . toBe ( 0 ) ;
143
176
144
- $rootScope . $apply ( 'max = 5' ) ;
145
- $rootScope . $apply ( 'foo = "abcdef"' ) ;
146
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeTruthy ( ) ;
177
+ pageScope . $apply ( 'max = 5' ) ;
178
+ pageScope . $apply ( 'foo = "abcdef"' ) ;
179
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeTruthy ( ) ;
147
180
expect ( getCharCounter ( el ) . length ) . toBe ( 1 ) ;
148
181
expect ( getCharCounter ( el ) . text ( ) ) . toBe ( '6/5' ) ;
149
182
150
- $rootScope . $apply ( 'max = -1' ) ;
151
- $rootScope . $apply ( 'foo = "abcdefg"' ) ;
152
- expect ( $rootScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
183
+ pageScope . $apply ( 'max = -1' ) ;
184
+ pageScope . $apply ( 'foo = "abcdefg"' ) ;
185
+ expect ( pageScope . form . foo . $error [ 'md-maxlength' ] ) . toBeFalsy ( ) ;
153
186
expect ( getCharCounter ( el ) . length ) . toBe ( 0 ) ;
154
- } ) ) ;
187
+ } ) ;
155
188
} ) ;
156
189
157
- it ( 'should put placeholder into a label element' , inject ( function ( $rootScope , $compile ) {
158
- var el = $compile ( '<md-input-container><input ng-model="foo" placeholder="some placeholder"></md-input-container>' ) ( $rootScope ) ;
190
+ it ( 'should put placeholder into a label element' , function ( ) {
191
+ var el = $compile ( '<md-input-container><input ng-model="foo" placeholder="some placeholder"></md-input-container>' ) ( pageScope ) ;
159
192
var placeholder = el [ 0 ] . querySelector ( '.md-placeholder' ) ;
160
193
var label = el . find ( 'label' ) [ 0 ] ;
161
194
162
195
expect ( el . find ( 'input' ) [ 0 ] . hasAttribute ( 'placeholder' ) ) . toBe ( false ) ;
163
196
expect ( label ) . toBeTruthy ( ) ;
164
197
expect ( label . textContent ) . toEqual ( 'some placeholder' ) ;
165
- } ) ) ;
198
+ } ) ;
166
199
167
- it ( 'should ignore placeholder when a label element is present' , inject ( function ( $rootScope , $compile ) {
200
+ it ( 'should ignore placeholder when a label element is present' , function ( ) {
168
201
var el = $compile (
169
202
'<md-input-container>' +
170
203
' <label>Hello</label>' +
171
204
' <input ng-model="foo" placeholder="some placeholder" />' +
172
205
'</md-input-container>'
173
- ) ( $rootScope ) ;
206
+ ) ( pageScope ) ;
174
207
175
208
var label = el . find ( 'label' ) [ 0 ] ;
176
209
177
210
expect ( el . find ( 'input' ) [ 0 ] . hasAttribute ( 'placeholder' ) ) . toBe ( true ) ;
178
211
expect ( label ) . toBeTruthy ( ) ;
179
212
expect ( label . textContent ) . toEqual ( 'Hello' ) ;
180
- } ) ) ;
213
+ } ) ;
181
214
182
- it ( 'should put an aria-label on the input when no label is present' , inject ( function ( $rootScope , $compile ) {
215
+ it ( 'should put an aria-label on the input when no label is present' , function ( ) {
183
216
var el = $compile ( '<form name="form">' +
184
217
' <md-input-container md-no-float>' +
185
218
' <input placeholder="baz" md-maxlength="max" ng-model="foo" name="foo">' +
186
219
' </md-input-container>' +
187
- '</form>' ) ( $rootScope ) ;
220
+ '</form>' ) ( pageScope ) ;
188
221
189
- $rootScope . $apply ( ) ;
222
+ pageScope . $apply ( ) ;
190
223
191
224
var input = el . find ( 'input' ) ;
192
225
expect ( input . attr ( 'aria-label' ) ) . toBe ( 'baz' ) ;
193
- } ) ) ;
226
+ } ) ;
194
227
195
- it ( 'should put the container in "has value" state when input has a static value' , inject ( function ( $rootScope , $compile ) {
196
- var scope = $rootScope . $new ( ) ;
228
+ it ( 'should put the container in "has value" state when input has a static value' , function ( ) {
229
+ var scope = pageScope . $new ( ) ;
197
230
var template =
198
231
'<md-input-container>' +
199
232
'<label>Name</label>' +
@@ -204,5 +237,5 @@ describe('md-input-container directive', function() {
204
237
scope . $apply ( ) ;
205
238
206
239
expect ( element . hasClass ( 'md-input-has-value' ) ) . toBe ( true ) ;
207
- } ) ) ;
240
+ } ) ;
208
241
} ) ;
0 commit comments