UNPKG

@mittwald/react-use-promise

Version:

Simple and declarative use of Promises in your React components. Observe their state and refresh them in various advanced ways.

47 lines (46 loc) 1.56 kB
import { vitest, beforeEach, expect, test, afterEach } from "vitest"; import { sleep } from "../lib/testing.js"; import { getAsyncResource } from "./getAsyncResource.js"; import { asyncResourceStore } from "./store.js"; const sleepTime = 2000; beforeEach(() => { vitest.useFakeTimers(); asyncResourceStore.clear(); }); afterEach(() => { vitest.runOnlyPendingTimers(); vitest.useRealTimers(); }); const loader = vitest.fn(async (value) => { await sleep(sleepTime); return value; }); const load = async (resource) => { const loadingPromise = resource.load(); vitest.advanceTimersToNextTimer(); await loadingPromise; }; test("Expect loader is not called when parameters is null", async () => { const resource = getAsyncResource(loader, null); await load(resource); expect(loader).not.toHaveBeenCalled(); }); test("Expect value is undefined when parameters is null", async () => { const resource = getAsyncResource(loader, null); await load(resource); const value = resource.value.value.isSet ? resource.value.value.value : undefined; expect(resource.value.value.isSet).toBe(true); expect(value).toBe(undefined); }); test("Expect meta is set", async () => { const resource = getAsyncResource(loader, ["test"]); expect(resource.meta).toBeDefined(); }); test("Expect tags are set in meta", async () => { const resource = getAsyncResource(loader, ["test"], { tags: ["tag1", "tag2"], }); expect(resource.meta.tags?.tags).toEqual(["tag1", "tag2"]); });