forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
from-promise-spec.ts
210 lines (192 loc) · 6.59 KB
/
from-promise-spec.ts
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
import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../dist/cjs/Rx';
declare const process: any;
const Observable = Rx.Observable;
/** @test {fromPromise} */
describe('Observable.fromPromise', () => {
it('should emit one value from a resolved promise', (done: MochaDone) => {
const promise = Promise.resolve(42);
Observable.fromPromise(promise)
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should raise error from a rejected promise', (done: MochaDone) => {
const promise = Promise.reject('bad');
Observable.fromPromise(promise)
.subscribe((x: any) => {
done(new Error('should not be called'));
},
(e: any) => {
expect(e).to.equal('bad');
done();
}, () => {
done(new Error('should not be called'));
});
});
it('should share the underlying promise with multiple subscribers', (done: MochaDone) => {
const promise = Promise.resolve(42);
const observable = Observable.fromPromise(promise);
observable
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, null);
setTimeout(() => {
observable
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
});
it('should accept already-resolved Promise', (done: MochaDone) => {
const promise = Promise.resolve(42);
promise.then((x: number) => {
expect(x).to.equal(42);
Observable.fromPromise(promise)
.subscribe(
(y: number) => { expect(y).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, () => {
done();
});
}, () => {
done(new Error('should not be called'));
});
});
it('should accept PromiseLike object for interoperability', (done: MochaDone) => {
class CustomPromise<T> implements PromiseLike<T> {
constructor(private promise: PromiseLike<T>) {
}
then(onFulfilled?, onRejected?): PromiseLike<T> {
return new CustomPromise(this.promise.then(onFulfilled, onRejected));
};
}
const promise = new CustomPromise(Promise.resolve(42));
Observable.fromPromise(promise)
.subscribe(
(x: number) => { expect(x).to.equal(42); },
() => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should emit a value from a resolved promise on a separate scheduler', (done: MochaDone) => {
const promise = Promise.resolve(42);
Observable.fromPromise(promise, Rx.Scheduler.asap)
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
it('should raise error from a rejected promise on a separate scheduler', (done: MochaDone) => {
const promise = Promise.reject('bad');
Observable.fromPromise(promise, Rx.Scheduler.asap)
.subscribe(
(x: any) => { done(new Error('should not be called')); },
(e: any) => {
expect(e).to.equal('bad');
done();
}, () => {
done(new Error('should not be called'));
});
});
it('should share the underlying promise with multiple subscribers on a separate scheduler', (done: MochaDone) => {
const promise = Promise.resolve(42);
const observable = Observable.fromPromise(promise, Rx.Scheduler.asap);
observable
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
},
null);
setTimeout(() => {
observable
.subscribe(
(x: number) => { expect(x).to.equal(42); },
(x) => {
done(new Error('should not be called'));
}, () => {
done();
});
});
});
it('should not emit, throw or complete if immediately unsubscribed', (done: MochaDone) => {
const nextSpy = sinon.spy();
const throwSpy = sinon.spy();
const completeSpy = sinon.spy();
const promise = Promise.resolve(42);
const subscription = Observable.fromPromise(promise)
.subscribe(nextSpy, throwSpy, completeSpy);
subscription.unsubscribe();
setTimeout(() => {
expect(nextSpy).not.have.been.called;
expect(throwSpy).not.have.been.called;
expect(completeSpy).not.have.been.called;
done();
});
});
if (typeof process === 'object' && Object.prototype.toString.call(process) === '[object process]') {
it('should globally throw unhandled errors on process', (done: MochaDone) => {
const originalException = process.listeners('uncaughtException');
process.removeAllListeners('uncaughtException');
process.once('uncaughtException', function (error) {
expect(error).to.be.an('error', 'fail');
originalException.forEach(l => process.addListener('uncaughtException', l));
done();
});
Observable.fromPromise(Promise.reject('bad'))
.subscribe(
(x: any) => { done(new Error('should not be called')); },
(e: any) => {
expect(e).to.equal('bad');
throw new Error('fail');
}, () => {
done(new Error('should not be called'));
});
});
} else if (typeof window === 'object' &&
(Object.prototype.toString.call(window) === '[object global]' || Object.prototype.toString.call(window) === '[object Window]')) {
it('should globally throw unhandled errors on window', (done: MochaDone) => {
const expected = ['Uncaught fail', 'fail', 'Script error.', 'uncaught exception: fail'];
const current = window.onerror;
window.onerror = null;
let invoked = false;
function onException(e) {
if (invoked) {
return;
}
invoked = true;
expect(expected).to.contain(e);
window.onerror = current;
done();
}
window.onerror = onException;
Observable.fromPromise(Promise.reject('bad'))
.subscribe(
(x: any) => { done(new Error('should not be called')); },
(e: any) => {
expect(e).to.equal('bad');
throw 'fail';
}, () => {
done(new Error('should not be called'));
});
});
}
});