|
| 1 | +import { act } from "react"; |
| 2 | +import { createRoot } from "react-dom/client"; |
| 3 | +import { expect, it } from "vite-plus/test"; |
| 4 | +import type { PullRequestDiffParams } from "./usePullRequestDiff"; |
| 5 | +import { usePullRequestComparisonState } from "./usePullRequestComparisonState"; |
| 6 | + |
| 7 | +const pr: PullRequestDiffParams = { |
| 8 | + environmentId: "environment", |
| 9 | + projectId: "project", |
| 10 | + host: "dev.azure.com", |
| 11 | + repository: "repo", |
| 12 | + number: 1, |
| 13 | +}; |
| 14 | + |
| 15 | +function setup() { |
| 16 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 17 | + const container = document.createElement("div"); |
| 18 | + const root = createRoot(container); |
| 19 | + function Probe({ params }: { params: PullRequestDiffParams }) { |
| 20 | + const comparison = usePullRequestComparisonState(params); |
| 21 | + return ( |
| 22 | + <> |
| 23 | + <button type="button" onClick={() => comparison.toggle("notes.txt")}> |
| 24 | + {comparison.isViewed("notes.txt") ? "Viewed" : "Unviewed"} |
| 25 | + </button> |
| 26 | + <button |
| 27 | + type="button" |
| 28 | + onClick={() => |
| 29 | + comparison.setCollapsedIds((ids) => |
| 30 | + ids.includes("notes.txt") |
| 31 | + ? ids.filter((id) => id !== "notes.txt") |
| 32 | + : [...ids, "notes.txt"], |
| 33 | + ) |
| 34 | + } |
| 35 | + > |
| 36 | + Toggle file |
| 37 | + </button> |
| 38 | + {!comparison.collapsedIds.includes("notes.txt") ? <p>File contents</p> : null} |
| 39 | + </> |
| 40 | + ); |
| 41 | + } |
| 42 | + const render = (params: PullRequestDiffParams) => |
| 43 | + act(() => root.render(<Probe params={params} />)); |
| 44 | + return { |
| 45 | + render, |
| 46 | + isViewed: () => container.querySelector("button")?.textContent === "Viewed", |
| 47 | + fileVisible: () => container.querySelector("p")?.textContent === "File contents", |
| 48 | + toggleFile: () => act(() => container.querySelectorAll("button")[1]?.click()), |
| 49 | + toggle: () => act(() => container.querySelector("button")?.click()), |
| 50 | + [Symbol.dispose]: () => act(() => root.unmount()), |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +it("keeps a viewed path within one comparison but clears it when the comparison changes", () => { |
| 55 | + using screen = setup(); |
| 56 | + const first = { ...pr, iterationRange: { from: 1, to: 2 } }; |
| 57 | + screen.render(first); |
| 58 | + screen.toggle(); |
| 59 | + screen.render({ ...first }); |
| 60 | + expect(screen.isViewed()).toBe(true); |
| 61 | + |
| 62 | + screen.render({ ...pr, iterationRange: { from: 2, to: 3 } }); |
| 63 | + expect(screen.isViewed()).toBe(false); |
| 64 | + screen.toggle(); |
| 65 | + expect(screen.isViewed()).toBe(true); |
| 66 | + |
| 67 | + screen.render(first); |
| 68 | + expect(screen.isViewed()).toBe(false); |
| 69 | +}); |
| 70 | + |
| 71 | +it("keeps historical viewed paths separate from latest and from other pull requests", () => { |
| 72 | + using screen = setup(); |
| 73 | + screen.render({ ...pr, commit: "abc" }); |
| 74 | + screen.toggle(); |
| 75 | + screen.render(pr); |
| 76 | + expect(screen.isViewed()).toBe(false); |
| 77 | + screen.render({ ...pr, commit: "abc", number: 2 }); |
| 78 | + expect(screen.isViewed()).toBe(false); |
| 79 | +}); |
| 80 | + |
| 81 | +it("clears a comparison's viewed paths even when the next comparison is not toggled", () => { |
| 82 | + using screen = setup(); |
| 83 | + const first = { ...pr, iterationRange: { from: 1, to: 2 } }; |
| 84 | + screen.render(first); |
| 85 | + screen.toggle(); |
| 86 | + screen.render({ ...pr, iterationRange: { from: 2, to: 3 } }); |
| 87 | + expect(screen.isViewed()).toBe(false); |
| 88 | + screen.render(first); |
| 89 | + expect(screen.isViewed()).toBe(false); |
| 90 | +}); |
| 91 | + |
| 92 | +it("retains file collapse within a comparison and clears it across A → B → A without toggling B", () => { |
| 93 | + using screen = setup(); |
| 94 | + const first = { ...pr, iterationRange: { from: 1, to: 2 } }; |
| 95 | + screen.render(first); |
| 96 | + screen.toggleFile(); |
| 97 | + expect(screen.fileVisible()).toBe(false); |
| 98 | + screen.render({ ...first }); |
| 99 | + expect(screen.fileVisible()).toBe(false); |
| 100 | + |
| 101 | + screen.render({ ...pr, iterationRange: { from: 2, to: 3 } }); |
| 102 | + expect(screen.fileVisible()).toBe(true); |
| 103 | + screen.render(first); |
| 104 | + expect(screen.fileVisible()).toBe(true); |
| 105 | + screen.toggleFile(); |
| 106 | + expect(screen.fileVisible()).toBe(false); |
| 107 | + screen.toggleFile(); |
| 108 | + expect(screen.fileVisible()).toBe(true); |
| 109 | +}); |
| 110 | + |
| 111 | +it.each([ |
| 112 | + { commit: "def" }, |
| 113 | + { commit: undefined }, |
| 114 | + { iterationRange: { from: 1, to: 2 } }, |
| 115 | + { environmentId: "other-environment" }, |
| 116 | + { projectId: "other-project" }, |
| 117 | + { host: "other-host" }, |
| 118 | + { repository: "other-repo" }, |
| 119 | + { number: 2 }, |
| 120 | +])("clears collapsed files when the comparison identity changes by %j", (change) => { |
| 121 | + using screen = setup(); |
| 122 | + const first = { ...pr, commit: "abc" }; |
| 123 | + screen.render(first); |
| 124 | + screen.toggleFile(); |
| 125 | + screen.render({ ...first, ...change }); |
| 126 | + expect(screen.fileVisible()).toBe(true); |
| 127 | + screen.render(first); |
| 128 | + expect(screen.fileVisible()).toBe(true); |
| 129 | +}); |
| 130 | + |
| 131 | +it("preserves viewed paths when collapse changes and collapsed files when viewed paths change", () => { |
| 132 | + using screen = setup(); |
| 133 | + screen.render({ ...pr, iterationRange: { from: 1, to: 2 } }); |
| 134 | + screen.toggleFile(); |
| 135 | + screen.toggle(); |
| 136 | + expect(screen.fileVisible()).toBe(false); |
| 137 | + expect(screen.isViewed()).toBe(true); |
| 138 | + screen.toggleFile(); |
| 139 | + expect(screen.fileVisible()).toBe(true); |
| 140 | + expect(screen.isViewed()).toBe(true); |
| 141 | +}); |
0 commit comments