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

pullRequestReviewStore.test.ts

65 lines2.7 KB

1import { beforeEach, describe, expect, it } from "vite-plus/test";
2import { ProjectId } from "@t3tools/contracts";
3​
4import {
5 type PendingReviewComment,
6 pullRequestReviewKey,
7 usePullRequestReviewStore,
8} from "./pullRequestReviewStore";
9​
10function comment(id: string, body = id): PendingReviewComment {
11 return { id, body, path: "src/app.ts", position: { kind: "added", newLine: 1 } };
13​
14describe("pull request review drafts", () => {
15 beforeEach(() => {
16 usePullRequestReviewStore.setState({ drafts: {}, summaries: {} });
17 });
18​
19 it("removes only the line comments included in a submitted snapshot", () => {
20 const store = usePullRequestReviewStore.getState();
21 store.addComment("review-a", comment("submitted"));
22 const submittedIds =
23 usePullRequestReviewStore.getState().drafts["review-a"]?.map((entry) => entry.id) ?? [];
24​
25 usePullRequestReviewStore.getState().addComment("review-a", comment("added-in-flight"));
26 usePullRequestReviewStore.getState().removeComments("review-a", submittedIds);
27​
28 expect(usePullRequestReviewStore.getState().drafts["review-a"]).toEqual([
29 comment("added-in-flight"),
30 ]);
31 });
32​
33 it("keeps drafts on different hosts separate when a thread reviews the same repository and number", () => {
34 const reference = {
35 projectId: ProjectId.make("project-a"),
36 repository: "owner/repo",
37 number: 7,
38 };
39 const publicKey = pullRequestReviewKey({ ...reference, host: "github.com" });
40 const enterpriseKey = pullRequestReviewKey({ ...reference, host: "github.example.com" });
41 const store = usePullRequestReviewStore.getState();
42 store.addComment(publicKey, comment("public"));
43 store.setSummary(publicKey, "Public review");
44​
45 expect(usePullRequestReviewStore.getState().drafts[enterpriseKey]).toBeUndefined();
46 expect(usePullRequestReviewStore.getState().summaries[enterpriseKey]).toBeUndefined();
47​
48 store.addComment(enterpriseKey, comment("enterprise"));
49 store.setSummary(enterpriseKey, "Enterprise review");
50 store.clear(enterpriseKey);
51 store.clearSummary(enterpriseKey, "Enterprise review");
52​
53 expect(usePullRequestReviewStore.getState().drafts[publicKey]).toEqual([comment("public")]);
54 expect(usePullRequestReviewStore.getState().summaries[publicKey]).toBe("Public review");
55 });
56​
57 it("does not clear a summary revised while submission is in flight", () => {
58 const store = usePullRequestReviewStore.getState();
59 store.setSummary("review-a", "Submitted body");
60 usePullRequestReviewStore.getState().setSummary("review-a", "Revised body");
61 usePullRequestReviewStore.getState().clearSummary("review-a", "Submitted body");
62​
63 expect(usePullRequestReviewStore.getState().summaries["review-a"]).toBe("Revised body");
64 });
65});