From d2187647d7a25eb768f15b34db1b45ad80038e7e Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Wed, 10 Mar 2021 16:31:41 -0500 Subject: [PATCH] Add more tests of asyncMap error behavior. --- .../observables/__tests__/asyncMap.ts | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/utilities/observables/__tests__/asyncMap.ts b/src/utilities/observables/__tests__/asyncMap.ts index 854a2e8a43c..f571c60c711 100644 --- a/src/utilities/observables/__tests__/asyncMap.ts +++ b/src/utilities/observables/__tests__/asyncMap.ts @@ -54,4 +54,83 @@ describe("asyncMap", () => { }), }); }); + + itAsync("handles exceptions from mapping functions", (resolve, reject) => { + const triples: number[] = []; + asyncMap(make1234Observable(), num => { + if (num === 3) throw new Error("expected"); + return num * 3; + }).subscribe({ + next: rejectExceptions(reject, triple => { + expect(triple).toBeLessThan(9); + triples.push(triple); + }), + error: rejectExceptions(reject, error => { + expect(error.message).toBe("expected"); + expect(triples).toEqual([3, 6]); + resolve(); + }), + }); + }); + + itAsync("handles rejected promises from mapping functions", (resolve, reject) => { + const triples: number[] = []; + asyncMap(make1234Observable(), num => { + if (num === 3) return Promise.reject(new Error("expected")); + return num * 3; + }).subscribe({ + next: rejectExceptions(reject, triple => { + expect(triple).toBeLessThan(9); + triples.push(triple); + }), + error: rejectExceptions(reject, error => { + expect(error.message).toBe("expected"); + expect(triples).toEqual([3, 6]); + resolve(); + }), + }); + }); + + itAsync("handles async exceptions from mapping functions", (resolve, reject) => { + const triples: number[] = []; + asyncMap(make1234Observable(), num => wait(10).then(() => { + if (num === 3) throw new Error("expected"); + return num * 3; + })).subscribe({ + next: rejectExceptions(reject, triple => { + expect(triple).toBeLessThan(9); + triples.push(triple); + }), + error: rejectExceptions(reject, error => { + expect(error.message).toBe("expected"); + expect(triples).toEqual([3, 6]); + resolve(); + }), + }); + }); + + itAsync("handles exceptions from next functions", (resolve, reject) => { + const triples: number[] = []; + asyncMap(make1234Observable(), num => { + return num * 3; + }).subscribe({ + next(triple) { + triples.push(triple); + // Unfortunately this exception won't be caught by asyncMap, because + // the Observable implementation wraps this next function with its own + // try-catch. Uncomment the remaining lines to make this test more + // meaningful, in the event that this behavior ever changes. + // if (triple === 9) throw new Error("expected"); + }, + // error: rejectExceptions(reject, error => { + // expect(error.message).toBe("expected"); + // expect(triples).toEqual([3, 6, 9]); + // resolve(); + // }), + complete: rejectExceptions(reject, () => { + expect(triples).toEqual([3, 6, 9, 12]); + resolve(); + }), + }); + }); });