1
1
describe ( '$mdToast service' , function ( ) {
2
2
beforeEach ( module ( 'material.components.toast' , function ( $provide ) {
3
3
} ) ) ;
4
+ afterEach ( inject ( function ( $timeout , $animate ) {
5
+ $animate . triggerCallbacks ( ) ;
6
+ $timeout . flush ( ) ;
7
+ } ) ) ;
4
8
5
9
function setup ( options ) {
10
+ var promise ;
6
11
inject ( function ( $mdToast , $rootScope , $animate ) {
7
- $animate . triggerCallbacks ( ) ;
8
12
options = options || { } ;
9
- $mdToast . show ( options ) ;
13
+ $animate . triggerCallbacks ( ) ;
14
+
15
+ promise = $mdToast . show ( options ) ;
16
+
10
17
$rootScope . $apply ( ) ;
11
18
$animate . triggerCallbacks ( ) ;
12
19
} ) ;
20
+ return promise ;
13
21
}
14
22
15
23
describe ( 'simple()' , function ( ) {
16
24
hasConfigMethods ( [ 'content' , 'action' , 'capsule' , 'highlightAction' , 'theme' ] ) ;
17
25
18
26
it ( 'supports a basic toast' , inject ( function ( $mdToast , $rootScope , $timeout , $animate ) {
19
- var resolved = false ;
27
+ var openAndclosed = false ;
20
28
var parent = angular . element ( '<div>' ) ;
21
29
$mdToast . show (
22
30
$mdToast . simple ( {
@@ -26,7 +34,7 @@ describe('$mdToast service', function() {
26
34
capsule : true
27
35
} )
28
36
) . then ( function ( ) {
29
- resolved = true ;
37
+ openAndclosed = true ;
30
38
} ) ;
31
39
$rootScope . $digest ( ) ;
32
40
expect ( parent . find ( 'span' ) . text ( ) ) . toBe ( 'Do something' ) ;
@@ -35,7 +43,7 @@ describe('$mdToast service', function() {
35
43
$animate . triggerCallbacks ( ) ;
36
44
$timeout . flush ( ) ;
37
45
$animate . triggerCallbacks ( ) ;
38
- expect ( resolved ) . toBe ( true ) ;
46
+ expect ( openAndclosed ) . toBe ( true ) ;
39
47
} ) ) ;
40
48
41
49
it ( 'supports dynamicly updating the content' , inject ( function ( $mdToast , $rootScope , $rootElement ) {
@@ -125,41 +133,6 @@ describe('$mdToast service', function() {
125
133
126
134
describe ( 'build()' , function ( ) {
127
135
describe ( 'options' , function ( ) {
128
- it ( 'should hide current toast when showing new one' , inject ( function ( $rootElement ) {
129
- setup ( {
130
- template : '<md-toast class="one">'
131
- } ) ;
132
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.one' ) ) . toBeTruthy ( ) ;
133
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.two' ) ) . toBeFalsy ( ) ;
134
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.three' ) ) . toBeFalsy ( ) ;
135
-
136
- setup ( {
137
- template : '<md-toast class="two">'
138
- } ) ;
139
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.one' ) ) . toBeFalsy ( ) ;
140
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.two' ) ) . toBeTruthy ( ) ;
141
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.three' ) ) . toBeFalsy ( ) ;
142
-
143
- setup ( {
144
- template : '<md-toast class="three">'
145
- } ) ;
146
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.one' ) ) . toBeFalsy ( ) ;
147
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.two' ) ) . toBeFalsy ( ) ;
148
- expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.three' ) ) . toBeTruthy ( ) ;
149
- } ) ) ;
150
-
151
- it ( 'should hide after duration' , inject ( function ( $timeout , $animate , $rootElement ) {
152
- var parent = angular . element ( '<div>' ) ;
153
- var hideDelay = 1234 ;
154
- setup ( {
155
- template : '<md-toast />' ,
156
- hideDelay : hideDelay
157
- } ) ;
158
- expect ( $rootElement . find ( 'md-toast' ) . length ) . toBe ( 1 ) ;
159
- $timeout . flush ( hideDelay ) ;
160
- expect ( $rootElement . find ( 'md-toast' ) . length ) . toBe ( 0 ) ;
161
- } ) ) ;
162
-
163
136
it ( 'should have template' , inject ( function ( $timeout , $rootScope , $rootElement ) {
164
137
var parent = angular . element ( '<div>' ) ;
165
138
setup ( {
@@ -191,9 +164,11 @@ describe('$mdToast service', function() {
191
164
expect ( toast . hasClass ( 'md-left' ) ) . toBe ( true ) ;
192
165
} ) ) ;
193
166
} ) ;
167
+ } ) ;
194
168
195
- describe ( 'lifecycle' , function ( ) {
196
- it ( 'should hide current toast when showing new one' , inject ( function ( $rootElement ) {
169
+ describe ( 'lifecycle' , function ( ) {
170
+ describe ( 'should hide' , function ( ) {
171
+ it ( 'current toast when showing new one' , inject ( function ( $rootElement ) {
197
172
setup ( {
198
173
template : '<md-toast class="one">'
199
174
} ) ;
@@ -216,18 +191,116 @@ describe('$mdToast service', function() {
216
191
expect ( $rootElement [ 0 ] . querySelector ( 'md-toast.three' ) ) . toBeTruthy ( ) ;
217
192
} ) ) ;
218
193
219
- it ( 'should add class to toastParent' , inject ( function ( $rootElement ) {
194
+ it ( 'after duration' , inject ( function ( $timeout , $animate , $rootElement ) {
195
+ var parent = angular . element ( '<div>' ) ;
196
+ var hideDelay = 1234 ;
220
197
setup ( {
221
- template : '<md-toast>'
198
+ template : '<md-toast />' ,
199
+ hideDelay : hideDelay
222
200
} ) ;
223
- expect ( $rootElement . hasClass ( 'md-toast-open-bottom' ) ) . toBe ( true ) ;
201
+ expect ( $rootElement . find ( 'md-toast' ) . length ) . toBe ( 1 ) ;
202
+ $timeout . flush ( hideDelay ) ;
203
+ expect ( $rootElement . find ( 'md-toast' ) . length ) . toBe ( 0 ) ;
204
+ } ) ) ;
224
205
206
+ it ( 'and resolve with default `true`' , inject ( function ( $timeout , $animate , $mdToast ) {
207
+ var hideDelay = 1234 , result , fault ;
225
208
setup ( {
226
- template : '<md-toast>' ,
227
- position : 'top'
228
- } ) ;
229
- expect ( $rootElement . hasClass ( 'md-toast-open-top' ) ) . toBe ( true ) ;
209
+ template : '<md-toast />' ,
210
+ hideDelay : 1234
211
+ } ) . then (
212
+ function ( response ) { result = response ; } ,
213
+ function ( error ) { fault = error ; }
214
+ ) ;
215
+
216
+ $mdToast . hide ( ) ;
217
+
218
+ $timeout . flush ( ) ;
219
+ $animate . triggerCallbacks ( ) ;
220
+
221
+ expect ( result ) . toBe ( true ) ;
222
+ expect ( angular . isUndefined ( fault ) ) . toBe ( true ) ;
223
+
224
+ } ) ) ;
225
+
226
+ it ( 'and resolve with specified value' , inject ( function ( $timeout , $animate , $mdToast ) {
227
+ var hideDelay = 1234 , result , fault ;
228
+ setup ( {
229
+ template : '<md-toast />' ,
230
+ hideDelay : 1234
231
+ } ) . then (
232
+ function ( response ) { result = response ; } ,
233
+ function ( error ) { fault = error ; }
234
+ ) ;
235
+
236
+ $mdToast . hide ( "secret" ) ;
237
+
238
+ $timeout . flush ( ) ;
239
+ $animate . triggerCallbacks ( ) ;
240
+
241
+ expect ( result ) . toBe ( "secret" ) ;
242
+ expect ( angular . isUndefined ( fault ) ) . toBe ( true ) ;
243
+
244
+ } ) ) ;
245
+
246
+ it ( 'and resolve `true` after timeout' , inject ( function ( $timeout , $animate ) {
247
+ var hideDelay = 1234 , result , fault ;
248
+ setup ( {
249
+ template : '<md-toast />' ,
250
+ hideDelay : 1234
251
+ } ) . then (
252
+ function ( response ) { result = response ; } ,
253
+ function ( error ) { fault = error ; }
254
+ ) ;
255
+
256
+ $timeout . flush ( ) ;
257
+ $animate . triggerCallbacks ( ) ;
258
+
259
+ expect ( result ) . toBe ( true ) ;
260
+ expect ( angular . isUndefined ( fault ) ) . toBe ( true ) ;
261
+
262
+ } ) ) ;
263
+
264
+ it ( 'and resolve `ok` with click on OK button' , inject ( function ( $mdToast , $rootScope , $timeout , $animate ) {
265
+ var result , fault ;
266
+ var parent = angular . element ( '<div>' ) ;
267
+ var toast = $mdToast . simple ( {
268
+ parent : parent ,
269
+ content : 'Do something'
270
+ } ) . action ( 'Close with "ok" response' ) ;
271
+
272
+ $mdToast
273
+ . show ( toast )
274
+ . then (
275
+ function ( response ) { result = response ; } ,
276
+ function ( error ) { fault = error ; }
277
+ ) ;
278
+ $rootScope . $digest ( ) ;
279
+ $animate . triggerCallbacks ( ) ;
280
+
281
+ parent . find ( 'button' ) . triggerHandler ( 'click' ) ;
282
+
283
+ $timeout . flush ( ) ;
284
+ $animate . triggerCallbacks ( ) ;
285
+
286
+ expect ( result ) . toBe ( 'ok' ) ;
287
+ expect ( angular . isUndefined ( fault ) ) . toBe ( true ) ;
288
+
230
289
} ) ) ;
231
290
} ) ;
291
+
292
+ it ( 'should add class to toastParent' , inject ( function ( $rootElement ) {
293
+ setup ( {
294
+ template : '<md-toast>'
295
+ } ) ;
296
+ expect ( $rootElement . hasClass ( 'md-toast-open-bottom' ) ) . toBe ( true ) ;
297
+
298
+ setup ( {
299
+ template : '<md-toast>' ,
300
+ position : 'top'
301
+ } ) ;
302
+ expect ( $rootElement . hasClass ( 'md-toast-open-top' ) ) . toBe ( true ) ;
303
+ } ) ) ;
232
304
} ) ;
305
+
233
306
} ) ;
0 commit comments