Files
aniplay-api/src/libs/fetchFromMultipleSources.spec.ts
Rushil Perera 23b14536cc refactor!: migrate away from bun
- migrate package management to pnpm
- migrate test suite to vitest
- also remove Anify integration
2025-12-12 19:24:28 -05:00

141 lines
3.8 KiB
TypeScript

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<number>([
() => 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<number>([
() => 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<number>([
() => Promise.reject("error"),
() => Promise.reject(new Error("error")),
]);
expect(result).toBeNull();
});
it("has promises that all throw, contains error", async () => {
const { errorOccurred } = await fetchFromMultipleSources<number>([
() => 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<number>(
[
() => 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<number>(
[
() => 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<number>(
[
() => 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<number>(
[
() => 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);
});
});