Open History to see the last commit
1import { QueryClient } from "@tanstack/react-query";
2import { describe, expect, it, vi } from "vitest";
3
4vi.mock("../nativeApi", () => ({
5 ensureNativeApi: vi.fn(),
6}));
7
8vi.mock("../wsRpcClient", () => ({
9 getWsRpcClient: vi.fn(),
10}));
11
12import {
13 gitBranchesQueryOptions,
14 gitMutationKeys,
15 gitQueryKeys,
16 gitPreparePullRequestThreadMutationOptions,
17 gitPullMutationOptions,
18 gitRunStackedActionMutationOptions,
19 invalidateGitStatusQuery,
20 gitStatusQueryOptions,
21 invalidateGitQueries,
22} from "./gitReactQuery";
23
24describe("gitMutationKeys", () => {
25 it("scopes stacked action keys by cwd", () => {
26 expect(gitMutationKeys.runStackedAction("/repo/a")).not.toEqual(
27 gitMutationKeys.runStackedAction("/repo/b"),
28 );
29 });
30
31 it("scopes pull keys by cwd", () => {
32 expect(gitMutationKeys.pull("/repo/a")).not.toEqual(gitMutationKeys.pull("/repo/b"));
33 });
34
35 it("scopes pull request thread preparation keys by cwd", () => {
36 expect(gitMutationKeys.preparePullRequestThread("/repo/a")).not.toEqual(
37 gitMutationKeys.preparePullRequestThread("/repo/b"),
38 );
39 });
40});
41
42describe("git mutation options", () => {
43 const queryClient = new QueryClient();
44
45 it("attaches cwd-scoped mutation key for runStackedAction", () => {
46 const options = gitRunStackedActionMutationOptions({
47 cwd: "/repo/a",
48 queryClient,
49 });
50 expect(options.mutationKey).toEqual(gitMutationKeys.runStackedAction("/repo/a"));
51 });
52
53 it("attaches cwd-scoped mutation key for pull", () => {
54 const options = gitPullMutationOptions({ cwd: "/repo/a", queryClient });
55 expect(options.mutationKey).toEqual(gitMutationKeys.pull("/repo/a"));
56 });
57
58 it("attaches cwd-scoped mutation key for preparePullRequestThread", () => {
59 const options = gitPreparePullRequestThreadMutationOptions({
60 cwd: "/repo/a",
61 queryClient,
62 });
63 expect(options.mutationKey).toEqual(gitMutationKeys.preparePullRequestThread("/repo/a"));
64 });
65});
66
67describe("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
93describe("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});