11describe ( 'cgBusy' , function ( ) {
22
3- beforeEach ( module ( 'app' ) ) ;
3+ beforeEach ( module ( 'app' ) ) ;
44
5- var scope , compile , q , httpBackend ;
5+ var scope , compile , q , httpBackend , timeout ;
66
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 ) {
1314
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+ } ) ) ;
1718
18- it ( 'should show the overlay during promise' , function ( ) {
19+ it ( 'should show the overlay during promise' , function ( ) {
1920
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 ) ;
2223
23- this . testPromise = q . defer ( ) ;
24- scope . my_promise = this . testPromise . promise ;
24+ this . testPromise = q . defer ( ) ;
25+ scope . my_promise = this . testPromise . promise ;
2526
26- //httpBackend.flush();
27+ //httpBackend.flush();
2728
28- scope . $apply ( ) ;
29+ scope . $apply ( ) ;
2930
30- expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
31+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
3132
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)
3334
34- this . testPromise . resolve ( ) ;
35- scope . $apply ( ) ;
35+ this . testPromise . resolve ( ) ;
36+ scope . $apply ( ) ;
3637
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+ } ) ;
3940
40- it ( 'should show the overlay during multiple promises' , function ( ) {
41+ it ( 'should show the overlay during multiple promises' , function ( ) {
4142
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 ) ;
4445
45- this . testPromise = q . defer ( ) ;
46- scope . my_promise = this . testPromise . promise ;
46+ this . testPromise = q . defer ( ) ;
47+ scope . my_promise = this . testPromise . promise ;
4748
48- this . testPromise2 = q . defer ( ) ;
49- scope . my_promise2 = this . testPromise2 . promise ;
49+ this . testPromise2 = q . defer ( ) ;
50+ scope . my_promise2 = this . testPromise2 . promise ;
5051
51- //httpBackend.flush();
52+ //httpBackend.flush();
5253
53- scope . $apply ( ) ;
54+ scope . $apply ( ) ;
5455
55- expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
56+ expect ( this . element . children ( ) . length ) . toBe ( 1 ) ; //ensure element is added
5657
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)
5859
59- this . testPromise . resolve ( ) ;
60- scope . $apply ( ) ;
60+ this . testPromise . resolve ( ) ;
61+ scope . $apply ( ) ;
6162
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)
6364
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+ } ) ;
6869
69- it ( 'should load custom templates' , function ( ) {
70+ it ( 'should load custom templates' , function ( ) {
7071
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 ) ;
7374
74- httpBackend . flush ( ) ;
75+ httpBackend . flush ( ) ;
7576
76- scope . $apply ( ) ;
77+ scope . $apply ( ) ;
7778
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+ } ) ;
79136
80- } )
81137} ) ;
0 commit comments