forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config-spec.ts
249 lines (208 loc) · 7.08 KB
/
config-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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/** @prettier */
import { config } from '../src/internal/config';
import { expect } from 'chai';
import { Observable } from 'rxjs';
import { timeoutProvider } from 'rxjs/internal/scheduler/timeoutProvider';
describe('config', () => {
it('should have a Promise property that defaults to nothing', () => {
expect(config).to.have.property('Promise');
expect(config.Promise).to.be.undefined;
});
describe('onUnhandledError', () => {
afterEach(() => {
config.onUnhandledError = null;
});
it('should default to null', () => {
expect(config.onUnhandledError).to.be.null;
});
it('should call asynchronously if an error is emitted and not handled by the consumer observer', (done) => {
let called = false;
const results: any[] = [];
config.onUnhandledError = (err) => {
called = true;
expect(err).to.equal('bad');
done();
};
const source = new Observable<number>((subscriber) => {
subscriber.next(1);
subscriber.error('bad');
});
source.subscribe({
next: (value) => results.push(value),
});
expect(called).to.be.false;
expect(results).to.deep.equal([1]);
});
it('should call asynchronously if an error is emitted and not handled by the consumer next callback', (done) => {
let called = false;
const results: any[] = [];
config.onUnhandledError = (err) => {
called = true;
expect(err).to.equal('bad');
done();
};
const source = new Observable<number>((subscriber) => {
subscriber.next(1);
subscriber.error('bad');
});
source.subscribe((value) => results.push(value));
expect(called).to.be.false;
expect(results).to.deep.equal([1]);
});
it('should call asynchronously if an error is emitted and not handled by the consumer in the empty case', (done) => {
let called = false;
config.onUnhandledError = (err) => {
called = true;
expect(err).to.equal('bad');
done();
};
const source = new Observable((subscriber) => {
subscriber.error('bad');
});
source.subscribe();
expect(called).to.be.false;
});
/**
* This test is added so people know this behavior is _intentional_. It's part of the contract of observables
* and, while I'm not sure I like it, it might start surfacing untold numbers of errors, and break
* node applications if we suddenly changed this to start throwing errors on other jobs for instances
* where users accidentally called `subscriber.error` twice. Likewise, would we report an error
* for two calls of `complete`? This is really something a build-time tool like a linter should
* capture. Not a run time error reporting event.
*/
it('should not be called if two errors are sent to the subscriber', (done) => {
let called = false;
config.onUnhandledError = () => {
called = true;
};
const source = new Observable((subscriber) => {
subscriber.error('handled');
subscriber.error('swallowed');
});
let syncSentError: any;
source.subscribe({
error: (err) => {
syncSentError = err;
},
});
expect(syncSentError).to.equal('handled');
// When called, onUnhandledError is called on a timeout, so delay the
// the assertion of the expectation until after the point at which
// onUnhandledError would have been called.
timeoutProvider.setTimeout(() => {
expect(called).to.be.false;
done();
});
});
});
describe('onStoppedNotification', () => {
afterEach(() => {
config.onStoppedNotification = null;
});
it('should default to null', () => {
expect(config.onStoppedNotification).to.be.null;
});
it('should be called asynchronously if a subscription setup errors after the subscription is closed by an error', (done) => {
let called = false;
config.onStoppedNotification = (notification) => {
called = true;
expect(notification.kind).to.equal('E');
expect(notification).to.have.property('error', 'bad');
done();
};
const source = new Observable((subscriber) => {
subscriber.error('handled');
throw 'bad';
});
let syncSentError: any;
source.subscribe({
error: (err) => {
syncSentError = err;
},
});
expect(syncSentError).to.equal('handled');
expect(called).to.be.false;
});
it('should be called asynchronously if a subscription setup errors after the subscription is closed by a completion', (done) => {
let called = false;
let completed = false;
config.onStoppedNotification = (notification) => {
called = true;
expect(notification.kind).to.equal('E');
expect(notification).to.have.property('error', 'bad');
done();
};
const source = new Observable((subscriber) => {
subscriber.complete();
throw 'bad';
});
source.subscribe({
error: () => {
throw 'should not be called';
},
complete: () => {
completed = true;
},
});
expect(completed).to.be.true;
expect(called).to.be.false;
});
it('should be called if a next is sent to the stopped subscriber', (done) => {
let called = false;
config.onStoppedNotification = (notification) => {
called = true;
expect(notification.kind).to.equal('N');
expect(notification).to.have.property('value', 2);
done();
};
const source = new Observable((subscriber) => {
subscriber.next(1);
subscriber.complete();
subscriber.next(2);
});
let syncSentValue: any;
source.subscribe({
next: (value) => {
syncSentValue = value;
},
});
expect(syncSentValue).to.equal(1);
expect(called).to.be.false;
});
it('should be called if two errors are sent to the subscriber', (done) => {
let called = false;
config.onStoppedNotification = (notification) => {
called = true;
expect(notification.kind).to.equal('E');
expect(notification).to.have.property('error', 'swallowed');
done();
};
const source = new Observable((subscriber) => {
subscriber.error('handled');
subscriber.error('swallowed');
});
let syncSentError: any;
source.subscribe({
error: (err) => {
syncSentError = err;
},
});
expect(syncSentError).to.equal('handled');
expect(called).to.be.false;
});
it('should be called if two completes are sent to the subscriber', (done) => {
let called = false;
config.onStoppedNotification = (notification) => {
called = true;
expect(notification.kind).to.equal('C');
done();
};
const source = new Observable((subscriber) => {
subscriber.complete();
subscriber.complete();
});
source.subscribe();
expect(called).to.be.false;
});
});
});