|
1 | 1 | describe('$defer', function() {
|
2 |
| - var scope, $browser, $defer, $exceptionHandler; |
3 |
| - |
4 |
| - beforeEach(function(){ |
5 |
| - scope = angular.scope(angular.service, |
6 |
| - {'$exceptionHandler': jasmine.createSpy('$exceptionHandler')}); |
7 |
| - $browser = scope.$service('$browser'); |
8 |
| - $defer = scope.$service('$defer'); |
9 |
| - $exceptionHandler = scope.$service('$exceptionHandler'); |
10 |
| - }); |
11 | 2 |
|
12 |
| - afterEach(function(){ |
13 |
| - dealoc(scope); |
14 |
| - }); |
| 3 | + describe('setTimeout backed deferral', function() { |
| 4 | + var scope, $browser, $defer, $exceptionHandler; |
15 | 5 |
|
| 6 | + beforeEach(function(){ |
| 7 | + scope = angular.scope(angular.service, |
| 8 | + {'$exceptionHandler': jasmine.createSpy('$exceptionHandler')}); |
| 9 | + $browser = scope.$service('$browser'); |
| 10 | + $defer = scope.$service('$defer'); |
| 11 | + $exceptionHandler = scope.$service('$exceptionHandler'); |
| 12 | + }); |
16 | 13 |
|
17 |
| - it('should delegate functions to $browser.defer', function() { |
18 |
| - var counter = 0; |
19 |
| - $defer(function() { counter++; }); |
| 14 | + afterEach(function(){ |
| 15 | + dealoc(scope); |
| 16 | + }); |
20 | 17 |
|
21 |
| - expect(counter).toBe(0); |
22 | 18 |
|
23 |
| - $browser.defer.flush(); |
24 |
| - expect(counter).toBe(1); |
| 19 | + it('should delegate functions to $browser.defer', function() { |
| 20 | + var counter = 0; |
| 21 | + $defer(function() { counter++; }); |
25 | 22 |
|
26 |
| - $browser.defer.flush(); //does nothing |
27 |
| - expect(counter).toBe(1); |
| 23 | + expect(counter).toBe(0); |
28 | 24 |
|
29 |
| - expect($exceptionHandler).not.toHaveBeenCalled(); |
30 |
| - }); |
| 25 | + $browser.defer.flush(); |
| 26 | + expect(counter).toBe(1); |
31 | 27 |
|
| 28 | + $browser.defer.flush(); //does nothing |
| 29 | + expect(counter).toBe(1); |
32 | 30 |
|
33 |
| - it('should delegate exception to the $exceptionHandler service', function() { |
34 |
| - $defer(function() {throw "Test Error";}); |
35 |
| - expect($exceptionHandler).not.toHaveBeenCalled(); |
| 31 | + expect($exceptionHandler).not.toHaveBeenCalled(); |
| 32 | + }); |
36 | 33 |
|
37 |
| - $browser.defer.flush(); |
38 |
| - expect($exceptionHandler).toHaveBeenCalledWith("Test Error"); |
39 |
| - }); |
40 | 34 |
|
| 35 | + it('should delegate exception to the $exceptionHandler service', function() { |
| 36 | + $defer(function() {throw "Test Error";}); |
| 37 | + expect($exceptionHandler).not.toHaveBeenCalled(); |
41 | 38 |
|
42 |
| - it('should call eval after each callback is executed', function() { |
43 |
| - var eval = this.spyOn(scope, '$eval').andCallThrough(); |
| 39 | + $browser.defer.flush(); |
| 40 | + expect($exceptionHandler).toHaveBeenCalledWith("Test Error"); |
| 41 | + }); |
44 | 42 |
|
45 |
| - $defer(function() {}); |
46 |
| - expect(eval).wasNotCalled(); |
47 | 43 |
|
48 |
| - $browser.defer.flush(); |
49 |
| - expect(eval).wasCalled(); |
| 44 | + it('should call $apply after each callback is executed', function() { |
| 45 | + var eval = this.spyOn(scope, '$apply').andCallThrough(); |
50 | 46 |
|
51 |
| - eval.reset(); //reset the spy; |
| 47 | + $defer(function() {}); |
| 48 | + expect(eval).wasNotCalled(); |
52 | 49 |
|
53 |
| - $defer(function() {}); |
54 |
| - $defer(function() {}); |
55 |
| - $browser.defer.flush(); |
56 |
| - expect(eval.callCount).toBe(2); |
57 |
| - }); |
| 50 | + $browser.defer.flush(); |
| 51 | + expect(eval).wasCalled(); |
| 52 | + |
| 53 | + eval.reset(); //reset the spy; |
58 | 54 |
|
| 55 | + $defer(function() {}); |
| 56 | + $defer(function() {}); |
| 57 | + $browser.defer.flush(); |
| 58 | + expect(eval.callCount).toBe(2); |
| 59 | + }); |
59 | 60 |
|
60 |
| - it('should call eval even if an exception is thrown in callback', function() { |
61 |
| - var eval = this.spyOn(scope, '$eval').andCallThrough(); |
62 | 61 |
|
63 |
| - $defer(function() {throw "Test Error";}); |
64 |
| - expect(eval).wasNotCalled(); |
| 62 | + it('should call $apply even if an exception is thrown in callback', function() { |
| 63 | + var eval = this.spyOn(scope, '$apply').andCallThrough(); |
65 | 64 |
|
66 |
| - $browser.defer.flush(); |
67 |
| - expect(eval).wasCalled(); |
| 65 | + $defer(function() {throw "Test Error";}); |
| 66 | + expect(eval).wasNotCalled(); |
| 67 | + |
| 68 | + $browser.defer.flush(); |
| 69 | + expect(eval).wasCalled(); |
| 70 | + }); |
| 71 | + |
| 72 | + |
| 73 | + it('should allow you to specify the delay time', function(){ |
| 74 | + var defer = this.spyOn($browser, 'defer'); |
| 75 | + $defer(noop, 123); |
| 76 | + expect(defer.callCount).toEqual(1); |
| 77 | + expect(defer.mostRecentCall.args[1]).toEqual(123); |
| 78 | + }); |
68 | 79 | });
|
69 | 80 |
|
70 |
| - it('should allow you to specify the delay time', function(){ |
71 |
| - var defer = this.spyOn($browser, 'defer'); |
72 |
| - $defer(noop, 123); |
73 |
| - expect(defer.callCount).toEqual(1); |
74 |
| - expect(defer.mostRecentCall.args[1]).toEqual(123); |
| 81 | + |
| 82 | + describe('queue based deferral', function() { |
| 83 | + var scope, defer, log; |
| 84 | + |
| 85 | + beforeEach(function() { |
| 86 | + scope = angular.scope(); |
| 87 | + $defer = scope.$service('$defer'); |
| 88 | + log = []; |
| 89 | + }); |
| 90 | + |
| 91 | + |
| 92 | + it('should allow a task to be scheduled and executed upon flush()', function() { |
| 93 | + var id = $defer('myQueue', function() { log.push('work'); }); |
| 94 | + expect(id).toBeDefined(); |
| 95 | + expect(log).toEqual([]); |
| 96 | + |
| 97 | + $defer.flush('wrongQueue'); |
| 98 | + expect(log).toEqual([]); |
| 99 | + |
| 100 | + $defer.flush('myQueue'); |
| 101 | + expect(log).toEqual(['work']); |
| 102 | + }); |
| 103 | + |
| 104 | + |
| 105 | + it('should allow a task to be overriden by another task', function() { |
| 106 | + $defer('myQueue', function() { log.push('work 0') }); |
| 107 | + var id = $defer('myQueue', function() { log.push('work 1') }); |
| 108 | + $defer('myQueue', function() { log.push('work 2') }); |
| 109 | + $defer('myQueue', function() { log.push('work 3') }); |
| 110 | + $defer.cancel(id); |
| 111 | + |
| 112 | + $defer.flush('myQueue'); |
| 113 | + expect(log).toEqual(['work 0', 'work 2', 'work 3']); |
| 114 | + }); |
| 115 | + |
| 116 | + |
| 117 | + it('should ignore attempts to overide flushed tasks', function() { |
| 118 | + var id = $defer('myQueue', function() { log.push('work 0') }); |
| 119 | + $defer.flush('myQueue'); |
| 120 | + |
| 121 | + $defer('myQueue', function() { log.push('work 1') }); |
| 122 | + $defer.cancel(id); |
| 123 | + $defer.flush('myQueue'); |
| 124 | + |
| 125 | + expect(log).toEqual(['work 0', 'work 1']); |
| 126 | + }); |
| 127 | + |
| 128 | + |
| 129 | + it('should generate different ids for tasks', function() { |
| 130 | + var id1 = $defer('myQueue', function() {}); |
| 131 | + var id2 = $defer('myQueue', function() {}); |
| 132 | + var id3 = $defer('myQueue', function() {}); |
| 133 | + expect(id1.id < id2.id < id3.id).toBe(true); |
| 134 | + }); |
75 | 135 | });
|
76 | 136 | });
|
0 commit comments