Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitReactQuery.test.ts

118 lines3.3 KB

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 type { InfiniteData } from "@tanstack/react-query";
13import type { GitListBranchesResult } from "@t3tools/contracts";
14​
15import {
16 gitBranchSearchInfiniteQueryOptions,
17 gitMutationKeys,
18 gitPreparePullRequestThreadMutationOptions,
19 gitPullMutationOptions,
20 gitRunStackedActionMutationOptions,
21 invalidateGitQueries,
22} from "./gitReactQuery";
23​
24const BRANCH_QUERY_RESULT: GitListBranchesResult = {
25 branches: [],
26 isRepo: true,
27 hasOriginRemote: true,
28 nextCursor: null,
29 totalCount: 0,
30};
31​
32const BRANCH_SEARCH_RESULT: InfiniteData<GitListBranchesResult, number> = {
33 pages: [BRANCH_QUERY_RESULT],
34 pageParams: [0],
35};
36​
37describe("gitMutationKeys", () => {
38 it("scopes stacked action keys by cwd", () => {
39 expect(gitMutationKeys.runStackedAction("/repo/a")).not.toEqual(
40 gitMutationKeys.runStackedAction("/repo/b"),
41 );
42 });
43​
44 it("scopes pull keys by cwd", () => {
45 expect(gitMutationKeys.pull("/repo/a")).not.toEqual(gitMutationKeys.pull("/repo/b"));
46 });
47​
48 it("scopes pull request thread preparation keys by cwd", () => {
49 expect(gitMutationKeys.preparePullRequestThread("/repo/a")).not.toEqual(
50 gitMutationKeys.preparePullRequestThread("/repo/b"),
51 );
52 });
53});
54​
55describe("git mutation options", () => {
56 const queryClient = new QueryClient();
57​
58 it("attaches cwd-scoped mutation key for runStackedAction", () => {
59 const options = gitRunStackedActionMutationOptions({
60 cwd: "/repo/a",
61 queryClient,
62 });
63 expect(options.mutationKey).toEqual(gitMutationKeys.runStackedAction("/repo/a"));
64 });
65​
66 it("attaches cwd-scoped mutation key for pull", () => {
67 const options = gitPullMutationOptions({ cwd: "/repo/a", queryClient });
68 expect(options.mutationKey).toEqual(gitMutationKeys.pull("/repo/a"));
69 });
70​
71 it("attaches cwd-scoped mutation key for preparePullRequestThread", () => {
72 const options = gitPreparePullRequestThreadMutationOptions({
73 cwd: "/repo/a",
74 queryClient,
75 });
76 expect(options.mutationKey).toEqual(gitMutationKeys.preparePullRequestThread("/repo/a"));
77 });
78});
79​
80describe("invalidateGitQueries", () => {
81 it("can invalidate a single cwd without blasting other git query scopes", async () => {
82 const queryClient = new QueryClient();
83​
84 queryClient.setQueryData(
85 gitBranchSearchInfiniteQueryOptions({
86 cwd: "/repo/a",
87 query: "feature",
88 }).queryKey,
89 BRANCH_SEARCH_RESULT,
90 );
91 queryClient.setQueryData(
92 gitBranchSearchInfiniteQueryOptions({
93 cwd: "/repo/b",
94 query: "feature",
95 }).queryKey,
96 BRANCH_SEARCH_RESULT,
97 );
98​
99 await invalidateGitQueries(queryClient, { cwd: "/repo/a" });
100​
101 expect(
102 queryClient.getQueryState(
103 gitBranchSearchInfiniteQueryOptions({
104 cwd: "/repo/a",
105 query: "feature",
106 }).queryKey,
107 )?.isInvalidated,
108 ).toBe(true);
109 expect(
110 queryClient.getQueryState(
111 gitBranchSearchInfiniteQueryOptions({
112 cwd: "/repo/b",
113 query: "feature",
114 }).queryKey,
115 )?.isInvalidated,
116 ).toBe(false);
117 });
118});