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

gitReactQuery.test.ts

135 lines3.9 KB

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