-
Notifications
You must be signed in to change notification settings - Fork 1
/
pledge_spec_ch2.js
150 lines (119 loc) · 4.71 KB
/
pledge_spec_ch2.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
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
describe('Chapter 2: Fulfillment Callback Attachment',function(){});
/*======================================================
.d8888b.
d88P Y88b
888
.d88P
.od888P"
d88P"
888"
888888888
Chapter 2: Attaching and Calling Promise Event Handlers
--------------------------------------------------------
We are beginning to see how a deferral can manipulate a
promise. But what does a promise actually do? How can one be
used? By completing this chapter, you will learn the
fundamentals of how promises act on eventual information.
========================================================*/
describe("A promise's .then method", function(){
var deferral, promise;
beforeEach(function(){
deferral = defer();
promise = deferral.$promise;
});
function s1 (data) { /* use data */ }
function e1 (reason) { /* handle reason */ }
function s2 (d) { /* use d */ }
function e2 (r) { /* handle r */ }
it('adds groups of handlers (callback functions) to the promise', function(){
promise.then( s1, e1 );
expect( promise.handlerGroups[0].successCb ).toBe( s1 );
expect( promise.handlerGroups[0].errorCb ).toBe( e1 );
});
it('can be called multiple times to add more handlers', function(){
promise.then( s1, e1 );
expect( promise.handlerGroups[0].successCb ).toBe( s1 );
expect( promise.handlerGroups[0].errorCb ).toBe( e1 );
promise.then( s2, e2 );
expect( promise.handlerGroups[1].successCb ).toBe( s2 );
expect( promise.handlerGroups[1].errorCb ).toBe( e2 );
});
it('attaches a falsy value in place of non-function success or error callbacks', function(){
promise.then( 'a string', {} );
expect( promise.handlerGroups[0].successCb ).toBeFalsy();
expect( promise.handlerGroups[0].errorCb ).toBeFalsy();
});
});
// Getting to the main functionality
describe('A promise', function(){
var numDeferral, promiseForNum, foo;
var setFoo10 = jasmine.createSpy().and.callFake(function () { foo = 10; });
var addToFoo = jasmine.createSpy().and.callFake(function (num) { foo += num; });
beforeEach(function(){
numDeferral = defer();
promiseForNum = numDeferral.$promise;
foo = 0;
setFoo10.calls.reset();
addToFoo.calls.reset();
});
describe('that is not yet resolved', function(){
it('does not call any success handlers yet', function(){
promiseForNum.then( setFoo10 );
expect( setFoo10 ).not.toHaveBeenCalled();
});
});
describe('that is already resolved', function(){
beforeEach(function(){
numDeferral.resolve( 25 );
});
// Recommended: add a .callHandlers method to your promise prototype.
it('calls a success handler added by .then', function(){
promiseForNum.then( setFoo10 );
expect( setFoo10 ).toHaveBeenCalled();
});
it("calls a success handler by passing in the promise's value", function(){
promiseForNum.then( addToFoo );
// console.log(promiseForNum);
expect( addToFoo ).toHaveBeenCalledWith( 25 );
});
it('calls each success handler once per attachment', function(){
promiseForNum.then( setFoo10 );
//console.log('starting tricky spec');
promiseForNum.then( addToFoo );
promiseForNum.then( addToFoo );
expect( setFoo10.calls.count() ).toBe( 1 );
expect( addToFoo.calls.count() ).toBe( 2 );
expect( addToFoo ).toHaveBeenCalledWith( 25 );
});
it('calls each success handler when added', function(){
promiseForNum.then( setFoo10 );
expect( foo ).toBe( 10 );
promiseForNum.then( addToFoo );
expect( foo ).toBe( 35 );
});
});
// So we can run callbacks if we already have the value.
// But what if events occur in opposite order?
describe('that already has a success handler', function(){
it('calls that handler when resolved', function(){
promiseForNum.then( setFoo10 );
numDeferral.resolve();
expect( setFoo10 ).toHaveBeenCalled();
});
it('calls all its success handlers in order one time when resolved', function(){
promiseForNum.then( setFoo10 );
promiseForNum.then( addToFoo );
numDeferral.resolve( 25 );
//console.log(numDeferral);
expect( foo ).toBe( 35 );
});
});
});
/*
We've just made something nifty. A promise's .then can
attach behavior both before & after the promise is actually
resolved, and we know that the actions will run when they can.
The .then method can also be called multiple times, so you can
attach callbacks to run in different parts of your code, and
they will all run once the promise is resolved.
*/