fix(frontend): type pushState mocks with real History.pushState signature
- npm run typecheck failed on the 0-arg vi.fn<() => void>() because mock.calls[0] became an empty tuple, so calls[0][2] (the URL arg) was typed as undefined. - Constrain the mock to the real (state, unused, url?) signature so mock.calls[0] is a 3-tuple and [2] is string-or-URL-or-null. Drops the as-string cast at every callsite.
This commit is contained in:
@@ -58,11 +58,13 @@ function setLocation(url: string): void {
|
|||||||
describe("useDrawerUrlState", () => {
|
describe("useDrawerUrlState", () => {
|
||||||
// `vi.fn()` with no args in vitest 4 is typed `Mock<Procedure |
|
// `vi.fn()` with no args in vitest 4 is typed `Mock<Procedure |
|
||||||
// Constructable>`, which carries a constructor signature and won't
|
// Constructable>`, which carries a constructor signature and won't
|
||||||
// assign to `() => void`. Constraining the generic to `vi.fn<() => void>()`
|
// assign to a real `pushState` slot. Constrain the generic to the
|
||||||
// gives a plain function-typed mock that flows directly into `pushState`
|
// `History.pushState` signature so the mock flows into `vi.stubGlobal`
|
||||||
// / `replaceState` slots.
|
// without complaint AND `mock.calls[0]` is typed as a 3-tuple, letting
|
||||||
let pushStateMock: ReturnType<typeof vi.fn<() => void>>;
|
// us read `calls[0][2]` (the URL) without an `as unknown` cast.
|
||||||
let replaceStateMock: ReturnType<typeof vi.fn<() => void>>;
|
type PushState = (state: unknown, unused: string, url?: string | URL | null) => void;
|
||||||
|
let pushStateMock: ReturnType<typeof vi.fn<PushState>>;
|
||||||
|
let replaceStateMock: ReturnType<typeof vi.fn<PushState>>;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
pushStateMock = vi.fn();
|
pushStateMock = vi.fn();
|
||||||
|
|||||||
Reference in New Issue
Block a user