summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/observable/tentative/observable-inspect.any.js
blob: 8aff741d2670db26a7a355fdd747e70082479a43 (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Because we test that the global error handler is called at various times.
setup({ allow_uncaught_exception: true });

test(() => {
  const results = [];
  let sourceSubscriptionCall = 0;
  const source = new Observable(subscriber => {
    sourceSubscriptionCall++;
    results.push(`source subscribe ${sourceSubscriptionCall}`);
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  let inspectSubscribeCall = 0;
  const result = source.inspect({
    subscribe: () => {
      inspectSubscribeCall++;
      results.push(`inspect() subscribe ${inspectSubscribeCall}`);
    },
    next: (value) => results.push(`inspect() next ${value}`),
    error: (e) => results.push(`inspect() error ${e.message}`),
    complete: () => results.push(`inspect() complete`),
  });

  result.subscribe({
    next: (value) => results.push(`result next ${value}`),
    error: (e) => results.push(`result error ${e.message}`),
    complete: () => results.push(`result complete`),
  });

  result.subscribe({
    next: (value) => results.push(`result next ${value}`),
    error: (e) => results.push(`result error ${e.message}`),
    complete: () => results.push(`result complete`),
  });

  assert_array_equals(results,
    [
      "inspect() subscribe 1",
      "source subscribe 1",
      "inspect() next 1",
      "result next 1",
      "inspect() next 2",
      "result next 2",
      "inspect() next 3",
      "result next 3",
      "inspect() complete",
      "result complete",
      "inspect() subscribe 2",
      "source subscribe 2",
      "inspect() next 1",
      "result next 1",
      "inspect() next 2",
      "result next 2",
      "inspect() next 3",
      "result next 3",
      "inspect() complete",
      "result complete",
    ]);
}, "inspect(): Provides a pre-subscription subscribe callback");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const results = [];

  const result = source.inspect({
    next: value => results.push(value),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  result.subscribe();
  result.subscribe();

  assert_array_equals(results, [1, 2, 3, "complete", 1, 2, 3, "complete"]);
}, "inspect(): Provides a way to tap into the values and completions of the " +
   "source observable using an observer");

test(() => {
  const error = new Error("error from source");
  const source = new Observable(subscriber => subscriber.error(error));

  const results = [];

  const result = source.inspect({
    next: value => results.push(value),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  let errorReported = null;
  self.addEventListener('error', e => errorReported = e.error, {once: true});
  result.subscribe();

  assert_array_equals(results, [error]);
  assert_equals(errorReported, error,
      "errorReported to global matches error from source Observable");
}, "inspect(): Error handler does not stop error from being reported to the " +
   "global, when subscriber does not pass error handler");

test(() => {
  const error = new Error("error from source");
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.error(error);
  });

  const results = [];

  const result = source.inspect({
    next: value => results.push(value),
    error: e => results.push(e),
    complete: () => results.push("complete"),
  });

  const observer = {
    error: e => results.push(e),
  };
  result.subscribe(observer);
  result.subscribe(observer);

  assert_array_equals(results, [1, 2, 3, error, error, 1, 2, 3, error, error]);
}, "inspect(): Provides a way to tap into the values and errors of the " +
   "source observable using an observer. Errors are passed through");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const results = [];

  const result = source.inspect(value => results.push(value));

  result.subscribe();
  result.subscribe();

  assert_array_equals(results, [1, 2, 3, 1, 2, 3]);
}, "inspect(): ObserverCallback passed in");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
  });

  const error = new Error("error from inspect() next handler");
  const result = source.inspect({
    next: (value) => {
      if (value === 2) {
        throw error;
      }
    },
  });

  const results1 = [];
  result.subscribe({
    next: (value) => results1.push(value),
    error: (e) => results1.push(e),
    complete: () => results1.push("complete"),
  });

  const results2 = [];
  result.subscribe({
    next: (value) => results2.push(value),
    error: (e) => results2.push(e),
    complete: () => results2.push("complete"),
  });

  assert_array_equals(results1, [1, error]);
  assert_array_equals(results2, [1, error]);
}, "inspect(): Throwing an error in the observer next handler is caught and " +
   "sent to the error callback of the result observable");

