diff --git a/readme.changes.md b/readme.changes.md index adc3c5f7e7ee573dc805cca94f31259129615368..54038395de1ce49134428a33e7710f9c8b0c325e 100644 --- a/readme.changes.md +++ b/readme.changes.md @@ -7,6 +7,8 @@ - resolve calls made while a failed mutation refetches never running - resolve a synchronous throw from `mutate` escaping to the caller and leaving the mutation stuck in its mutating state +- resolve `useMutate` setting `isPending` after its mutation is swapped within + the 200ms pending delay ## v3.0.2 diff --git a/src/react.ts b/src/react.ts index 156ccc5c7f873024525bfbccc02404183e9f11a8..c54193dd8ddf187e1182174151438628a25c2f54 100644 --- a/src/react.ts +++ b/src/react.ts @@ -241,6 +241,7 @@ class Observer { this.unsubscribe = null; this.currentKey = null; this.state = initialState(); + this.resetPending(); this.debounced = false; } diff --git a/test/useMutate.test.tsx b/test/useMutate.test.tsx index bfa833b2bc7399ea70e2ae20a1ba443ba22ade52..f4b6f8bdcdbcf52aeeb62867c37128a585a9d6e4 100644 --- a/test/useMutate.test.tsx +++ b/test/useMutate.test.tsx @@ -136,6 +136,43 @@ test("useMutate - a failure before the pending delay never leaves isPending stuc }); }); +test("useMutate - swapping the mutation within the pending delay never sets isPending", async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }); + const user = userEvent.setup({ delay: null }); + + const { client } = createTestMutationClient(); + const s = new IterableStream(); + const define = (id: string) => + client.define({ + id, + mutate: async () => { + return (await s.next()).value; + }, + describe: "Test the action", + describeResult: null, + optimistic: () => {}, + }); + const mutA = define("test-swap-a"); + const mutB = define("test-swap-b"); + + let isPending = false; + function TestComponent({ mutation }: { mutation: typeof mutA }) { + const state = useMutate(mutation); + isPending = state.isPending; + return ( + + ); + } + + const view = render(); + await act(() => user.click(screen.getByTestId("a"))); + view.rerender(); + await act(() => vi.advanceTimersByTime(500)); + assertEquals(isPending, false); +}); + test("useMutate - local error and success handling", async () => { vi.useFakeTimers({ shouldAdvanceTime: true }); const user = userEvent.setup({ delay: null });