1
1
describe ( '$mdDialog' , function ( ) {
2
+ var triggerTransitionEnd ;
2
3
3
4
beforeEach ( module ( 'material.components.dialog' ) ) ;
4
-
5
5
beforeEach ( inject ( function spyOnMdEffects ( $$q , $animate ) {
6
+
6
7
spyOn ( $animate , 'leave' ) . and . callFake ( function ( element ) {
7
8
element . remove ( ) ;
8
9
return $$q . when ( ) ;
@@ -12,6 +13,22 @@ describe('$mdDialog', function() {
12
13
return $$q . when ( ) ;
13
14
} ) ;
14
15
} ) ) ;
16
+ beforeEach ( inject ( function ( $mdConstant , $rootScope , $animate , $timeout ) {
17
+ triggerTransitionEnd = function ( element , applyFlush ) {
18
+ // Defaults to 'true'... must explicitly set 'false'
19
+ if ( angular . isUndefined ( applyFlush ) ) applyFlush = true ;
20
+
21
+ $mdConstant . CSS . TRANSITIONEND . split ( " " )
22
+ . forEach ( function ( eventType ) {
23
+ element . triggerHandler ( eventType ) ;
24
+ } ) ;
25
+
26
+ $rootScope . $apply ( ) ;
27
+
28
+ applyFlush && $animate . triggerCallbacks ( ) ;
29
+ applyFlush && $timeout . flush ( ) ;
30
+ }
31
+ } ) ) ;
15
32
16
33
describe ( '#alert()' , function ( ) {
17
34
hasConfigurationMethods ( 'alert' , [
@@ -35,9 +52,9 @@ describe('$mdDialog', function() {
35
52
} ) ;
36
53
$rootScope . $apply ( ) ;
37
54
$animate . triggerCallbacks ( ) ;
55
+
38
56
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
39
- container . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
40
- $rootScope . $apply ( ) ;
57
+ triggerTransitionEnd ( container . find ( 'md-dialog' ) ) ;
41
58
42
59
var title = angular . element ( parent [ 0 ] . querySelector ( 'h2' ) ) ;
43
60
expect ( title . text ( ) ) . toBe ( 'Title' ) ;
@@ -56,7 +73,7 @@ describe('$mdDialog', function() {
56
73
$rootScope . $apply ( ) ;
57
74
58
75
var dialog = parent . find ( 'md-dialog' ) ;
59
- dialog . triggerHandler ( 'transitionend' ) ;
76
+ triggerTransitionEnd ( dialog ) ;
60
77
expect ( dialog . attr ( 'role' ) ) . toBe ( 'alertdialog' ) ;
61
78
62
79
$rootScope . $apply ( ) ;
@@ -65,7 +82,7 @@ describe('$mdDialog', function() {
65
82
} ) ) ;
66
83
67
84
68
- it ( 'should focus `md-dialog-content` on open' , inject ( function ( $mdDialog , $rootScope , $document , $timeout , $mdConstant ) {
85
+ it ( 'should focus `md-dialog-content` on open' , inject ( function ( $mdDialog , $rootScope , $document ) {
69
86
jasmine . mockElementFocus ( this ) ;
70
87
71
88
var parent = angular . element ( '<div>' ) ;
@@ -83,14 +100,7 @@ describe('$mdDialog', function() {
83
100
) ;
84
101
85
102
$rootScope . $apply ( ) ;
86
- $timeout . flush ( ) ;
87
-
88
- var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
89
- container . triggerHandler ( 'transitionend' ) ;
90
- $rootScope . $apply ( ) ;
91
-
92
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
93
- $rootScope . $apply ( ) ;
103
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
94
104
95
105
expect ( $document . activeElement ) . toBe ( parent [ 0 ] . querySelector ( 'md-dialog-content' ) ) ;
96
106
} ) ) ;
@@ -102,7 +112,7 @@ describe('$mdDialog', function() {
102
112
'ok' , 'cancel' , 'targetEvent' , 'theme'
103
113
] ) ;
104
114
105
- it ( 'shows a basic confirm dialog' , inject ( function ( $rootScope , $mdDialog , $animate , $mdConstant ) {
115
+ it ( 'shows a basic confirm dialog' , inject ( function ( $rootScope , $mdDialog , $animate ) {
106
116
var parent = angular . element ( '<div>' ) ;
107
117
var rejected = false ;
108
118
$mdDialog . show (
@@ -119,10 +129,9 @@ describe('$mdDialog', function() {
119
129
120
130
$rootScope . $apply ( ) ;
121
131
$animate . triggerCallbacks ( ) ;
132
+
122
133
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
123
- container . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
124
- $rootScope . $apply ( ) ;
125
- $animate . triggerCallbacks ( ) ;
134
+ triggerTransitionEnd ( container . find ( 'md-dialog' ) ) ;
126
135
127
136
var title = parent . find ( 'h2' ) ;
128
137
expect ( title . text ( ) ) . toBe ( 'Title' ) ;
@@ -137,13 +146,11 @@ describe('$mdDialog', function() {
137
146
138
147
buttons . eq ( 1 ) . triggerHandler ( 'click' ) ;
139
148
$rootScope . $digest ( ) ;
140
-
141
149
$animate . triggerCallbacks ( ) ;
150
+
142
151
var dialog = parent . find ( 'md-dialog' ) ;
143
- dialog . triggerHandler ( 'transitionend' ) ;
152
+ triggerTransitionEnd ( dialog ) ;
144
153
expect ( dialog . attr ( 'role' ) ) . toBe ( 'dialog' ) ;
145
- $rootScope . $digest ( ) ;
146
- $animate . triggerCallbacks ( ) ;
147
154
148
155
expect ( parent . find ( 'h2' ) . length ) . toBe ( 0 ) ;
149
156
expect ( rejected ) . toBe ( true ) ;
@@ -164,14 +171,7 @@ describe('$mdDialog', function() {
164
171
} ) ;
165
172
166
173
$rootScope . $apply ( ) ;
167
- $timeout . flush ( ) ;
168
-
169
- var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
170
- container . triggerHandler ( 'transitionend' ) ;
171
- $rootScope . $apply ( ) ;
172
-
173
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
174
- $rootScope . $apply ( ) ;
174
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
175
175
176
176
expect ( $document . activeElement ) . toBe ( parent [ 0 ] . querySelector ( '.dialog-close' ) ) ;
177
177
} ) ) ;
@@ -197,8 +197,7 @@ describe('$mdDialog', function() {
197
197
expect ( ready ) . toBe ( false ) ;
198
198
199
199
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
200
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
201
- $rootScope . $apply ( ) ;
200
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
202
201
203
202
container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
204
203
expect ( container . length ) . toBe ( 1 ) ;
@@ -224,8 +223,7 @@ describe('$mdDialog', function() {
224
223
expect ( closing ) . toBe ( false ) ;
225
224
226
225
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
227
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
228
- $rootScope . $apply ( ) ;
226
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
229
227
230
228
parent . triggerHandler ( { type : 'keyup' ,
231
229
keyCode : $mdConstant . KEY_CODE . ESCAPE
@@ -263,17 +261,15 @@ describe('$mdDialog', function() {
263
261
$rootScope . $apply ( ) ;
264
262
265
263
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
266
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
267
- $rootScope . $apply ( ) ;
264
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
268
265
269
266
expect ( parent . find ( 'md-dialog' ) . length ) . toBe ( 1 ) ;
270
267
271
268
parent . triggerHandler ( { type : 'keyup' ,
272
269
keyCode : $mdConstant . KEY_CODE . ESCAPE
273
270
} ) ;
274
271
$timeout . flush ( ) ;
275
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
276
- $rootScope . $apply ( ) ;
272
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
277
273
278
274
expect ( parent . find ( 'md-dialog' ) . length ) . toBe ( 0 ) ;
279
275
} ) ) ;
@@ -288,9 +284,7 @@ describe('$mdDialog', function() {
288
284
$rootScope . $apply ( ) ;
289
285
290
286
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
291
- container . triggerHandler ( 'transitionend' ) ;
292
- $rootScope . $apply ( ) ;
293
-
287
+ triggerTransitionEnd ( container ) ;
294
288
expect ( parent . find ( 'md-dialog' ) . length ) . toBe ( 1 ) ;
295
289
296
290
$rootElement . triggerHandler ( { type : 'keyup' , keyCode : $mdConstant . KEY_CODE . ESCAPE } ) ;
@@ -311,19 +305,15 @@ describe('$mdDialog', function() {
311
305
$rootScope . $apply ( ) ;
312
306
313
307
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
314
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
315
- $rootScope . $apply ( ) ;
316
-
308
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
317
309
expect ( parent . find ( 'md-dialog' ) . length ) . toBe ( 1 ) ;
318
310
319
311
container . triggerHandler ( {
320
312
type : 'click' ,
321
313
target : container [ 0 ]
322
314
} ) ;
323
315
$timeout . flush ( ) ;
324
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
325
- $rootScope . $apply ( ) ;
326
-
316
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
327
317
expect ( parent . find ( 'md-dialog' ) . length ) . toBe ( 0 ) ;
328
318
} ) ) ;
329
319
@@ -406,13 +396,7 @@ describe('$mdDialog', function() {
406
396
} ) ;
407
397
408
398
$rootScope . $apply ( ) ;
409
- $timeout . flush ( ) ;
410
-
411
- var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
412
- container . triggerHandler ( 'transitionend' ) ;
413
- $rootScope . $apply ( ) ;
414
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
415
- $rootScope . $apply ( ) ;
399
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
416
400
417
401
expect ( $document . activeElement ) . toBe ( parent [ 0 ] . querySelector ( '#focus-target' ) ) ;
418
402
} ) ) ;
@@ -437,29 +421,12 @@ describe('$mdDialog', function() {
437
421
$timeout . flush ( ) ;
438
422
439
423
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
440
- container . triggerHandler ( 'transitionend' ) ;
441
- $rootScope . $apply ( ) ;
442
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
443
- $rootScope . $apply ( ) ;
424
+ triggerTransitionEnd ( container ) ;
425
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
444
426
445
427
expect ( $document . activeElement ) . toBe ( undefined ) ;
446
428
} ) ) ;
447
429
448
- /**
449
- * Verifies that an element has the expected CSS for its transform property.
450
- * Works by creating a new element, setting the expected CSS on that
451
- * element, and comparing to the element being tested. This convoluted
452
- * approach is needed because if jQuery is installed it can rewrite
453
- * 'translate3d' values to equivalent 'matrix' values, for example turning
454
- * 'translate3d(240px, 120px, 0px) scale(0.5, 0.5)' into
455
- * 'matrix(0.5, 0, 0, 0.5, 240, 120)'.
456
- */
457
- var verifyTransformCss = function ( element , transformAttr , expectedCss ) {
458
- var testDiv = angular . element ( '<div>' ) ;
459
- testDiv . css ( transformAttr , expectedCss ) ;
460
- expect ( element . css ( transformAttr ) ) . toBe ( testDiv . css ( transformAttr ) ) ;
461
- } ;
462
-
463
430
it ( 'should expand from and shrink to targetEvent element' , inject ( function ( $mdDialog , $rootScope , $timeout , $mdConstant ) {
464
431
// Create a targetEvent parameter pointing to a fake element with a
465
432
// defined bounding rectangle.
@@ -482,8 +449,7 @@ describe('$mdDialog', function() {
482
449
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
483
450
var dialog = parent . find ( 'md-dialog' ) ;
484
451
485
- dialog . triggerHandler ( 'transitionend' ) ;
486
- $rootScope . $apply ( ) ;
452
+ triggerTransitionEnd ( dialog , false ) ;
487
453
488
454
// The dialog's bounding rectangle is always zero size and position in
489
455
// these tests, so the target of the CSS transform should be the midpoint
@@ -529,8 +495,7 @@ describe('$mdDialog', function() {
529
495
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
530
496
var dialog = parent . find ( 'md-dialog' ) ;
531
497
532
- dialog . triggerHandler ( 'transitionend' ) ;
533
- $rootScope . $apply ( ) ;
498
+ triggerTransitionEnd ( dialog , false ) ;
534
499
535
500
verifyTransformCss ( dialog , $mdConstant . CSS . TRANSFORM ,
536
501
'translate3d(240px, 120px, 0px) scale(0.5, 0.5)' ) ;
@@ -573,12 +538,10 @@ describe('$mdDialog', function() {
573
538
var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
574
539
var dialog = parent . find ( 'md-dialog' ) ;
575
540
576
- $timeout . flush ( ) ;
577
541
verifyTransformCss ( dialog , $mdConstant . CSS . TRANSFORM ,
578
542
'translate3d(240px, 120px, 0px) scale(0.5, 0.5)' ) ;
579
543
580
- dialog . triggerHandler ( 'transitionend' ) ;
581
- $rootScope . $apply ( ) ;
544
+ triggerTransitionEnd ( dialog , false ) ;
582
545
583
546
// Clear the animation CSS so we can be sure it gets reset.
584
547
dialog . css ( $mdConstant . CSS . TRANSFORM , '' ) ;
@@ -616,13 +579,7 @@ describe('$mdDialog', function() {
616
579
} ) ;
617
580
618
581
$rootScope . $apply ( ) ;
619
- $timeout . flush ( ) ;
620
-
621
- var container = angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ;
622
- container . triggerHandler ( 'transitionend' ) ;
623
- $rootScope . $apply ( ) ;
624
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
625
- $rootScope . $apply ( ) ;
582
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
626
583
627
584
expect ( $document . activeElement ) . toBe ( parent [ 0 ] . querySelector ( '#focus-target' ) ) ;
628
585
} ) ) ;
@@ -644,16 +601,9 @@ describe('$mdDialog', function() {
644
601
parent : parent
645
602
} ) ;
646
603
$rootScope . $apply ( ) ;
647
- $animate . triggerCallbacks ( ) ;
648
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
649
- $rootScope . $apply ( ) ;
650
- $animate . triggerCallbacks ( ) ;
604
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) , false ) ;
651
605
652
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
653
- $rootScope . $apply ( ) ;
654
- $animate . triggerCallbacks ( ) ;
655
- $rootScope . $apply ( ) ;
656
- $animate . triggerCallbacks ( ) ;
606
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
657
607
expect ( parent [ 0 ] . querySelectorAll ( 'md-dialog.one' ) . length ) . toBe ( 0 ) ;
658
608
expect ( parent [ 0 ] . querySelectorAll ( 'md-dialog.two' ) . length ) . toBe ( 1 ) ;
659
609
} ) ) ;
@@ -683,8 +633,7 @@ describe('$mdDialog', function() {
683
633
} ) ;
684
634
685
635
$rootScope . $apply ( ) ;
686
- angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) . triggerHandler ( 'transitionend' ) ;
687
- $rootScope . $apply ( ) ;
636
+ triggerTransitionEnd ( angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ) ;
688
637
$$rAF . flush ( ) ;
689
638
690
639
var dialog = angular . element ( parent [ 0 ] . querySelector ( 'md-dialog' ) ) ;
@@ -718,8 +667,7 @@ describe('$mdDialog', function() {
718
667
) ;
719
668
720
669
$rootScope . $apply ( ) ;
721
- angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) . triggerHandler ( 'transitionend' ) ;
722
- $rootScope . $apply ( ) ;
670
+ triggerTransitionEnd ( angular . element ( parent [ 0 ] . querySelector ( '.md-dialog-container' ) ) ) ;
723
671
724
672
var dialog = angular . element ( parent [ 0 ] . querySelector ( 'md-dialog' ) ) ;
725
673
expect ( dialog . attr ( 'aria-label' ) ) . toEqual ( 'label' ) ;
@@ -737,10 +685,7 @@ describe('$mdDialog', function() {
737
685
} ) ;
738
686
739
687
$rootScope . $apply ( ) ;
740
- $timeout . flush ( ) ;
741
-
742
- parent . find ( 'md-dialog' ) . triggerHandler ( 'transitionend' ) ;
743
- $rootScope . $apply ( ) ;
688
+ triggerTransitionEnd ( parent . find ( 'md-dialog' ) ) ;
744
689
745
690
var dialog = angular . element ( parent . find ( 'md-dialog' ) ) ;
746
691
expect ( dialog . attr ( 'aria-hidden' ) ) . toBe ( undefined ) ;
@@ -760,6 +705,22 @@ describe('$mdDialog', function() {
760
705
} ) ) ;
761
706
} ) ;
762
707
}
708
+
709
+ /**
710
+ * Verifies that an element has the expected CSS for its transform property.
711
+ * Works by creating a new element, setting the expected CSS on that
712
+ * element, and comparing to the element being tested. This convoluted
713
+ * approach is needed because if jQuery is installed it can rewrite
714
+ * 'translate3d' values to equivalent 'matrix' values, for example turning
715
+ * 'translate3d(240px, 120px, 0px) scale(0.5, 0.5)' into
716
+ * 'matrix(0.5, 0, 0, 0.5, 240, 120)'.
717
+ */
718
+ var verifyTransformCss = function ( element , transformAttr , expectedCss ) {
719
+ var testDiv = angular . element ( '<div>' ) ;
720
+ testDiv . css ( transformAttr , expectedCss ) ;
721
+ expect ( element . css ( transformAttr ) ) . toBe ( testDiv . css ( transformAttr ) ) ;
722
+ } ;
723
+
763
724
} ) ;
764
725
765
726
describe ( '$mdDialog with custom interpolation symbols' , function ( ) {
0 commit comments