Repository navigation
Expand file tree
/
Copy pathcheckQueryKeys.test.ts
More file actions
139 lines (125 loc) · 5.31 KB
/
Copy pathcheckQueryKeys.test.ts
File metadata and controls
139 lines (125 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// SPDX-FileCopyrightText: 2026 Anton Wiklund and Solid Memo contributors
//
// SPDX-License-Identifier: MIT
import { describe, expect, it, vi } from "vitest";
import { checked, defaultIo, fileProblems, main, run } from "./checkQueryKeys.ts";
const MADE = `import { queryKeys } from "./queryKeys";
export function useDecks(instanceUrl: string) {
const queryClient = useQueryClient();
const queryKey = queryKeys.decks(instanceUrl);
queryClient.setQueryData<Deck[]>(queryKeys.decks(instanceUrl), []);
return useQuery({ queryKey, queryFn: () => [] });
}
`;
describe("checked", () => {
it("reads the screens' TypeScript", () => {
expect(checked("packages/ui/src/ui/Workspace.tsx")).toBe(true);
expect(checked("apps/studio/src/ui/releaseCheck.ts")).toBe(true);
});
it("leaves out the keys modules, tests, tests' helpers and other files", () => {
expect(checked("packages/ui/src/ui/queryKeys.ts")).toBe(false);
expect(checked("apps/studio/src/ui/queryKeys.ts")).toBe(false);
expect(checked("packages/ui/src/ui/Workspace.test.tsx")).toBe(false);
expect(checked("apps/studio/src/ui/releaseCheck.test.ts")).toBe(false);
expect(checked("packages/ui/src/test/useCasesFake.ts")).toBe(false);
expect(checked("packages/ui/src/i18n/en.json")).toBe(false);
});
});
describe("fileProblems", () => {
it("accepts keys made by queryKeys", () => {
expect(fileProblems("Decks.tsx", MADE)).toEqual([]);
});
it("refuses a query or mutation key written out, as a property, a variable or a condition's branch", () => {
const source = `useQuery({ queryKey: ["decks", instanceUrl] });
queryClient.invalidateQueries({ queryKey: ["studyQueue"] });
const mutationKey = ["libraryUpgrade", deck.url];
useQuery({
queryKey:
deckUrl === undefined ? ["statistics", instanceUrl] : queryKeys.statistics(instanceUrl),
});
useQuery({
queryKey: [
"statistics",
],
});
`;
expect(fileProblems("Decks.tsx", source)).toEqual([
"Decks.tsx:1: a queryKey writes its key out; make it with a function of queryKeys.",
"Decks.tsx:2: a queryKey writes its key out; make it with a function of queryKeys.",
"Decks.tsx:3: a mutationKey writes its key out; make it with a function of queryKeys.",
"Decks.tsx:5: a queryKey writes its key out; make it with a function of queryKeys.",
"Decks.tsx:9: a queryKey writes its key out; make it with a function of queryKeys.",
]);
});
it("refuses a key written out in a QueryClient's read or write of a query", () => {
const source = `queryClient.getQueryData<Deck[]>(["decks", instanceUrl]);
queryClient.setQueryData(
["reviews", url, key.cardId],
state,
);
queryClient.getQueryState(["cards", url]);
`;
expect(fileProblems("Decks.tsx", source)).toEqual([
"Decks.tsx:1: a getQueryData writes its key out; make it with a function of queryKeys.",
"Decks.tsx:2: a setQueryData writes its key out; make it with a function of queryKeys.",
"Decks.tsx:6: a getQueryState writes its key out; make it with a function of queryKeys.",
]);
});
it("leaves a comment's keys alone", () => {
const source = `/**
* The decks (queryKey: ["decks", url]) are read once.
*/
// queryKey: ["decks", url]
/* queryKey: ["decks", url] */
`;
expect(fileProblems("Decks.tsx", source)).toEqual([]);
});
});
describe("main", () => {
function fakeIo(files: Record<string, string>) {
const logs: string[] = [];
return {
logs,
files: () => Object.keys(files),
read: (path: string) => files[path]!,
log: (message: string) => logs.push(message),
};
}
it("says so and returns 0 when no screen writes a key out", () => {
const io = fakeIo({
"packages/ui/src/ui/Decks.tsx": MADE,
"packages/ui/src/ui/queryKeys.ts": `export const queryKeys = { decks: (url: string) => ["decks", url] };\nconst queryKey = ["x"];\n`,
"packages/ui/src/ui/Decks.test.tsx": `queryClient.setQueryData(["decks", url], []);\n`,
});
expect(main(io)).toBe(0);
expect(io.logs).toEqual(["None of the 1 source files writes a query key out."]);
});
it("logs each key written out and returns 1", () => {
const io = fakeIo({
"packages/ui/src/ui/Decks.tsx": MADE,
"apps/studio/src/ui/Catalog.tsx": `\nuseQuery({ queryKey: ["catalog", url] });\n`,
});
expect(main(io)).toBe(1);
expect(io.logs).toEqual([
"apps/studio/src/ui/Catalog.tsx:2: a queryKey writes its key out; make it with a function of queryKeys.",
]);
});
});
describe("the repository's screens", () => {
it("make every query key with queryKeys", () => {
const log = vi.spyOn(console, "log").mockImplementation(() => {});
const process = { exitCode: undefined as number | string | null | undefined };
run(process);
expect(process.exitCode).toBe(0);
expect(log).toHaveBeenCalledWith(expect.stringMatching(/^None of the \d+ source files/));
log.mockRestore();
});
it("are read from Solid Memo's and the Studio's sources", () => {
const io = defaultIo(`${import.meta.dirname}/..`);
const files = io.files();
expect(files).toContain("packages/ui/src/ui/Workspace.tsx");
expect(files).toContain("apps/studio/src/ui/StudioWorkspace.tsx");
expect(files.every((file) => /^(packages\/ui|apps\/studio)\/src\//.test(file))).toBe(true);
expect(io.read("packages/ui/src/ui/Workspace.tsx")).toContain("queryKeys.");
});
});