|
| 1 | +import { |
| 2 | + EnvironmentId, |
| 3 | + ProjectId, |
| 4 | + SourceControlProviderKind, |
| 5 | + type PullRequestDetailView, |
| 6 | + type PullRequestDiffResult, |
| 7 | +} from "@t3tools/contracts"; |
| 8 | +import { expect, it, vi } from "vite-plus/test"; |
| 9 | + |
| 10 | +import "~/index.css"; |
| 11 | + |
| 12 | +const { diffAtom } = await vi.hoisted(async () => { |
| 13 | + const { AsyncResult, Atom } = await import("effect/reactivity"); |
| 14 | + const firstPage: PullRequestDiffResult = { |
| 15 | + patch: [ |
| 16 | + "diff --git a/a.ts b/a.ts", |
| 17 | + "index 1111111..2222222 100644", |
| 18 | + "--- a/a.ts", |
| 19 | + "+++ b/a.ts", |
| 20 | + "@@ -1 +1 @@", |
| 21 | + "-before()", |
| 22 | + "+after()", |
| 23 | + "", |
| 24 | + ].join("\n"), |
| 25 | + truncated: false, |
| 26 | + nextCursor: null, |
| 27 | + }; |
| 28 | + // An environment query whose refetch returns equal content hands back the previous value |
| 29 | + // itself, so a refresh publishes a new result around the very same page. |
| 30 | + return { diffAtom: Atom.make(() => AsyncResult.success(firstPage)) }; |
| 31 | +}); |
| 32 | + |
| 33 | +vi.mock(import("~/state/use-atom-command"), async (original) => ({ |
| 34 | + ...(await original()), |
| 35 | + useAtomCommand: (() => vi.fn()) as never, |
| 36 | +})); |
| 37 | +vi.mock(import("~/state/pullRequests"), async (original) => { |
| 38 | + const actual = await original(); |
| 39 | + return { |
| 40 | + ...actual, |
| 41 | + pullRequestEnvironment: { |
| 42 | + ...actual.pullRequestEnvironment, |
| 43 | + diff: (() => diffAtom) as never, |
| 44 | + }, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +import { render } from "../../../test/render"; |
| 49 | +import PullRequestCodeTab from "./PullRequestCodeTab"; |
| 50 | + |
| 51 | +const detail: PullRequestDetailView = { |
| 52 | + provider: SourceControlProviderKind.make("github"), |
| 53 | + projectId: ProjectId.make("project"), |
| 54 | + projectTitle: "Project", |
| 55 | + workspaceRoot: "/workspace", |
| 56 | + repository: "owner/repo", |
| 57 | + number: 1, |
| 58 | + title: "Test pull request", |
| 59 | + body: "", |
| 60 | + url: "https://github.com/owner/repo/pull/1", |
| 61 | + author: { login: "author", name: null, avatarUrl: null }, |
| 62 | + viewer: "author", |
| 63 | + state: "open", |
| 64 | + isDraft: false, |
| 65 | + mergeability: "mergeable", |
| 66 | + additions: 1, |
| 67 | + deletions: 1, |
| 68 | + changedFiles: 1, |
| 69 | + headBranch: "feature", |
| 70 | + baseBranch: "main", |
| 71 | + createdAt: "2026-09-01T00:00:00Z", |
| 72 | + updatedAt: "2026-09-01T00:00:00Z", |
| 73 | + mergedAt: null, |
| 74 | + closedAt: null, |
| 75 | + reviewers: [], |
| 76 | + labels: [], |
| 77 | + checks: [], |
| 78 | + comments: [], |
| 79 | + commentCount: 0, |
| 80 | + commentsTruncated: false, |
| 81 | + reviewThreads: [], |
| 82 | + commits: [], |
| 83 | + mergeCapabilities: { merge: false, squash: false, rebase: false }, |
| 84 | + capabilities: { |
| 85 | + diff: true, |
| 86 | + comment: false, |
| 87 | + search: true, |
| 88 | + actions: [], |
| 89 | + mergeMethods: [], |
| 90 | + review: { inlineComment: false, reply: false, resolve: false, verdicts: [] }, |
| 91 | + reviewers: { request: false, listCandidates: false }, |
| 92 | + edit: { changeRequest: false, comment: false }, |
| 93 | + }, |
| 94 | + viewerPermissions: { |
| 95 | + actions: [], |
| 96 | + comment: false, |
| 97 | + resolve: false, |
| 98 | + verdicts: [], |
| 99 | + requestReviewers: false, |
| 100 | + }, |
| 101 | +}; |
| 102 | + |
| 103 | +const tab = (refreshToken: number) => ( |
| 104 | + <PullRequestCodeTab |
| 105 | + scrollerRef={() => {}} |
| 106 | + environmentId={EnvironmentId.make("environment")} |
| 107 | + reference={detail} |
| 108 | + detail={detail} |
| 109 | + selectedCommitOid={null} |
| 110 | + onSelectedCommitChange={() => {}} |
| 111 | + onRefresh={() => {}} |
| 112 | + refreshToken={refreshToken} |
| 113 | + /> |
| 114 | +); |
| 115 | + |
| 116 | +it("keeps the diff after a refresh that brings back the same page", async () => { |
| 117 | + const container = document.createElement("div"); |
| 118 | + container.className = "flex h-[700px] w-[900px]"; |
| 119 | + await using screen = await render(tab(0), { container }); |
| 120 | + await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 121 | + |
| 122 | + await screen.rerender(tab(1)); |
| 123 | + |
| 124 | + await expect |
| 125 | + .element(screen.getByText("This pull request has no file changes.")) |
| 126 | + .not.toBeInTheDocument(); |
| 127 | + await expect.element(screen.getByText("1 file", { exact: true })).toBeVisible(); |
| 128 | +}); |
0 commit comments