|
| 1 | +import { assertExhaustive, tryCatch, promiseWithResolvers } from "../src/utils.js"; |
| 2 | + |
| 3 | +describe("assertExhaustive", () => { |
| 4 | + it("should throw an error when called", () => { |
| 5 | + expect(() => assertExhaustive("unexpected" as never)).toThrow(Error); |
| 6 | + }); |
| 7 | +}); |
| 8 | + |
| 9 | +describe("tryCatch", () => { |
| 10 | + it("should resolve with [null, value] when promise resolves", async () => { |
| 11 | + const promise = Promise.resolve(42); |
| 12 | + const result = await tryCatch(promise); |
| 13 | + expect(result).toEqual([null, 42]); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should resolve with [error, null] when promise rejects", async () => { |
| 17 | + const error = new Error("fail"); |
| 18 | + const promise = Promise.reject(error); |
| 19 | + const result = await tryCatch(promise); |
| 20 | + expect(result[0]).toBe(error); |
| 21 | + expect(result[1]).toBeNull(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should resolve with [error, null] when promise throws non-Error", async () => { |
| 25 | + const promise = Promise.reject("fail"); |
| 26 | + const result = await tryCatch(promise); |
| 27 | + expect(result[0]).toBe("fail"); |
| 28 | + expect(result[1]).toBeNull(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should resolve with [null, undefined] when promise resolves to undefined", async () => { |
| 32 | + const promise = Promise.resolve(undefined); |
| 33 | + const result = await tryCatch(promise); |
| 34 | + expect(result).toEqual([null, undefined]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should resolve with [null, value] when promise is already resolved", async () => { |
| 38 | + const resolved = Promise.resolve("done"); |
| 39 | + const result = await tryCatch(resolved); |
| 40 | + expect(result).toEqual([null, "done"]); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should resolve with [null, undefined] when promise is undefined", async () => { |
| 44 | + const result = await tryCatch(undefined); |
| 45 | + expect(result).toEqual([null, undefined]); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("promiseWithResolvers", () => { |
| 50 | + it("should return a deferred promise with resolve and reject", async () => { |
| 51 | + const deferred = promiseWithResolvers<number>(); |
| 52 | + expect(typeof deferred.promise.then).toBe("function"); |
| 53 | + expect(typeof deferred.resolve).toBe("function"); |
| 54 | + expect(typeof deferred.reject).toBe("function"); |
| 55 | + let resolved = false; |
| 56 | + deferred.promise.then((value: number) => { |
| 57 | + expect(value).toBe(123); |
| 58 | + resolved = true; |
| 59 | + }); |
| 60 | + deferred.resolve(123); |
| 61 | + await deferred.promise; |
| 62 | + expect(resolved).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should reject the promise when reject is called", async () => { |
| 66 | + const deferred = promiseWithResolvers<string>(); |
| 67 | + const error = new Error("fail"); |
| 68 | + let caught: Error | null = null; |
| 69 | + deferred.promise.catch((e: Error) => { |
| 70 | + caught = e; |
| 71 | + }); |
| 72 | + deferred.reject(error); |
| 73 | + await expect(deferred.promise).rejects.toBe(error); |
| 74 | + expect(caught).toBe(error); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should allow resolving with undefined", async () => { |
| 78 | + const deferred = promiseWithResolvers<void>(); |
| 79 | + let resolved = false; |
| 80 | + deferred.promise.then((value: void) => { |
| 81 | + expect(value).toBeUndefined(); |
| 82 | + resolved = true; |
| 83 | + }); |
| 84 | + deferred.resolve(); |
| 85 | + await deferred.promise; |
| 86 | + expect(resolved).toBe(true); |
| 87 | + }); |
| 88 | +}); |
0 commit comments