forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloading.unit.js
More file actions
256 lines (233 loc) · 10.2 KB
/
loading.unit.js
File metadata and controls
256 lines (233 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
describe('$ionicLoading service', function() {
beforeEach(module('ionic', function($provide) {
//Set default options to blank for the sake of tests
$provide.constant('$ionicLoadingConfig', {});
}));
it('should reuse loader instance for getLoader', inject(function($ionicLoading) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
var loader2 = TestUtil.unwrapPromise($ionicLoading._getLoader());
expect(loader).toBe(loader2);
}));
describe('.show()', function() {
it('should retain backdrop if !noBackdrop and !isShown', inject(function($ionicLoading, $ionicBackdrop) {
spyOn($ionicBackdrop, 'retain');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({});
expect($ionicBackdrop.retain).toHaveBeenCalled();
}));
it('should not retain backdrop if noBackdrop', inject(function($ionicLoading, $ionicBackdrop) {
spyOn($ionicBackdrop, 'retain');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({ noBackdrop: true });
expect($ionicBackdrop.retain).not.toHaveBeenCalled();
}));
it('should not retain backdrop if isShown', inject(function($ionicLoading, $ionicBackdrop) {
spyOn($ionicBackdrop, 'retain');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.isShown = true;
loader.show({});
expect($ionicBackdrop.retain).not.toHaveBeenCalled();
}));
it('should not timeout if no duration', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({});
expect(loader.durationTimeout).toBeFalsy();
}));
it('should timeout if duration', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({ duration: 1000 });
expect(loader.durationTimeout).toBeTruthy();
expect(loader.durationTimeout.$$timeoutId).toBeTruthy();
}));
it('should add active', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
ionic.requestAnimationFrame = function(cb) { cb(); };
expect(loader.element.hasClass('active')).toBe(false);
loader.show({});
$timeout.flush();
expect(loader.element.hasClass('active')).toBe(true);
}));
it('should isShown = true', inject(function($ionicLoading) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
expect(loader.isShown).toBeFalsy();
loader.show({});
expect(loader.isShown).toBe(true);
}));
it('should use options.template', inject(function($ionicLoading, $rootScope) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({ template: 'foo {{"bar"}}' });
$rootScope.$apply();
expect(loader.element.text()).toBe('foo bar');
}));
it('should use options.templateUrl', inject(function($ionicLoading, $rootScope, $ionicTemplateLoader, $q) {
spyOn($ionicTemplateLoader, 'load').andReturn($q.when('{{1}} content'));
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({ templateUrl: 'template.html' });
expect($ionicTemplateLoader.load).toHaveBeenCalledWith('template.html');
$rootScope.$apply();
expect(loader.element.text()).toBe('1 content');
}));
it('should add and remove backdrop-loading to backdrop', inject(function($ionicLoading, $ionicBackdrop) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.show({ templateUrl: 'template.html' });
expect($ionicBackdrop._element.hasClass('backdrop-loading')).toBe(true);
loader.hide();
expect($ionicBackdrop._element.hasClass('backdrop-loading')).toBe(false);
}));
it('should add and remove loading-active class to body', inject(function($ionicLoading, $timeout) {
// used by _modal.scss to prevent clicks on modal screen when loading is active
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
ionic.requestAnimationFrame = function(cb) { cb(); };
loader.show({});
$timeout.flush();
expect(angular.element(document.body).hasClass('loading-active')).toBe(true);
loader.hide();
expect(angular.element(document.body).hasClass('loading-active')).toBe(false);
}));
});
describe('.hide()', function() {
it('should release backdrop if hasBackdrop and isShown', inject(function($ionicLoading, $ionicBackdrop) {
spyOn($ionicBackdrop, 'release');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.isShown = true;
loader.hasBackdrop = true;
loader.hide();
expect($ionicBackdrop.release).toHaveBeenCalled();
}));
it('should not release backdrop if !hasBackdrop', inject(function($ionicLoading, $ionicBackdrop) {
spyOn($ionicBackdrop, 'release');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.isShown = true;
loader.hide();
expect($ionicBackdrop.release).not.toHaveBeenCalled();
}));
it('should cancel durationTimeout and set isShown to false', inject(function($ionicLoading, $timeout) {
spyOn($timeout, 'cancel');
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
loader.durationTimeout = {};
loader.isShown = true;
loader.hide({});
expect($timeout.cancel).toHaveBeenCalledWith(loader.durationTimeout);
expect(loader.isShown).toBe(false);
}));
});
it('should show with options', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
spyOn(loader, 'show');
var options = {};
$ionicLoading.show(options);
$timeout.flush();
expect(loader.show).toHaveBeenCalledWith(options);
}));
it('should $timeout.cancel & hide', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
spyOn($timeout, 'cancel');
spyOn(loader, 'hide');
$ionicLoading.hide();
expect($timeout.cancel).toHaveBeenCalled();
$rootScope.$apply();
expect(loader.hide).toHaveBeenCalled();
}));
it('hide should cancel show delay and just go ahead and hide', inject(function($ionicLoading, $timeout) {
ionic.requestAnimationFrame = function(cb) { cb(); };
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
spyOn(loader, 'hide').andCallThrough();
spyOn(loader, 'show').andCallThrough();
$ionicLoading.show({ delay: 1000 });
$ionicLoading.hide();
expect(loader.show).not.toHaveBeenCalled();
expect(loader.hide).not.toHaveBeenCalled();
$timeout.flush();
expect(loader.show).not.toHaveBeenCalled();
expect(loader.hide).toHaveBeenCalled();
expect(loader.isShown).toBe(false);
expect(loader.element.hasClass('active')).toBe(false);
}));
describe("back button", function() {
it('.show() should register back button action', inject(function($ionicLoading, $ionicPlatform, $timeout, IONIC_BACK_PRIORITY) {
spyOn($ionicPlatform, 'registerBackButtonAction');
$ionicLoading.show();
$timeout.flush();
expect($ionicPlatform.registerBackButtonAction).toHaveBeenCalledWith(
angular.noop,
IONIC_BACK_PRIORITY.loading
);
}));
it('.hide() should deregister back button action', inject(function($ionicLoading, $ionicPlatform, $timeout) {
var deregisterSpy = jasmine.createSpy('deregister');
spyOn($ionicPlatform, 'registerBackButtonAction').andReturn(deregisterSpy);
$ionicLoading.show();
$timeout.flush();
expect(deregisterSpy).not.toHaveBeenCalled();
$ionicLoading.hide();
$timeout.flush();
expect(deregisterSpy).toHaveBeenCalled();
}));
});
it('should use options.hideOnStateChange to hide on $stateChangeSuccess', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
hideOnStateChange: true,
template: ''
});
spyOn(loader, 'hide');
$rootScope.$broadcast('$stateChangeSuccess');
$rootScope.$apply();
expect(loader.hide).toHaveBeenCalled();
}));
it('should use options.hideOnStateChange to hide on $stateChangeError', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
hideOnStateChange: true,
template: ''
});
spyOn(loader, 'hide');
$rootScope.$broadcast('$stateChangeError');
$rootScope.$apply();
expect(loader.hide).toHaveBeenCalled();
}));
it('should default false options.hideOnStateChange', inject(function($ionicLoading, $rootScope, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
template: ''
});
spyOn(loader, 'hide');
$rootScope.$broadcast('$stateChangeSuccess');
$rootScope.$apply();
expect(loader.hide).not.toHaveBeenCalled();
}));
});
describe('$ionicLoadingConfig', function() {
beforeEach(module('ionic', function($provide) {
$provide.constant('$ionicLoadingConfig', {
template: 'some template'
});
}));
it('should use $ionicLoadingConfig options by default', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show();
$timeout.flush();
expect(loader.element.text()).toBe('some template');
}));
it('should allow override', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
template: 'some other template'
});
$timeout.flush();
expect(loader.element.text()).toBe('some other template');
}));
it('should use the original defaults with subsequent calls', inject(function($ionicLoading, $timeout) {
var loader = TestUtil.unwrapPromise($ionicLoading._getLoader());
$ionicLoading.show({
template: 'some other template'
});
$timeout.flush();
expect(loader.element.text()).toBe('some other template');
$ionicLoading.hide();
$timeout.flush();
$ionicLoading.show();
$timeout.flush();
expect(loader.element.text()).toBe('some template');
}));
});