| ... | @@ -136,6 +136,43 @@ test("useMutate - a failure before the pending delay never leaves isPending stuc | ... | @@ -136,6 +136,43 @@ test("useMutate - a failure before the pending delay never leaves isPending stuc |
| 136 | }); | 136 | }); |
| 137 | }); | 137 | }); |
| 138 | | 138 | |
| | 139 | test("useMutate - swapping the mutation within the pending delay never sets isPending", async () => { |
| | 140 | vi.useFakeTimers({ shouldAdvanceTime: true }); |
| | 141 | const user = userEvent.setup({ delay: null }); |
| | 142 | |
| | 143 | const { client } = createTestMutationClient(); |
| | 144 | const s = new IterableStream<string>(); |
| | 145 | const define = (id: string) => |
| | 146 | client.define({ |
| | 147 | id, |
| | 148 | mutate: async () => { |
| | 149 | return (await s.next()).value; |
| | 150 | }, |
| | 151 | describe: "Test the action", |
| | 152 | describeResult: null, |
| | 153 | optimistic: () => {}, |
| | 154 | }); |
| | 155 | const mutA = define("test-swap-a"); |
| | 156 | const mutB = define("test-swap-b"); |
| | 157 | |
| | 158 | let isPending = false; |
| | 159 | function TestComponent({ mutation }: { mutation: typeof mutA }) { |
| | 160 | const state = useMutate(mutation); |
| | 161 | isPending = state.isPending; |
| | 162 | return ( |
| | 163 | <button data-testid="a" onClick={() => state.run()}> |
| | 164 | button |
| | 165 | </button> |
| | 166 | ); |
| | 167 | } |
| | 168 | |
| | 169 | const view = render(<TestComponent mutation={mutA} />); |
| | 170 | await act(() => user.click(screen.getByTestId("a"))); |
| | 171 | view.rerender(<TestComponent mutation={mutB} />); |
| | 172 | await act(() => vi.advanceTimersByTime(500)); |
| | 173 | assertEquals(isPending, false); |
| | 174 | }); |
| | 175 | |
| 139 | test("useMutate - local error and success handling", async () => { | 176 | test("useMutate - local error and success handling", async () => { |
| 140 | vi.useFakeTimers({ shouldAdvanceTime: true }); | 177 | vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 141 | const user = userEvent.setup({ delay: null }); | 178 | const user = userEvent.setup({ delay: null }); |