-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
220 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/lib/queries/client/mutations/mutation/mutation.options.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" | ||
import { type QueryClient } from "../../createClient" | ||
import { createQueryClient, waitForTimeout } from "../../../../../tests/utils" | ||
import { MutationObserver } from "../observers/MutationObserver" | ||
import { waitFor } from "@testing-library/react" | ||
import { noop } from "rxjs" | ||
import { MutationCache } from "../cache/MutationCache" | ||
|
||
describe("mutations", () => { | ||
let queryClient: QueryClient | ||
|
||
beforeEach(() => { | ||
queryClient = createQueryClient() | ||
queryClient.mount() | ||
}) | ||
|
||
afterEach(() => { | ||
queryClient.clear() | ||
}) | ||
|
||
test("mutation onError callbacks should see updated options", async () => { | ||
const onError = vi.fn() | ||
|
||
const mutation = new MutationObserver(queryClient, { | ||
mutationFn: async () => { | ||
await waitForTimeout(10) | ||
|
||
throw new Error("error") | ||
}, | ||
onError: () => { | ||
onError(1) | ||
} | ||
}) | ||
|
||
void mutation.mutate().catch(noop) | ||
|
||
mutation.setOptions({ | ||
onError: () => { | ||
onError(2) | ||
} | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(onError).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
expect(onError).toHaveBeenCalledWith(2) | ||
}) | ||
|
||
test("mutation onSettled callbacks should see updated options", async () => { | ||
const onSettled = vi.fn() | ||
|
||
const mutation = new MutationObserver(queryClient, { | ||
mutationFn: async () => { | ||
return "update" | ||
}, | ||
onSettled: () => { | ||
onSettled(1) | ||
} | ||
}) | ||
|
||
void mutation.mutate() | ||
|
||
mutation.setOptions({ | ||
onSettled: () => { | ||
onSettled(2) | ||
} | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(onSettled).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
expect(onSettled).toHaveBeenCalledWith(2) | ||
}) | ||
|
||
test("mutation onMutate callbacks should see updated options", async () => { | ||
const onMutate = vi.fn() | ||
|
||
const queryClient2 = createQueryClient({ | ||
mutationCache: new MutationCache({ | ||
onMutate: async () => { | ||
await waitForTimeout(10) | ||
|
||
return "" | ||
} | ||
}) | ||
}) | ||
|
||
const mutation = new MutationObserver(queryClient2, { | ||
mutationFn: async () => { | ||
return "update" | ||
}, | ||
onMutate: () => { | ||
onMutate(1) | ||
} | ||
}) | ||
|
||
void mutation.mutate() | ||
|
||
mutation.setOptions({ | ||
onMutate: () => { | ||
onMutate(2) | ||
} | ||
}) | ||
|
||
await waitFor(() => { | ||
expect(onMutate).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
expect(onMutate).toHaveBeenCalledWith(2) | ||
}) | ||
}) |
Oops, something went wrong.