forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeferSpec.js
76 lines (54 loc) · 2 KB
/
deferSpec.js
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
describe('$defer', function() {
var scope, $browser, $defer, $exceptionHandler;
beforeEach(function(){
scope = angular.scope({}, angular.service,
{'$exceptionHandler': jasmine.createSpy('$exceptionHandler')});
$browser = scope.$service('$browser');
$defer = scope.$service('$defer');
$exceptionHandler = scope.$service('$exceptionHandler');
});
afterEach(function(){
dealoc(scope);
});
it('should delegate functions to $browser.defer', function() {
var counter = 0;
$defer(function() { counter++; });
expect(counter).toBe(0);
$browser.defer.flush();
expect(counter).toBe(1);
$browser.defer.flush(); //does nothing
expect(counter).toBe(1);
expect($exceptionHandler).not.toHaveBeenCalled();
});
it('should delegate exception to the $exceptionHandler service', function() {
$defer(function() {throw "Test Error";});
expect($exceptionHandler).not.toHaveBeenCalled();
$browser.defer.flush();
expect($exceptionHandler).toHaveBeenCalledWith("Test Error");
});
it('should call eval after each callback is executed', function() {
var eval = this.spyOn(scope, '$eval').andCallThrough();
$defer(function() {});
expect(eval).not.toHaveBeenCalled();
$browser.defer.flush();
expect(eval).toHaveBeenCalled();
eval.reset(); //reset the spy;
$defer(function() {});
$defer(function() {});
$browser.defer.flush();
expect(eval.callCount).toBe(2);
});
it('should call eval even if an exception is thrown in callback', function() {
var eval = this.spyOn(scope, '$eval').andCallThrough();
$defer(function() {throw "Test Error";});
expect(eval).not.toHaveBeenCalled();
$browser.defer.flush();
expect(eval).toHaveBeenCalled();
});
it('should allow you to specify the delay time', function(){
var defer = this.spyOn($browser, 'defer');
$defer(noop, 123);
expect(defer.callCount).toEqual(1);
expect(defer.mostRecentCall.args[1]).toEqual(123);
});
});