|
| 1 | +import { createElement } from "react"; |
| 2 | +import { act, create } from "react-test-renderer"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + TerminalStatusBanner, |
| 6 | + type TerminalStatusBannerProps, |
| 7 | +} from "./TerminalStatusBanner"; |
| 8 | + |
| 9 | +function render(props: TerminalStatusBannerProps) { |
| 10 | + let renderer!: ReturnType<typeof create>; |
| 11 | + act(() => { |
| 12 | + renderer = create(createElement(TerminalStatusBanner, props)); |
| 13 | + }); |
| 14 | + return renderer; |
| 15 | +} |
| 16 | + |
| 17 | +function renderedText(renderer: ReturnType<typeof create>): string { |
| 18 | + const acc: string[] = []; |
| 19 | + const walk = (node: unknown) => { |
| 20 | + if (typeof node === "string") { |
| 21 | + acc.push(node); |
| 22 | + } else if (Array.isArray(node)) { |
| 23 | + node.forEach(walk); |
| 24 | + } else if (node && typeof node === "object" && "children" in node) { |
| 25 | + walk((node as { children: unknown }).children); |
| 26 | + } |
| 27 | + }; |
| 28 | + walk(renderer.toJSON()); |
| 29 | + return acc.join(" "); |
| 30 | +} |
| 31 | + |
| 32 | +describe("TerminalStatusBanner", () => { |
| 33 | + it.each([ |
| 34 | + { terminalStatus: "completed", label: "Run completed", button: "Continue" }, |
| 35 | + { terminalStatus: "failed", label: "Run failed", button: "Retry" }, |
| 36 | + { terminalStatus: "stopped", label: "Run stopped", button: "Continue" }, |
| 37 | + ] as const)( |
| 38 | + "shows $label with a $button action for a $terminalStatus run", |
| 39 | + ({ terminalStatus, label, button }) => { |
| 40 | + const text = renderedText(render({ terminalStatus, onRetry: vi.fn() })); |
| 41 | + expect(text).toContain(label); |
| 42 | + expect(text).toContain(button); |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + it("does not label a stopped run as failed", () => { |
| 47 | + const text = renderedText(render({ terminalStatus: "stopped" })); |
| 48 | + expect(text).not.toContain("Run failed"); |
| 49 | + expect(text).not.toContain("Retry"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("fires onRetry when the action is pressed", () => { |
| 53 | + const onRetry = vi.fn(); |
| 54 | + const renderer = render({ terminalStatus: "stopped", onRetry }); |
| 55 | + const pressable = renderer.root.findAll( |
| 56 | + (node) => node.props.onPress === onRetry, |
| 57 | + )[0]; |
| 58 | + act(() => { |
| 59 | + pressable.props.onPress(); |
| 60 | + }); |
| 61 | + expect(onRetry).toHaveBeenCalledTimes(1); |
| 62 | + }); |
| 63 | +}); |
0 commit comments