feat: add quiet reading with rare confirmations
Part of the quiet-user-feedback change; docs and specs land in their own repositories.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user