1
1
describe ( 'cgBusy' , function ( ) {
2
2
3
- beforeEach ( module ( 'app' ) ) ;
3
+ beforeEach ( module ( 'app' ) ) ;
4
4
5
- var scope , compile , q , httpBackend ;
5
+ var scope , compile , q , httpBackend , timeout ;
6
6
7
- beforeEach ( inject ( function ( $rootScope , $compile , $q , $httpBackend , $templateCache ) {
8
- scope = $rootScope . $new ( ) ;
9
- compile = $compile ;
10
- q = $q ;
11
- httpBackend = $httpBackend ;
12
- httpBackend . whenGET ( 'test-custom-template.html' ) . respond ( function ( method , url , data , headers ) {
7
+ beforeEach ( inject ( function ( $rootScope , $compile , $q , $httpBackend , $templateCache , $timeout ) {
8
+ scope = $rootScope . $new ( ) ;
9
+ compile = $compile ;
10
+ q = $q ;
11
+ httpBackend = $httpBackend ;
12
+ timeout = $timeout ;
13
+ httpBackend . whenGET ( 'test-custom-template.html' ) . respond ( function ( method , url , data , headers ) {
13
14
14
- return [ [ 200 ] , '<div id="custom">test-custom-template-contents</div>' ] ;
15
- } ) ;
16
- } ) ) ;
15
+ return [ [ 200 ] , '<div id="custom">test-custom-template-contents</div>' ] ;
16
+ } ) ;
17
+ } ) ) ;
17
18
18
- it ( 'should show the overlay during promise' , function ( ) {
19
+ it ( 'should show the overlay during promise' , function ( ) {
19
20
20
- this . element = compile ( '<div cg-busy="my_promise"></div>' ) ( scope ) ;
21
- angular . element ( 'body' ) . append ( this . element ) ;
21
+ this . element = compile ( '<div cg-busy="my_promise"></div>' ) ( scope ) ;
22
+ angular . element ( 'body' ) . append ( this . element ) ;
22
23
23
- this . testPromise = q . defer ( ) ;
24
- scope . my_promise = this . testPromise . promise ;
24
+ this . testPromise = q . defer ( ) ;
25
+ scope . my_promise = this . testPromise . promise ;
25
26
26
- //httpBackend.flush();
27
+ //httpBackend.flush();
27
28
28
- scope . $apply ( ) ;
29
+ scope . $apply ( ) ;
29
30
30
- expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
31
+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
31
32
32
- expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its visible (promise is ongoing)
33
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its visible (promise is ongoing)
33
34
34
- this . testPromise . resolve ( ) ;
35
- scope . $apply ( ) ;
35
+ this . testPromise . resolve ( ) ;
36
+ scope . $apply ( ) ;
36
37
37
- expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ; //ensure its now invisible as the promise is resolved
38
- } ) ;
38
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ; //ensure its now invisible as the promise is resolved
39
+ } ) ;
39
40
40
- it ( 'should show the overlay during multiple promises' , function ( ) {
41
+ it ( 'should show the overlay during multiple promises' , function ( ) {
41
42
42
- this . element = compile ( '<div cg-busy="[my_promise,my_promise2]"></div>' ) ( scope ) ;
43
- angular . element ( 'body' ) . append ( this . element ) ;
43
+ this . element = compile ( '<div cg-busy="[my_promise,my_promise2]"></div>' ) ( scope ) ;
44
+ angular . element ( 'body' ) . append ( this . element ) ;
44
45
45
- this . testPromise = q . defer ( ) ;
46
- scope . my_promise = this . testPromise . promise ;
46
+ this . testPromise = q . defer ( ) ;
47
+ scope . my_promise = this . testPromise . promise ;
47
48
48
- this . testPromise2 = q . defer ( ) ;
49
- scope . my_promise2 = this . testPromise2 . promise ;
49
+ this . testPromise2 = q . defer ( ) ;
50
+ scope . my_promise2 = this . testPromise2 . promise ;
50
51
51
- //httpBackend.flush();
52
+ //httpBackend.flush();
52
53
53
- scope . $apply ( ) ;
54
+ scope . $apply ( ) ;
54
55
55
- expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
56
+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
56
57
57
- expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its visible (promise is ongoing)
58
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its visible (promise is ongoing)
58
59
59
- this . testPromise . resolve ( ) ;
60
- scope . $apply ( ) ;
60
+ this . testPromise . resolve ( ) ;
61
+ scope . $apply ( ) ;
61
62
62
- expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its still visible (promise is ongoing)
63
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ; //ensure its still visible (promise is ongoing)
63
64
64
- this . testPromise2 . resolve ( ) ;
65
- scope . $apply ( ) ;
66
- expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ; //ensure its now invisible as the promise is resolved
67
- } ) ;
65
+ this . testPromise2 . resolve ( ) ;
66
+ scope . $apply ( ) ;
67
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ; //ensure its now invisible as the promise is resolved
68
+ } ) ;
68
69
69
- it ( 'should load custom templates' , function ( ) {
70
+ it ( 'should load custom templates' , function ( ) {
70
71
71
- this . element = compile ( '<div cg-busy="{promise:my_promise,templateUrl:\'test-custom-template.html\'}"></div>' ) ( scope ) ;
72
- angular . element ( 'body' ) . append ( this . element ) ;
72
+ this . element = compile ( '<div cg-busy="{promise:my_promise,templateUrl:\'test-custom-template.html\'}"></div>' ) ( scope ) ;
73
+ angular . element ( 'body' ) . append ( this . element ) ;
73
74
74
- httpBackend . flush ( ) ;
75
+ httpBackend . flush ( ) ;
75
76
76
- scope . $apply ( ) ;
77
+ scope . $apply ( ) ;
77
78
78
- expect ( angular . element ( '#custom' ) . html ( ) ) . toBe ( 'test-custom-template-contents' ) ;
79
+ expect ( angular . element ( '#custom' ) . html ( ) ) . toBe ( 'test-custom-template-contents' ) ;
80
+
81
+ } ) ;
82
+
83
+ it ( 'should delay when delay provided.' , function ( ) {
84
+
85
+ this . element = compile ( '<div cg-busy="{promise:my_promise,delay:300}"></div>' ) ( scope ) ;
86
+ angular . element ( 'body' ) . append ( this . element ) ;
87
+
88
+ this . testPromise = q . defer ( ) ;
89
+ scope . my_promise = this . testPromise . promise ;
90
+
91
+ scope . $apply ( ) ;
92
+
93
+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
94
+
95
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ;
96
+
97
+ timeout . flush ( 200 ) ;
98
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ;
99
+
100
+ timeout . flush ( 301 ) ;
101
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ;
102
+ this . testPromise . resolve ( ) ;
103
+ scope . $apply ( ) ;
104
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ;
105
+
106
+ } ) ;
107
+
108
+ it ( 'should use minDuration correctly.' , function ( ) {
109
+
110
+ this . element = compile ( '<div cg-busy="{promise:my_promise,minDuration:1000}"></div>' ) ( scope ) ;
111
+ angular . element ( 'body' ) . append ( this . element ) ;
112
+
113
+ this . testPromise = q . defer ( ) ;
114
+ scope . my_promise = this . testPromise . promise ;
115
+
116
+ scope . $apply ( ) ;
117
+
118
+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
119
+
120
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ;
121
+
122
+ timeout . flush ( 200 ) ;
123
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ;
124
+
125
+ this . testPromise . resolve ( ) ;
126
+ timeout . flush ( 400 ) ;
127
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ;
128
+
129
+ timeout . flush ( 300 ) ; //900ms total
130
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'block' ) ;
131
+
132
+ timeout . flush ( 101 ) ; //1001ms total
133
+ expect ( this . element . children ( ) . css ( 'display' ) ) . toBe ( 'none' ) ;
134
+
135
+ } ) ;
79
136
80
- } )
81
137
} ) ;
0 commit comments