Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 266da34

Browse files
gyx1000pkozlowski-opensource
authored andcommitted
test($httpBackend): use browser.defer mock instead of fakeTimeout
1 parent 9474ec1 commit 266da34

File tree

1 file changed

+9
-38
lines changed

1 file changed

+9
-38
lines changed

test/ng/httpBackendSpec.js

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,7 @@
44
describe('$httpBackend', function() {
55

66
var $backend, $browser, callbacks,
7-
xhr, fakeDocument, callback,
8-
fakeTimeoutId = 0;
9-
10-
// TODO(vojta): should be replaced by $defer mock
11-
function fakeTimeout(fn, delay) {
12-
fakeTimeout.fns.push(fn);
13-
fakeTimeout.delays.push(delay);
14-
fakeTimeout.ids.push(++fakeTimeoutId);
15-
return fakeTimeoutId;
16-
}
17-
18-
fakeTimeout.fns = [];
19-
fakeTimeout.delays = [];
20-
fakeTimeout.ids = [];
21-
fakeTimeout.flush = function() {
22-
var len = fakeTimeout.fns.length;
23-
fakeTimeout.delays = [];
24-
fakeTimeout.ids = [];
25-
while (len--) fakeTimeout.fns.shift()();
26-
};
27-
fakeTimeout.cancel = function(id) {
28-
var i = fakeTimeout.ids.indexOf(id);
29-
if (i >= 0) {
30-
fakeTimeout.fns.splice(i, 1);
31-
fakeTimeout.delays.splice(i, 1);
32-
fakeTimeout.ids.splice(i, 1);
33-
return true;
34-
}
35-
return false;
36-
};
7+
xhr, fakeDocument, callback;
378

389

3910
beforeEach(inject(function($injector) {
@@ -57,7 +28,7 @@ describe('$httpBackend', function() {
5728
})
5829
}
5930
};
60-
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
31+
$backend = createHttpBackend($browser, createMockXhr, $browser.defer, callbacks, fakeDocument);
6132
callback = jasmine.createSpy('done');
6233
}));
6334

@@ -154,7 +125,7 @@ describe('$httpBackend', function() {
154125
xhr = MockXhr.$$lastInstance;
155126
spyOn(xhr, 'abort');
156127

157-
fakeTimeout.flush();
128+
$browser.defer.flush();
158129
expect(xhr.abort).toHaveBeenCalledOnce();
159130

160131
xhr.status = 0;
@@ -171,9 +142,9 @@ describe('$httpBackend', function() {
171142
xhr = MockXhr.$$lastInstance;
172143
spyOn(xhr, 'abort');
173144

174-
expect(fakeTimeout.delays[0]).toBe(2000);
145+
expect($browser.deferredFns[0].time).toBe(2000);
175146

176-
fakeTimeout.flush();
147+
$browser.defer.flush();
177148
expect(xhr.abort).toHaveBeenCalledOnce();
178149

179150
xhr.status = 0;
@@ -227,13 +198,13 @@ describe('$httpBackend', function() {
227198
xhr = MockXhr.$$lastInstance;
228199
spyOn(xhr, 'abort');
229200

230-
expect(fakeTimeout.delays[0]).toBe(2000);
201+
expect($browser.deferredFns[0].time).toBe(2000);
231202

232203
xhr.status = 200;
233204
xhr.onload();
234205
expect(callback).toHaveBeenCalledOnce();
235206

236-
expect(fakeTimeout.delays.length).toBe(0);
207+
expect($browser.deferredFns.length).toBe(0);
237208
expect(xhr.abort).not.toHaveBeenCalled();
238209
});
239210

@@ -342,12 +313,12 @@ describe('$httpBackend', function() {
342313

343314
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, 2000);
344315
expect(fakeDocument.$$scripts.length).toBe(1);
345-
expect(fakeTimeout.delays[0]).toBe(2000);
316+
expect($browser.deferredFns[0].time).toBe(2000);
346317

347318
var script = fakeDocument.$$scripts.shift(),
348319
callbackId = script.src.match(SCRIPT_URL)[2];
349320

350-
fakeTimeout.flush();
321+
$browser.defer.flush();
351322
expect(fakeDocument.$$scripts.length).toBe(0);
352323
expect(callback).toHaveBeenCalledOnce();
353324

0 commit comments

Comments
 (0)