| ... | ... | @@ -1,10 +1,24 @@ |
| 1 | 1 | import { QueryClient } from "@tanstack/react-query"; |
| 2 | | import { describe, expect, it } from "vitest"; |
| 2 | import { describe, expect, it, vi } from "vitest"; |
| 3 | |
| 4 | vi.mock("../nativeApi", () => ({ |
| 5 | ensureNativeApi: vi.fn(), |
| 6 | })); |
| 7 | |
| 8 | vi.mock("../wsRpcClient", () => ({ |
| 9 | getWsRpcClient: vi.fn(), |
| 10 | })); |
| 11 | |
| 3 | 12 | import { |
| 13 | gitBranchesQueryOptions, |
| 4 | 14 | gitMutationKeys, |
| 15 | gitQueryKeys, |
| 5 | 16 | gitPreparePullRequestThreadMutationOptions, |
| 6 | 17 | gitPullMutationOptions, |
| 7 | 18 | gitRunStackedActionMutationOptions, |
| 19 | invalidateGitStatusQuery, |
| 20 | gitStatusQueryOptions, |
| 21 | invalidateGitQueries, |
| 8 | 22 | } from "./gitReactQuery"; |
| 9 | 23 | |
| 10 | 24 | describe("gitMutationKeys", () => { |
| ... | ... | @@ -49,3 +63,51 @@ describe("git mutation options", () => { |
| 49 | 63 | expect(options.mutationKey).toEqual(gitMutationKeys.preparePullRequestThread("/repo/a")); |
| 50 | 64 | }); |
| 51 | 65 | }); |
| 66 | |
| 67 | describe("invalidateGitQueries", () => { |
| 68 | it("can invalidate a single cwd without blasting other git query scopes", async () => { |
| 69 | const queryClient = new QueryClient(); |
| 70 | |
| 71 | queryClient.setQueryData(gitQueryKeys.status("/repo/a"), { ok: "a" }); |
| 72 | queryClient.setQueryData(gitQueryKeys.branches("/repo/a"), { ok: "a-branches" }); |
| 73 | queryClient.setQueryData(gitQueryKeys.status("/repo/b"), { ok: "b" }); |
| 74 | queryClient.setQueryData(gitQueryKeys.branches("/repo/b"), { ok: "b-branches" }); |
| 75 | |
| 76 | await invalidateGitQueries(queryClient, { cwd: "/repo/a" }); |
| 77 | |
| 78 | expect( |
| 79 | queryClient.getQueryState(gitStatusQueryOptions("/repo/a").queryKey)?.isInvalidated, |
| 80 | ).toBe(true); |
| 81 | expect( |
| 82 | queryClient.getQueryState(gitBranchesQueryOptions("/repo/a").queryKey)?.isInvalidated, |
| 83 | ).toBe(true); |
| 84 | expect( |
| 85 | queryClient.getQueryState(gitStatusQueryOptions("/repo/b").queryKey)?.isInvalidated, |
| 86 | ).toBe(false); |
| 87 | expect( |
| 88 | queryClient.getQueryState(gitBranchesQueryOptions("/repo/b").queryKey)?.isInvalidated, |
| 89 | ).toBe(false); |
| 90 | }); |
| 91 | }); |
| 92 | |
| 93 | describe("invalidateGitStatusQuery", () => { |
| 94 | it("invalidates only status for the selected cwd", async () => { |
| 95 | const queryClient = new QueryClient(); |
| 96 | |
| 97 | queryClient.setQueryData(gitQueryKeys.status("/repo/a"), { ok: "a" }); |
| 98 | queryClient.setQueryData(gitQueryKeys.branches("/repo/a"), { ok: "a-branches" }); |
| 99 | queryClient.setQueryData(gitQueryKeys.status("/repo/b"), { ok: "b" }); |
| 100 | |
| 101 | await invalidateGitStatusQuery(queryClient, "/repo/a"); |
| 102 | |
| 103 | expect( |
| 104 | queryClient.getQueryState(gitStatusQueryOptions("/repo/a").queryKey)?.isInvalidated, |
| 105 | ).toBe(true); |
| 106 | expect( |
| 107 | queryClient.getQueryState(gitBranchesQueryOptions("/repo/a").queryKey)?.isInvalidated, |
| 108 | ).toBe(false); |
| 109 | expect( |
| 110 | queryClient.getQueryState(gitStatusQueryOptions("/repo/b").queryKey)?.isInvalidated, |
| 111 | ).toBe(false); |
| 112 | }); |
| 113 | }); |