|
| 1 | +/* Common.pills.test.tsx — render coverage for the small Common.tsx |
| 2 | + * presentational exports (StatusPill, RolePill, ScopePill, Sparkline, |
| 3 | + * Skeleton) and the PromptCard copy paths. */ |
| 4 | + |
| 5 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' |
| 6 | +import { render, screen, waitFor, cleanup } from '@testing-library/react' |
| 7 | +import userEvent from '@testing-library/user-event' |
| 8 | + |
| 9 | +import { |
| 10 | + StatusPill, |
| 11 | + RolePill, |
| 12 | + ScopePill, |
| 13 | + Sparkline, |
| 14 | + Skeleton, |
| 15 | + PromptCard, |
| 16 | +} from './Common' |
| 17 | + |
| 18 | +let writeText: ReturnType<typeof vi.fn> |
| 19 | +beforeEach(() => { |
| 20 | + vi.clearAllMocks() |
| 21 | + writeText = vi.fn().mockResolvedValue(undefined) |
| 22 | + Object.defineProperty(navigator, 'clipboard', { |
| 23 | + value: { writeText }, configurable: true, writable: true, |
| 24 | + }) |
| 25 | +}) |
| 26 | +afterEach(() => cleanup()) |
| 27 | + |
| 28 | +describe('StatusPill', () => { |
| 29 | + it('maps running→healthy and deploying→building', () => { |
| 30 | + const { rerender } = render(<StatusPill status="running" />) |
| 31 | + expect(document.querySelector('.status-pill.healthy')!.textContent).toBe('healthy') |
| 32 | + rerender(<StatusPill status="deploying" />) |
| 33 | + expect(document.querySelector('.status-pill.building')!.textContent).toBe('building') |
| 34 | + }) |
| 35 | + it('renders failed, expired (→stopped) and a fallback', () => { |
| 36 | + const { rerender } = render(<StatusPill status="failed" />) |
| 37 | + expect(document.querySelector('.status-pill.failed')).toBeTruthy() |
| 38 | + rerender(<StatusPill status="expired" />) |
| 39 | + expect(document.querySelector('.status-pill.stopped')!.textContent).toBe('expired') |
| 40 | + rerender(<StatusPill status={'stopped' as any} />) |
| 41 | + expect(document.querySelector('.status-pill.stopped')).toBeTruthy() |
| 42 | + }) |
| 43 | +}) |
| 44 | + |
| 45 | +describe('RolePill', () => { |
| 46 | + it('adds the role modifier class only for owner/admin', () => { |
| 47 | + const { rerender } = render(<RolePill role={'owner' as any} />) |
| 48 | + expect(document.querySelector('.role-pill.owner')).toBeTruthy() |
| 49 | + rerender(<RolePill role={'admin' as any} />) |
| 50 | + expect(document.querySelector('.role-pill.admin')).toBeTruthy() |
| 51 | + rerender(<RolePill role={'member' as any} />) |
| 52 | + expect(document.querySelector('.role-pill')!.className.trim()).toBe('role-pill') |
| 53 | + }) |
| 54 | +}) |
| 55 | + |
| 56 | +describe('ScopePill', () => { |
| 57 | + it('renders write, agent, and read variants', () => { |
| 58 | + const { rerender } = render(<ScopePill scope="write" />) |
| 59 | + expect(screen.getByText(/clickable/)).toBeTruthy() |
| 60 | + rerender(<ScopePill scope="agent" />) |
| 61 | + expect(screen.getByText(/agent surface/)).toBeTruthy() |
| 62 | + rerender(<ScopePill scope="read" />) |
| 63 | + expect(screen.getByText(/mirror/)).toBeTruthy() |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +describe('Sparkline + Skeleton', () => { |
| 68 | + it('renders a polyline for the given points', () => { |
| 69 | + render(<Sparkline points={[1, 5, 3, 8]} />) |
| 70 | + const poly = document.querySelector('svg.sparkline polyline') |
| 71 | + expect(poly).toBeTruthy() |
| 72 | + expect(poly!.getAttribute('points')!.split(' ').length).toBe(4) |
| 73 | + }) |
| 74 | + it('renders a single-point sparkline without dividing by zero', () => { |
| 75 | + render(<Sparkline points={[4]} />) |
| 76 | + expect(document.querySelector('svg.sparkline polyline')).toBeTruthy() |
| 77 | + }) |
| 78 | + it('renders a skeleton with default and custom sizes', () => { |
| 79 | + const { rerender } = render(<Skeleton />) |
| 80 | + expect(document.querySelector('span.skel')).toBeTruthy() |
| 81 | + rerender(<Skeleton width={50} height={8} />) |
| 82 | + expect(document.querySelector('span.skel')).toBeTruthy() |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +describe('PromptCard copy', () => { |
| 87 | + it('copies the fallback prompt when no promptText is given', async () => { |
| 88 | + render(<PromptCard title="Make a DB" prompt="provision pg" method="POST" endpoint="/db/new" hint="hint" />) |
| 89 | + await userEvent.click(screen.getByTestId('copy-prompt')) |
| 90 | + expect(writeText).toHaveBeenCalledWith(expect.stringContaining('/db/new')) |
| 91 | + await waitFor(() => expect(screen.getByTestId('copy-prompt').textContent).toContain('copied')) |
| 92 | + }) |
| 93 | + |
| 94 | + it('copies explicit promptText and the curl command', async () => { |
| 95 | + render(<PromptCard title="Get" prompt="x" promptText="custom prompt" method="GET" endpoint="/healthz" danger />) |
| 96 | + await userEvent.click(screen.getByTestId('copy-prompt')) |
| 97 | + expect(writeText).toHaveBeenLastCalledWith('custom prompt') |
| 98 | + await userEvent.click(screen.getByTestId('copy-curl')) |
| 99 | + expect(writeText).toHaveBeenLastCalledWith(expect.stringContaining('curl -X GET')) |
| 100 | + await waitFor(() => expect(screen.getByTestId('copy-curl').textContent).toContain('copied')) |
| 101 | + }) |
| 102 | + |
| 103 | + it('warns and does not flash when copy fails', async () => { |
| 104 | + writeText.mockRejectedValue(new Error('denied')) |
| 105 | + // Force the execCommand fallback to also fail. |
| 106 | + ;(document as any).execCommand = vi.fn(() => false) |
| 107 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 108 | + render(<PromptCard title="t" prompt="p" method="DELETE" endpoint="/x" />) |
| 109 | + await userEvent.click(screen.getByTestId('copy-curl')) |
| 110 | + await waitFor(() => expect(warn).toHaveBeenCalled()) |
| 111 | + expect(screen.getByTestId('copy-curl').textContent).toContain('copy curl') |
| 112 | + warn.mockRestore() |
| 113 | + }) |
| 114 | +}) |
0 commit comments