test(() => {
  const sourceError = new Error("error from source");
  const inspectError = new Error("error from inspect() error handler");

  const source = new Observable(subscriber => {
    subscriber.error(sourceError);
  });

  const result = source.inspect({
    error: () => {
      throw inspectError;
    },
  });

  const results = [];
  result.subscribe({
    next: () => results.push("next"),
    error: (e) => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [inspectError]);
}, "inspect(): Throwing an error in the observer error handler in " +
   "inspect() is caught and sent to the error callback of the result " +
   "observable");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  const error = new Error("error from inspect() complete handler");
  const result = source.inspect({
    complete: () => {
      throw error;
    },
  });

  const results = [];
  result.subscribe({
    next: (value) => results.push(value),
    error: (e) => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [1, 2, 3, error]);
}, "inspect(): Throwing an error in the observer complete handler is caught " +
   "and sent to the error callback of the result observable");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
  });

  const error = new Error("error from inspect() next handler");
  const result = source.inspect({
    next: (value) => {
      if (value === 2) {
        throw error;
      }
    },
  });

  const results = [];
  result.subscribe({
    next: (value) => results.push(value),
    error: (e) => results.push(e),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, [1, error]);
}, "inspect(): Throwing an error in the next handler function in do should " +
   "be caught and sent to the error callback of the result observable");

test(() => {
  const source = new Observable(subscriber => {});

  const result = source.inspect({
    subscribe: () => {
      throw new Error("error from do subscribe handler");
    },
  });

  const results = [];
  result.subscribe({
    next: () => results.push("next"),
    error: (e) => results.push(e.message),
    complete: () => results.push("complete"),
  });

  assert_array_equals(results, ["error from do subscribe handler"]);
}, "inspect(): Errors thrown in subscribe() Inspector handler subscribe " +
   "handler are caught and sent to error callback");

test(() => {
  const results = [];
  let sourceTeardownCall = 0;
  const source = new Observable(subscriber => {
    subscriber.addTeardown(() => {
      sourceTeardownCall++;
      results.push(`source teardown ${sourceTeardownCall}`);
    });
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  let doUnsubscribeCall = 0;
  const result = source.inspect({
    abort: (reason) => {
      doUnsubscribeCall++;
      results.push(`inspect() abort ${doUnsubscribeCall} ${reason}`);
    },
    next: (value) => results.push(`inspect() next ${value}`),
    error: (e) => results.push(`inspect() error ${e.message}`),
    complete: () => results.push(`inspect() complete`),
  });

  const controller = new AbortController();
  result.subscribe({
    next: (value) => {
      results.push(`result next ${value}`);
      if (value === 2) {
        controller.abort("abort reason");
      }
    },
    error: (e) => results.push(`result error ${e.message}`),
    complete: () => results.push(`result complete`),
  }, { signal: controller.signal });

  assert_array_equals(results, [
    "inspect() next 1",
    "result next 1",
    "inspect() next 2",
    "result next 2",
    "inspect() abort 1 abort reason",
    "source teardown 1",
  ]);
}, "inspect(): Provides a way to tap into the moment a source observable is unsubscribed from");

test(() => {
  const results = [];
  let sourceTeardownCall = 0;
  const source = new Observable(subscriber => {
    subscriber.addTeardown(() => {
      sourceTeardownCall++;
      results.push(`source teardown ${sourceTeardownCall}`);
    });
    subscriber.next(1);
    subscriber.next(2);
    subscriber.next(3);
    subscriber.complete();
  });

  let inspectUnsubscribeCall = 0;
  const result = source.inspect({
    next: (value) => results.push(`inspect() next ${value}`),
    complete: () => results.push(`inspect() complete`),
    abort: (reason) => {
      inspectUnsubscribeCall++;
      results.push(`inspect() abort ${inspectUnsubscribeCall} ${reason}`);
    },
  });

  result.subscribe({
    next: (value) => results.push(`result next ${value}`),
    complete: () => results.push(`result complete`),
  });

  assert_array_equals(results, [
    "inspect() next 1",
    "result next 1",
    "inspect() next 2",
    "result next 2",
    "inspect() next 3",
    "result next 3",
    "source teardown 1",
    "inspect() complete",
    "result complete",
  ]);
}, "inspect(): Inspector abort() handler is not called if the source " +
   "completes before the result is unsubscribed from");

test(() => {
  const source = new Observable(subscriber => {
    subscriber.next(1);
  });

  const results = [];

  const result = source.inspect({
    abort: () => {
      results.push('abort() handler run');
      throw new Error("error from inspect() subscribe handler");
    },
  });

  const controller = new AbortController();

  self.on('error').take(1).subscribe(e =>
      results.push(e.message + ', from report exception'));

  result.subscribe({
    next: (value) => {
      results.push(value);
      controller.abort();
    },
    // This should not be invoked at all!!
    error: (e) => results.push(e.message + ', from Observer#error()'),
    complete: () => results.push("complete"),
  }, {signal: controller.signal});

  assert_array_equals(results, [1, "abort() handler run",
      "Uncaught Error: error from inspect() subscribe handler, from report " +
      "exception"]);
}, "inspect(): Errors thrown from inspect()'s abort() handler are caught " +
   "and reported to the global, because the subscription is already closed " +
   "by the time the handler runs");