import { describe, expect, it } from "vitest"; import { fetchFromMultipleSources } from "./fetchFromMultipleSources"; describe("fetchFromMultipleSources", () => { it("no promises, throws exception", async () => { await expect(fetchFromMultipleSources([])).rejects.toThrow( "fetchPromises cannot be empty", ); }); it("has promises with valid responses, returns first one with value", async () => { const { result } = await fetchFromMultipleSources([ () => Promise.resolve(undefined), () => Promise.resolve(null), () => Promise.reject(), () => Promise.resolve(2), () => Promise.resolve(3), ]); expect(result).toEqual(2); }); it("has promises with valid responses, no error occurred", async () => { const { errorOccurred } = await fetchFromMultipleSources([ () => Promise.resolve(undefined), () => Promise.resolve(null), () => Promise.reject(), () => Promise.resolve(2), () => Promise.resolve(3), ]); expect(errorOccurred).toBe(false); }); it("has promises that all throw, returns null", async () => { const { result } = await fetchFromMultipleSources([ () => Promise.reject("error"), () => Promise.reject(new Error("error")), ]); expect(result).toBeNull(); }); it("has promises that all throw, contains error", async () => { const { errorOccurred } = await fetchFromMultipleSources([ () => Promise.reject("error"), () => Promise.reject(new Error("error")), ]); expect(errorOccurred).toBe(true); }); it("has promises but cache has value, returns cached value", async () => { const { result } = await fetchFromMultipleSources( [ () => Promise.resolve(null), () => Promise.reject("error"), () => Promise.resolve(undefined), ], { fetchFromCache: () => Promise.resolve(-1), saveInCache: async () => {}, }, ); expect(result).toEqual(-1); }); it("has promises but cache has value, contains no errors", async () => { const { errorOccurred } = await fetchFromMultipleSources( [ () => Promise.resolve(null), () => Promise.reject("error"), () => Promise.resolve(undefined), ], { fetchFromCache: () => Promise.resolve(-1), saveInCache: async () => {}, }, ); expect(errorOccurred).toBe(false); }); it("has promises, no cached value, no valid response, should not save in cache", async () => { let actual; await fetchFromMultipleSources( [ () => Promise.resolve(null), () => Promise.reject(), () => Promise.resolve(undefined), ], { fetchFromCache: () => Promise.resolve(null), saveInCache: async (value) => { actual = value; }, }, ); expect(actual).toBeUndefined(); }); it("has promises, no cached value, has valid response, should save in cache", async () => { let actual: number | undefined; await fetchFromMultipleSources( [ () => Promise.resolve(null), () => Promise.reject(), () => Promise.resolve(3), ], { fetchFromCache: () => Promise.resolve(null), saveInCache: async (value) => { actual = value; }, }, ); const expected = 3; expect(actual).toBe(expected); }); it("has promises, no cached value, has valid response, value that was cached is returned", async () => { const { result } = await fetchFromMultipleSources( [ () => Promise.resolve(null), () => Promise.reject(), () => Promise.resolve(3), ], { fetchFromCache: () => Promise.resolve(null), saveInCache: async (value) => {}, }, ); expect(result).toBe(3); }); });