1
1
describe ( 'collapse directive' , function ( ) {
2
- var element , compileFn , scope , $compile , $animate ;
2
+ var element , compileFn , scope , $compile , $animate , $q ;
3
3
4
4
beforeEach ( module ( 'ui.bootstrap.collapse' ) ) ;
5
5
beforeEach ( module ( 'ngAnimateMock' ) ) ;
6
- beforeEach ( inject ( function ( _$rootScope_ , _$compile_ , _$animate_ ) {
6
+ beforeEach ( inject ( function ( _$rootScope_ , _$compile_ , _$animate_ , _$q_ ) {
7
7
scope = _$rootScope_ ;
8
8
$compile = _$compile_ ;
9
9
$animate = _$animate_ ;
10
+ $q = _$q_ ;
10
11
} ) ) ;
11
12
12
13
beforeEach ( function ( ) {
13
- element = angular . element ( '<div uib-collapse="isCollapsed">Some Content</div>' ) ;
14
+ element = angular . element (
15
+ '<div uib-collapse="isCollapsed" '
16
+ + 'expanding="expanding()" '
17
+ + 'expanded="expanded()" '
18
+ + 'collapsing="collapsing()" '
19
+ + 'collapsed="collapsed()">'
20
+ + 'Some Content</div>' ) ;
14
21
compileFn = $compile ( element ) ;
15
22
angular . element ( document . body ) . append ( element ) ;
16
23
} ) ;
@@ -19,30 +26,53 @@ describe('collapse directive', function() {
19
26
element . remove ( ) ;
20
27
} ) ;
21
28
29
+ function initCallbacks ( ) {
30
+ scope . collapsing = jasmine . createSpy ( 'scope.collapsing' ) ;
31
+ scope . collapsed = jasmine . createSpy ( 'scope.collapsed' ) ;
32
+ scope . expanding = jasmine . createSpy ( 'scope.expanding' ) ;
33
+ scope . expanded = jasmine . createSpy ( 'scope.expanded' ) ;
34
+ }
35
+
36
+ function assertCallbacks ( expected ) {
37
+ [ 'collapsing' , 'collapsed' , 'expanding' , 'expanded' ] . forEach ( function ( cbName ) {
38
+ if ( expected [ cbName ] ) {
39
+ expect ( scope [ cbName ] ) . toHaveBeenCalled ( ) ;
40
+ } else {
41
+ expect ( scope [ cbName ] ) . not . toHaveBeenCalled ( ) ;
42
+ }
43
+ } ) ;
44
+ }
45
+
22
46
it ( 'should be hidden on initialization if isCollapsed = true' , function ( ) {
47
+ initCallbacks ( ) ;
23
48
scope . isCollapsed = true ;
24
49
compileFn ( scope ) ;
25
50
scope . $digest ( ) ;
26
51
expect ( element . height ( ) ) . toBe ( 0 ) ;
52
+ assertCallbacks ( { collapsed : true } ) ;
27
53
} ) ;
28
54
29
55
it ( 'should collapse if isCollapsed = true on subsequent use' , function ( ) {
30
56
scope . isCollapsed = false ;
31
57
compileFn ( scope ) ;
32
58
scope . $digest ( ) ;
33
59
$animate . flush ( ) ;
60
+ initCallbacks ( ) ;
34
61
scope . isCollapsed = true ;
35
62
scope . $digest ( ) ;
36
63
$animate . flush ( ) ;
37
64
expect ( element . height ( ) ) . toBe ( 0 ) ;
65
+ assertCallbacks ( { collapsing : true , collapsed : true } ) ;
38
66
} ) ;
39
67
40
68
it ( 'should be shown on initialization if isCollapsed = false' , function ( ) {
69
+ initCallbacks ( ) ;
41
70
scope . isCollapsed = false ;
42
71
compileFn ( scope ) ;
43
72
scope . $digest ( ) ;
44
73
$animate . flush ( ) ;
45
74
expect ( element . height ( ) ) . not . toBe ( 0 ) ;
75
+ assertCallbacks ( { expanding : true , expanded : true } ) ;
46
76
} ) ;
47
77
48
78
it ( 'should expand if isCollapsed = false on subsequent use' , function ( ) {
@@ -53,13 +83,15 @@ describe('collapse directive', function() {
53
83
scope . isCollapsed = true ;
54
84
scope . $digest ( ) ;
55
85
$animate . flush ( ) ;
86
+ initCallbacks ( ) ;
56
87
scope . isCollapsed = false ;
57
88
scope . $digest ( ) ;
58
89
$animate . flush ( ) ;
59
90
expect ( element . height ( ) ) . not . toBe ( 0 ) ;
91
+ assertCallbacks ( { expanding : true , expanded : true } ) ;
60
92
} ) ;
61
93
62
- it ( 'should expand if isCollapsed = true on subsequent uses' , function ( ) {
94
+ it ( 'should collapse if isCollapsed = true on subsequent uses' , function ( ) {
63
95
scope . isCollapsed = false ;
64
96
compileFn ( scope ) ;
65
97
scope . $digest ( ) ;
@@ -70,10 +102,12 @@ describe('collapse directive', function() {
70
102
scope . isCollapsed = false ;
71
103
scope . $digest ( ) ;
72
104
$animate . flush ( ) ;
105
+ initCallbacks ( ) ;
73
106
scope . isCollapsed = true ;
74
107
scope . $digest ( ) ;
75
108
$animate . flush ( ) ;
76
109
expect ( element . height ( ) ) . toBe ( 0 ) ;
110
+ assertCallbacks ( { collapsing : true , collapsed : true } ) ;
77
111
} ) ;
78
112
79
113
it ( 'should change aria-expanded attribute' , function ( ) {
@@ -137,4 +171,95 @@ describe('collapse directive', function() {
137
171
expect ( element . height ( ) ) . toBeLessThan ( collapseHeight ) ;
138
172
} ) ;
139
173
} ) ;
174
+
175
+ describe ( 'expanding callback returning a promise' , function ( ) {
176
+ var defer , collapsedHeight ;
177
+
178
+ beforeEach ( function ( ) {
179
+ defer = $q . defer ( ) ;
180
+
181
+ scope . isCollapsed = true ;
182
+ scope . expanding = function ( ) {
183
+ return defer . promise ;
184
+ } ;
185
+ compileFn ( scope ) ;
186
+ scope . $digest ( ) ;
187
+ collapsedHeight = element . height ( ) ;
188
+
189
+ // set flag to expand ...
190
+ scope . isCollapsed = false ;
191
+ scope . $digest ( ) ;
192
+
193
+ // ... shouldn't expand yet ...
194
+ expect ( element . attr ( 'aria-expanded' ) ) . not . toBe ( 'true' ) ;
195
+ expect ( element . height ( ) ) . toBe ( collapsedHeight ) ;
196
+ } ) ;
197
+
198
+ it ( 'should wait for it to resolve before animating' , function ( ) {
199
+ defer . resolve ( ) ;
200
+
201
+ // should now expand
202
+ scope . $digest ( ) ;
203
+ $animate . flush ( ) ;
204
+
205
+ expect ( element . attr ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
206
+ expect ( element . height ( ) ) . toBeGreaterThan ( collapsedHeight ) ;
207
+ } ) ;
208
+
209
+ it ( 'should not animate if it rejects' , function ( ) {
210
+ defer . reject ( ) ;
211
+
212
+ // should NOT expand
213
+ scope . $digest ( ) ;
214
+
215
+ expect ( element . attr ( 'aria-expanded' ) ) . not . toBe ( 'true' ) ;
216
+ expect ( element . height ( ) ) . toBe ( collapsedHeight ) ;
217
+ } ) ;
218
+ } ) ;
219
+
220
+ describe ( 'collapsing callback returning a promise' , function ( ) {
221
+ var defer , expandedHeight ;
222
+
223
+ beforeEach ( function ( ) {
224
+ defer = $q . defer ( ) ;
225
+ scope . isCollapsed = false ;
226
+ scope . collapsing = function ( ) {
227
+ return defer . promise ;
228
+ } ;
229
+ compileFn ( scope ) ;
230
+ scope . $digest ( ) ;
231
+
232
+ expandedHeight = element . height ( ) ;
233
+
234
+ // set flag to collapse ...
235
+ scope . isCollapsed = true ;
236
+ scope . $digest ( ) ;
237
+
238
+ // ... but it shouldn't collapse yet ...
239
+ expect ( element . attr ( 'aria-expanded' ) ) . not . toBe ( 'false' ) ;
240
+ expect ( element . height ( ) ) . toBe ( expandedHeight ) ;
241
+ } ) ;
242
+
243
+ it ( 'should wait for it to resolve before animating' , function ( ) {
244
+ defer . resolve ( ) ;
245
+
246
+ // should now collapse
247
+ scope . $digest ( ) ;
248
+ $animate . flush ( ) ;
249
+
250
+ expect ( element . attr ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
251
+ expect ( element . height ( ) ) . toBeLessThan ( expandedHeight ) ;
252
+ } ) ;
253
+
254
+ it ( 'should not animate if it rejects' , function ( ) {
255
+ defer . reject ( ) ;
256
+
257
+ // should NOT collapse
258
+ scope . $digest ( ) ;
259
+
260
+ expect ( element . attr ( 'aria-expanded' ) ) . not . toBe ( 'false' ) ;
261
+ expect ( element . height ( ) ) . toBe ( expandedHeight ) ;
262
+ } ) ;
263
+ } ) ;
264
+
140
265
} ) ;
0 commit comments