import { describe, beforeEach, expect, it, vi } from "vitest"; import { createRoot } from "react-dom/client"; import { act } from "react-dom/test-utils"; import { createStreamingCache } from "../cache/createStreamingCache"; import { STATUS_ABORTED, STATUS_PENDING, STATUS_REJECTED, STATUS_RESOLVED, } from "../constants"; import { StreamingCache, StreamingCacheLoadOptions, StreamingValue, } from "../types"; import { StreamingValuePartial, useStreamingValue } from "./useStreamingValue"; describe("useStreamingValue", () => { let lastRendered: StreamingValuePartial | undefined = undefined; function Component({ streaming, throttleUpdatesBy, }: { streaming: StreamingValue; throttleUpdatesBy?: number; }): any { lastRendered = useStreamingValue(streaming, { throttleUpdatesBy, }); return null; } beforeEach(() => { // @ts-ignore global.IS_REACT_ACT_ENVIRONMENT = true; vi.useFakeTimers(); }); describe("single value (string)", () => { type Metadata = { length: number }; type Value = string; let cache: StreamingCache<[string], Value, Metadata>; let optionsMap: Map>; beforeEach(() => { optionsMap = new Map(); cache = createStreamingCache({ load: (options, key) => { optionsMap.set(key, options); }, }); }); it("should re-render as data streams in", () => { const streaming = cache.stream("test"); const container = document.createElement("div"); const root = createRoot(container); act(() => { root.render(); }); expect(lastRendered?.value).toEqual(undefined); const options = optionsMap.get("test")!; act(() => { options.update("ab", 0.5, { length: 4 }); }); expect(lastRendered).toEqual({ complete: false, data: { length: 4 }, progress: 0.5, status: STATUS_PENDING, value: "ab", }); act(() => { options.update("abcd", 1); options.resolve(); vi.advanceTimersByTime(10); }); expect(lastRendered).toEqual({ complete: true, data: { length: 4 }, progress: 1, status: STATUS_RESOLVED, value: "abcd", }); }); }); describe("array of values (number)", () => { let cache: StreamingCache<[string], number[]>; let optionsMap: Map>; beforeEach(() => { optionsMap = new Map(); cache = createStreamingCache({ load: (options, key) => { optionsMap.set(key, options); }, }); }); it("should re-render as values stream in", () => { const streaming = cache.stream("test"); const container = document.createElement("div"); const root = createRoot(container); act(() => { root.render(); }); expect(lastRendered?.value).toEqual(undefined); const options = optionsMap.get("test")!; act(() => { options.update([1, 2], 0.5); }); expect(lastRendered).toEqual({ complete: false, data: undefined, progress: 0.5, status: STATUS_PENDING, value: [1, 2], }); act(() => { options.update([1, 2, 3, 4], 1); options.resolve(); vi.advanceTimersByTime(10); }); expect(lastRendered).toEqual({ complete: true, data: undefined, progress: 1, status: STATUS_RESOLVED, value: [1, 2, 3, 4], }); }); it("should throttle updates to avoid overwhelming React's scheduler", () => { const streaming = cache.stream("test"); const container = document.createElement("div"); const root = createRoot(container); act(() => { root.render( ); }); expect(lastRendered?.value).toEqual(undefined); const options = optionsMap.get("test")!; act(() => { options.update([1], 0.25); }); expect(lastRendered?.value).toEqual([1]); act(() => { options.update([1, 2], 0.5); }); expect(lastRendered?.value).toEqual([1]); act(() => { vi.advanceTimersByTime(50); }); act(() => { options.update([1, 2, 3], 0.75); }); expect(lastRendered?.value).toEqual([1]); act(() => { vi.advanceTimersByTime(50); }); expect(lastRendered?.value).toEqual([1, 2, 3]); }); it("should update in response to an aborted request", async () => { const streaming = cache.stream("test"); const container = document.createElement("div"); const root = createRoot(container); act(() => { root.render(); }); expect(lastRendered?.status).toEqual(STATUS_PENDING); act(() => { cache.abort("test"); }); vi.runAllTimers(); expect(lastRendered?.status).toEqual(STATUS_ABORTED); }); }); it("should return an error if the underlying cache is rejected", async () => { let options: StreamingCacheLoadOptions | null = null; const cache = createStreamingCache<[string], string>({ load: (...args) => { options = args[0]; }, }); const streaming = cache.stream("test"); const container = document.createElement("div"); const root = createRoot(container); act(() => { root.render(); }); expect(lastRendered?.status).toEqual(STATUS_PENDING); act(() => options?.reject("failure")); expect(lastRendered?.error).toEqual("failure"); expect(lastRendered?.status).toEqual(STATUS_REJECTED); }); });