The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { act, render, type Screen } from "../../test/render"; |
| 4 | import type { PullRequestListPreferences } from "./pullRequest/pullRequestListPreferences"; |
| 5 | import { PULL_REQUEST_SAVED_VIEWS } from "./pullRequest/pullRequestSavedViews"; |
| 6 | import { SavedViewsMenu, type SavedView } from "./SavedViewsMenu"; |
| 7 | |
| 8 | const PULL_REQUEST_SAVED_VIEWS_KEY = PULL_REQUEST_SAVED_VIEWS.storageKey; |
| 9 | |
| 10 | const { toastAdd } = vi.hoisted(() => ({ toastAdd: vi.fn() })); |
| 11 | vi.mock("./ui/toast", () => ({ toastManager: { add: toastAdd } })); |
| 12 | |
| 13 | const reviewing = { |
| 14 | involvement: "reviewing", |
| 15 | state: "open", |
| 16 | q: "bug", |
| 17 | draft: "hide", |
| 18 | sort: "updated", |
| 19 | } as const satisfies PullRequestListPreferences; |
| 20 | |
| 21 | function setup() { |
| 22 | localStorage.clear(); |
| 23 | const onApply = vi.fn(); |
| 24 | toastAdd.mockClear(); |
| 25 | return { |
| 26 | onApply, |
| 27 | menu: (preferences: PullRequestListPreferences = reviewing) => ( |
| 28 | <SavedViewsMenu kind={PULL_REQUEST_SAVED_VIEWS} preferences={preferences} onApply={onApply} /> |
| 29 | ), |
| 30 | [Symbol.dispose]() { |
| 31 | localStorage.clear(); |
| 32 | vi.restoreAllMocks(); |
| 33 | }, |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | async function openMenu(screen: Screen) { |
| 38 | await act(() => |
| 39 | screen.container |
| 40 | .querySelector<HTMLButtonElement>('[aria-label="Saved pull request views"]')! |
| 41 | .click(), |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | async function choose(label: string) { |
| 46 | const item = [...document.querySelectorAll<HTMLElement>('[role="menuitem"]')].find( |
| 47 | (element) => element.textContent === label, |
| 48 | ); |
| 49 | expect(item, label).toBeDefined(); |
| 50 | await act(() => item!.click()); |
| 51 | } |
| 52 | |
| 53 | async function nameView(name: string) { |
| 54 | const input = document.querySelector<HTMLInputElement>("input")!; |
| 55 | await act(() => { |
| 56 | Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, name); |
| 57 | input.dispatchEvent(new Event("input", { bubbles: true })); |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | async function submit() { |
| 62 | await act(() => |
| 63 | document |
| 64 | .querySelector("form")! |
| 65 | .dispatchEvent(new Event("submit", { bubbles: true, cancelable: true })), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | function storedViews(): ReadonlyArray<SavedView<PullRequestListPreferences>> { |
| 70 | return JSON.parse(localStorage.getItem(PULL_REQUEST_SAVED_VIEWS_KEY) ?? "[]"); |
| 71 | } |
| 72 | |
| 73 | describe("SavedViewsMenu", () => { |
| 74 | it("saves a named filter setup and restores it after remounting", async () => { |
| 75 | using fixture = setup(); |
| 76 | const { menu } = fixture; |
| 77 | await using first = await render(menu()); |
| 78 | await openMenu(first); |
| 79 | await choose("Save current view…"); |
| 80 | expect(document.querySelector("input")?.value).toBe("PRs Requesting My Review"); |
| 81 | await submit(); |
| 82 | expect(storedViews()).toEqual([ |
| 83 | { id: expect.any(String), name: "PRs Requesting My Review", preferences: reviewing }, |
| 84 | ]); |
| 85 | |
| 86 | await first.unmount(); |
| 87 | const merged = { involvement: "all", state: "merged", checks: "failing" } as const; |
| 88 | await using screen = await render(menu(merged)); |
| 89 | await openMenu(screen); |
| 90 | await choose("PRs Requesting My Review"); |
| 91 | expect(fixture.onApply).toHaveBeenCalledExactlyOnceWith(reviewing); |
| 92 | }); |
| 93 | |
| 94 | it("updates, renames, and deletes a view without affecting another saved view", async () => { |
| 95 | using fixture = setup(); |
| 96 | const { menu } = fixture; |
| 97 | localStorage.setItem( |
| 98 | PULL_REQUEST_SAVED_VIEWS_KEY, |
| 99 | JSON.stringify([ |
| 100 | { id: "reviewing", name: "Review queue", preferences: reviewing }, |
| 101 | { id: "authored", name: "My PRs", preferences: { involvement: "authored", state: "open" } }, |
| 102 | ]), |
| 103 | ); |
| 104 | await using screen = await render(menu()); |
| 105 | await openMenu(screen); |
| 106 | await choose("Review queue"); |
| 107 | await screen.rerender(menu({ ...reviewing, checks: "passing" })); |
| 108 | await openMenu(screen); |
| 109 | await choose("Update saved view"); |
| 110 | expect(storedViews()[0]?.preferences.checks).toBe("passing"); |
| 111 | await openMenu(screen); |
| 112 | await choose("Rename view…"); |
| 113 | await nameView("My PRs"); |
| 114 | expect(document.body.textContent).toContain("A view with this name already exists."); |
| 115 | await nameView(" Ready to review "); |
| 116 | await submit(); |
| 117 | expect(storedViews()[0]?.name).toBe("Ready to review"); |
| 118 | await openMenu(screen); |
| 119 | await choose("Delete view"); |
| 120 | expect(storedViews().map((view) => view.id)).toEqual(["authored"]); |
| 121 | }); |
| 122 | |
| 123 | it("keeps an inferred view as the update target when edits match another view", async () => { |
| 124 | using fixture = setup(); |
| 125 | const { menu } = fixture; |
| 126 | const other = { |
| 127 | id: "other", |
| 128 | name: "Other queue", |
| 129 | preferences: { ...reviewing, checks: "passing" }, |
| 130 | } as const; |
| 131 | localStorage.setItem( |
| 132 | PULL_REQUEST_SAVED_VIEWS_KEY, |
| 133 | JSON.stringify([{ id: "reviewing", name: "Review queue", preferences: reviewing }, other]), |
| 134 | ); |
| 135 | await using screen = await render(menu()); |
| 136 | await screen.rerender(menu(other.preferences)); |
| 137 | expect(screen.container.textContent).toContain("Review queue*"); |
| 138 | await openMenu(screen); |
| 139 | await choose("Update saved view"); |
| 140 | expect(storedViews()).toEqual([ |
| 141 | { id: "reviewing", name: "Review queue", preferences: other.preferences }, |
| 142 | other, |
| 143 | ]); |
| 144 | await openMenu(screen); |
| 145 | await choose("Other queue"); |
| 146 | expect(screen.container.textContent).toContain("Other queue"); |
| 147 | }); |
| 148 | }); |