|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { screen, within } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { renderWithProviders as render } from "@/test/test-utils"; |
| 5 | +import { PromptDefinitionTable } from "./PromptDefinitionTable"; |
| 6 | +import type { PromptRead } from "@/generated/types"; |
| 7 | + |
| 8 | +function mockPrompt(overrides?: Partial<NonNullable<PromptRead>>): NonNullable<PromptRead> { |
| 9 | + return { |
| 10 | + id: "p1", |
| 11 | + name: "greet_user", |
| 12 | + originalName: "greet_user", |
| 13 | + customName: "", |
| 14 | + customNameSlug: "greet_user", |
| 15 | + description: "Greets the user", |
| 16 | + template: "Hello {user_name}", |
| 17 | + arguments: [], |
| 18 | + createdAt: "2026-06-30T00:00:00", |
| 19 | + updatedAt: "2026-06-30T00:00:00", |
| 20 | + enabled: true, |
| 21 | + ...overrides, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +describe("PromptDefinitionTable", () => { |
| 26 | + it("renders a row per prompt with its name and a copyable ID", () => { |
| 27 | + const a = mockPrompt({ id: "a", name: "prompt_a" }); |
| 28 | + const b = mockPrompt({ id: "b", name: "prompt_b" }); |
| 29 | + render(<PromptDefinitionTable prompts={[a, b]} onSelectPrompt={vi.fn()} />); |
| 30 | + |
| 31 | + expect(screen.getByRole("cell", { name: "prompt_a" })).toBeInTheDocument(); |
| 32 | + expect(screen.getByRole("cell", { name: "prompt_b" })).toBeInTheDocument(); |
| 33 | + expect(screen.getByText("a")).toBeInTheDocument(); |
| 34 | + expect(screen.getByText("b")).toBeInTheDocument(); |
| 35 | + expect(screen.getAllByRole("button", { name: /copy prompt id/i })).toHaveLength(2); |
| 36 | + }); |
| 37 | + |
| 38 | + it("does not render a Source URL column", () => { |
| 39 | + render(<PromptDefinitionTable prompts={[mockPrompt()]} onSelectPrompt={vi.fn()} />); |
| 40 | + |
| 41 | + expect(screen.queryByRole("columnheader", { name: /source url/i })).not.toBeInTheDocument(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("prefers displayName over the technical name for the Name column", () => { |
| 45 | + const prompt = mockPrompt({ displayName: "Greet User" }); |
| 46 | + render(<PromptDefinitionTable prompts={[prompt]} onSelectPrompt={vi.fn()} />); |
| 47 | + |
| 48 | + expect(screen.getByRole("cell", { name: "Greet User" })).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("calls onSelectPrompt with the row's prompt when the row is clicked", async () => { |
| 52 | + const onSelectPrompt = vi.fn(); |
| 53 | + const user = userEvent.setup(); |
| 54 | + const a = mockPrompt({ id: "a", name: "prompt_a" }); |
| 55 | + const b = mockPrompt({ id: "b", name: "prompt_b" }); |
| 56 | + render(<PromptDefinitionTable prompts={[a, b]} onSelectPrompt={onSelectPrompt} />); |
| 57 | + |
| 58 | + await user.click(screen.getByRole("cell", { name: "prompt_b" })); |
| 59 | + expect(onSelectPrompt).toHaveBeenCalledWith(b); |
| 60 | + }); |
| 61 | + |
| 62 | + it("does not select the row when the copy button is clicked", async () => { |
| 63 | + const onSelectPrompt = vi.fn(); |
| 64 | + const user = userEvent.setup(); |
| 65 | + render(<PromptDefinitionTable prompts={[mockPrompt()]} onSelectPrompt={onSelectPrompt} />); |
| 66 | + |
| 67 | + await user.click(screen.getByRole("button", { name: /copy prompt id/i })); |
| 68 | + expect(onSelectPrompt).not.toHaveBeenCalled(); |
| 69 | + }); |
| 70 | + |
| 71 | + it("selects the row via keyboard, but not when a key fires from an in-row control", async () => { |
| 72 | + const onSelectPrompt = vi.fn(); |
| 73 | + const user = userEvent.setup(); |
| 74 | + render(<PromptDefinitionTable prompts={[mockPrompt()]} onSelectPrompt={onSelectPrompt} />); |
| 75 | + |
| 76 | + // Enter from the copy button must not bubble up into a row selection. |
| 77 | + screen.getByRole("button", { name: /copy prompt id/i }).focus(); |
| 78 | + await user.keyboard("{Enter}"); |
| 79 | + expect(onSelectPrompt).not.toHaveBeenCalled(); |
| 80 | + |
| 81 | + // Enter on the row itself does select it. |
| 82 | + const dataRow = screen.getAllByRole("row")[1]; |
| 83 | + dataRow.focus(); |
| 84 | + await user.keyboard("{Enter}"); |
| 85 | + expect(onSelectPrompt).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it("omits the row overflow menu when neither onEdit nor onDelete is provided", () => { |
| 89 | + render(<PromptDefinitionTable prompts={[mockPrompt()]} onSelectPrompt={vi.fn()} />); |
| 90 | + |
| 91 | + expect(screen.queryByRole("button", { name: /more options/i })).not.toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it("invokes onEdit and onDelete with the row's prompt from the overflow menu", async () => { |
| 95 | + const onEdit = vi.fn(); |
| 96 | + const onDelete = vi.fn(); |
| 97 | + const onSelectPrompt = vi.fn(); |
| 98 | + const user = userEvent.setup(); |
| 99 | + const prompt = mockPrompt(); |
| 100 | + render( |
| 101 | + <PromptDefinitionTable |
| 102 | + prompts={[prompt]} |
| 103 | + onSelectPrompt={onSelectPrompt} |
| 104 | + onEdit={onEdit} |
| 105 | + onDelete={onDelete} |
| 106 | + />, |
| 107 | + ); |
| 108 | + |
| 109 | + await user.click(screen.getByRole("button", { name: /more options for greet_user/i })); |
| 110 | + await user.click(screen.getByRole("menuitem", { name: /^edit$/i })); |
| 111 | + expect(onEdit).toHaveBeenCalledWith(prompt); |
| 112 | + |
| 113 | + await user.click(screen.getByRole("button", { name: /more options for greet_user/i })); |
| 114 | + await user.click(screen.getByRole("menuitem", { name: /^delete$/i })); |
| 115 | + expect(onDelete).toHaveBeenCalledWith(prompt); |
| 116 | + |
| 117 | + // Opening the menu / picking an action must not also select the row. |
| 118 | + expect(onSelectPrompt).not.toHaveBeenCalled(); |
| 119 | + }); |
| 120 | + |
| 121 | + it("renders only the actions provided", async () => { |
| 122 | + const user = userEvent.setup(); |
| 123 | + render( |
| 124 | + <PromptDefinitionTable prompts={[mockPrompt()]} onSelectPrompt={vi.fn()} onEdit={vi.fn()} />, |
| 125 | + ); |
| 126 | + |
| 127 | + await user.click(screen.getByRole("button", { name: /more options for greet_user/i })); |
| 128 | + const menu = screen.getByRole("menu"); |
| 129 | + expect(within(menu).getByRole("menuitem", { name: /^edit$/i })).toBeInTheDocument(); |
| 130 | + expect(within(menu).queryByRole("menuitem", { name: /^delete$/i })).not.toBeInTheDocument(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments