Part of the quiet-user-feedback change; docs and specs land in their own repositories.
108 lines
2.3 KiB
TypeScript
108 lines
2.3 KiB
TypeScript
import { act, renderHook } from "@testing-library/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { useInitialAvailability } from "./initial-availability";
|
|
|
|
describe("useInitialAvailability", () => {
|
|
it("keeps loading until content, error, or the initial wait budget", async () => {
|
|
vi.useFakeTimers();
|
|
|
|
try {
|
|
const { rerender, result } = renderHook(
|
|
props => useInitialAvailability(props),
|
|
{
|
|
initialProps: {
|
|
hasContent: false,
|
|
identity: "account-a",
|
|
isError: false,
|
|
isFetching: true,
|
|
timeoutMs: 100,
|
|
},
|
|
},
|
|
);
|
|
|
|
expect(result.current).toBe("loading");
|
|
await act(() => vi.advanceTimersByTimeAsync(99));
|
|
expect(result.current).toBe("loading");
|
|
await act(() => vi.advanceTimersByTimeAsync(1));
|
|
expect(result.current).toBe("unavailable");
|
|
|
|
rerender({
|
|
hasContent: true,
|
|
identity: "account-a",
|
|
isError: false,
|
|
isFetching: false,
|
|
timeoutMs: 100,
|
|
});
|
|
expect(result.current).toBe("ready");
|
|
}
|
|
finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("does not flash loading during retry for the same identity", async () => {
|
|
vi.useFakeTimers();
|
|
|
|
try {
|
|
const { rerender, result } = renderHook(
|
|
props => useInitialAvailability(props),
|
|
{
|
|
initialProps: {
|
|
hasContent: false,
|
|
identity: "account-a",
|
|
isError: true,
|
|
isFetching: false,
|
|
},
|
|
},
|
|
);
|
|
|
|
await act(() => vi.runOnlyPendingTimersAsync());
|
|
expect(result.current).toBe("unavailable");
|
|
rerender({
|
|
hasContent: false,
|
|
identity: "account-a",
|
|
isError: false,
|
|
isFetching: true,
|
|
});
|
|
|
|
expect(result.current).toBe("unavailable");
|
|
}
|
|
finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("resets loading only when identity changes", async () => {
|
|
vi.useFakeTimers();
|
|
|
|
try {
|
|
const { rerender, result } = renderHook(
|
|
props => useInitialAvailability(props),
|
|
{
|
|
initialProps: {
|
|
hasContent: false,
|
|
identity: "account-a",
|
|
isError: true,
|
|
isFetching: false,
|
|
},
|
|
},
|
|
);
|
|
|
|
await act(() => vi.runOnlyPendingTimersAsync());
|
|
expect(result.current).toBe("unavailable");
|
|
rerender({
|
|
hasContent: false,
|
|
identity: "account-b",
|
|
isError: false,
|
|
isFetching: true,
|
|
});
|
|
|
|
expect(result.current).toBe("loading");
|
|
}
|
|
finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
});
|