|
| 1 | +// @vitest-environment happy-dom |
| 2 | +import { act } from "react"; |
| 3 | +import { createRoot, type Root } from "react-dom/client"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { RepositoryBranchSelect } from "./RepositoryBranchSelect"; |
| 6 | +import { CustomSelect } from "@/components/ui/CustomSelect"; |
| 7 | + |
| 8 | +const api = vi.hoisted(() => ({ listBranches: vi.fn(), getBranchPage: vi.fn() })); |
| 9 | +vi.mock("@/lib/api/github", () => ({ githubApi: api })); |
| 10 | +vi.mock("@/lib/api/projects", () => ({ projectsApi: api })); |
| 11 | +let container: HTMLDivElement; |
| 12 | +let root: Root; |
| 13 | +const changed = vi.fn(); |
| 14 | +const page = (names: string[], number = 1, hasMore = false) => ({ |
| 15 | + data: names.map((name) => ({ name })), |
| 16 | + pagination: { page: number, perPage: 100, hasMore }, |
| 17 | +}); |
| 18 | +beforeEach(() => { |
| 19 | + vi.resetAllMocks(); |
| 20 | + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); |
| 21 | + container = document.createElement("div"); |
| 22 | + document.body.append(container); |
| 23 | + root = createRoot(container); |
| 24 | +}); |
| 25 | +afterEach(async () => { |
| 26 | + await act(async () => root.unmount()); |
| 27 | + container.remove(); |
| 28 | + vi.unstubAllGlobals(); |
| 29 | +}); |
| 30 | +async function render(repo = "app", projectId?: string) { |
| 31 | + await act(async () => |
| 32 | + root.render( |
| 33 | + <RepositoryBranchSelect |
| 34 | + owner="acme" |
| 35 | + repo={repo} |
| 36 | + projectId={projectId} |
| 37 | + value="main" |
| 38 | + onChange={changed} |
| 39 | + />, |
| 40 | + ), |
| 41 | + ); |
| 42 | +} |
| 43 | +async function open() { |
| 44 | + await act(async () => |
| 45 | + container.querySelector<HTMLButtonElement>('button[aria-haspopup="listbox"]')!.click(), |
| 46 | + ); |
| 47 | +} |
| 48 | +async function search(query: string) { |
| 49 | + const input = document.querySelector<HTMLInputElement>('input[role="combobox"]')!; |
| 50 | + await act(async () => { |
| 51 | + Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, query); |
| 52 | + input.dispatchEvent(new Event("input", { bubbles: true })); |
| 53 | + }); |
| 54 | +} |
| 55 | +function options() { |
| 56 | + return [...document.querySelectorAll<HTMLButtonElement>('[role="option"]')]; |
| 57 | +} |
| 58 | + |
| 59 | +describe("repository branch selection (#870)", () => { |
| 60 | + it("keeps main pinned and searches branches beyond the first hundred in the migration picker", async () => { |
| 61 | + api.listBranches |
| 62 | + .mockResolvedValueOnce( |
| 63 | + page( |
| 64 | + Array.from({ length: 100 }, (_, i) => `a-${i}`), |
| 65 | + 1, |
| 66 | + true, |
| 67 | + ), |
| 68 | + ) |
| 69 | + .mockResolvedValueOnce(page(["main", "master", "z-last"], 2)); |
| 70 | + await render(); |
| 71 | + await open(); |
| 72 | + expect(options()[0]?.textContent).toBe("main"); |
| 73 | + expect(options()).toHaveLength(101); |
| 74 | + await search("z-last"); |
| 75 | + expect(api.listBranches).toHaveBeenLastCalledWith("acme", "app", 2); |
| 76 | + expect(options().map((option) => option.textContent)).toEqual(["z-last"]); |
| 77 | + await act(async () => |
| 78 | + document |
| 79 | + .querySelector("input")! |
| 80 | + .dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })), |
| 81 | + ); |
| 82 | + expect(changed).toHaveBeenCalledWith("z-last"); |
| 83 | + expect(document.querySelector('[role="listbox"]')).toBeNull(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("uses the project-scoped paginated operation for an existing deployment", async () => { |
| 87 | + api.getBranchPage.mockResolvedValue(page(["feature", "main"])); |
| 88 | + await render("app", "project-id"); |
| 89 | + await open(); |
| 90 | + expect(api.getBranchPage).toHaveBeenCalledWith("project-id", 1); |
| 91 | + expect(api.listBranches).not.toHaveBeenCalled(); |
| 92 | + await act(async () => |
| 93 | + options() |
| 94 | + .find((option) => option.textContent === "feature")! |
| 95 | + .click(), |
| 96 | + ); |
| 97 | + expect(changed).toHaveBeenCalledWith("feature"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("discards a previous repository's late page when the repository changes", async () => { |
| 101 | + let finish!: (result: ReturnType<typeof page>) => void; |
| 102 | + api.listBranches.mockImplementation((_owner, repo) => |
| 103 | + repo === "old" |
| 104 | + ? new Promise((resolve) => { |
| 105 | + finish = resolve; |
| 106 | + }) |
| 107 | + : Promise.resolve(page(["new-only"])), |
| 108 | + ); |
| 109 | + await render("old"); |
| 110 | + await open(); |
| 111 | + await render("new"); |
| 112 | + await open(); |
| 113 | + await act(async () => finish(page(["old-only"]))); |
| 114 | + expect(options().map((option) => option.textContent)).toEqual(["main", "new-only"]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("shows a retryable page failure without repeatedly requesting the failed page", async () => { |
| 118 | + api.listBranches.mockRejectedValueOnce(new Error("offline")); |
| 119 | + await render(); |
| 120 | + await open(); |
| 121 | + await search("feature"); |
| 122 | + expect(api.listBranches).toHaveBeenCalledTimes(1); |
| 123 | + expect(container.querySelector('[role="alert"]')?.textContent).toContain( |
| 124 | + "Could not load branches", |
| 125 | + ); |
| 126 | + api.listBranches.mockResolvedValue(page(["feature"])); |
| 127 | + await act(async () => |
| 128 | + container.querySelector<HTMLButtonElement>('[role="alert"] button')!.click(), |
| 129 | + ); |
| 130 | + expect(options().map((option) => option.textContent)).toEqual(["feature"]); |
| 131 | + expect(container.querySelector('[role="alert"]')).toBeNull(); |
| 132 | + }); |
| 133 | + |
| 134 | + it("preserves description search and query-specific empty messages for existing selectors", async () => { |
| 135 | + await act(async () => |
| 136 | + root.render( |
| 137 | + <CustomSelect |
| 138 | + value="one" |
| 139 | + options={[{ value: "one", label: "Project", description: "example.test" }]} |
| 140 | + onChange={changed} |
| 141 | + searchPlaceholder="Search projects" |
| 142 | + emptySearchMessage={(query) => `No project: ${query}`} |
| 143 | + />, |
| 144 | + ), |
| 145 | + ); |
| 146 | + await open(); |
| 147 | + await search("example.test"); |
| 148 | + expect(options()).toHaveLength(1); |
| 149 | + await search("missing"); |
| 150 | + expect(document.body.textContent).toContain("No project: missing"); |
| 151 | + }); |
| 152 | +}); |
0 commit comments