| 1 | import { render } from "ink-testing-library"; |
| 2 | import React from "react"; |
| 3 | import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 4 | |
| 5 | import { ConfigSelector } from "../ConfigSelector.js"; |
| 6 | import { Selector } from "../Selector.js"; |
| 7 | |
| 8 | describe("Ctrl+C behavior", () => { |
| 9 | let mockOnCancel: ReturnType<typeof vi.fn>; |
| 10 | let mockOnSelect: ReturnType<typeof vi.fn>; |
| 11 | let mockProcess: any; |
| 12 | |
| 13 | beforeEach(() => { |
| 14 | mockOnCancel = vi.fn(); |
| 15 | mockOnSelect = vi.fn(); |
| 16 | |
| 17 | // Mock process.exit for testing |
| 18 | mockProcess = { |
| 19 | exit: vi.fn(), |
| 20 | kill: vi.fn(), |
| 21 | }; |
| 22 | vi.stubGlobal("process", mockProcess); |
| 23 | }); |
| 24 | |
| 25 | afterEach(() => { |
| 26 | vi.clearAllMocks(); |
| 27 | vi.unstubAllGlobals(); |
| 28 | }); |
| 29 | |
| 30 | describe("Selector component", () => { |
| 31 | it("calls onCancel when Ctrl+C is pressed", () => { |
| 32 | const options = [ |
| 33 | { id: "1", name: "Option 1" }, |
| 34 | { id: "2", name: "Option 2" }, |
| 35 | ]; |
| 36 | |
| 37 | const { stdin } = render( |
| 38 | <Selector |
| 39 | title="Test" |
| 40 | options={options} |
| 41 | selectedIndex={0} |
| 42 | loading={false} |
| 43 | error={null} |
| 44 | onSelect={mockOnSelect} |
| 45 | onCancel={mockOnCancel} |
| 46 | onNavigate={vi.fn()} |
| 47 | />, |
| 48 | ); |
| 49 | |
| 50 | // Simulate Ctrl+C |
| 51 | stdin.write("\u0003"); |
| 52 | |
| 53 | expect(mockOnCancel).toHaveBeenCalledTimes(1); |
| 54 | expect(mockOnSelect).not.toHaveBeenCalled(); |
| 55 | }); |
| 56 | |
| 57 | it("calls onCancel when Ctrl+C is pressed during loading", () => { |
| 58 | const options: any[] = []; |
| 59 | |
| 60 | const { stdin } = render( |
| 61 | <Selector |
| 62 | title="Loading Test" |
| 63 | options={options} |
| 64 | selectedIndex={0} |
| 65 | loading={true} |
| 66 | error={null} |
| 67 | loadingMessage="Loading..." |
| 68 | onSelect={mockOnSelect} |
| 69 | onCancel={mockOnCancel} |
| 70 | onNavigate={vi.fn()} |
| 71 | />, |
| 72 | ); |
| 73 | |
| 74 | // Simulate Ctrl+C during loading |
| 75 | stdin.write("\u0003"); |
| 76 | |
| 77 | expect(mockOnCancel).toHaveBeenCalledTimes(1); |
| 78 | }); |
| 79 | |
| 80 | it("calls onCancel when Ctrl+C is pressed during error state", () => { |
| 81 | const options: any[] = []; |
| 82 | |
| 83 | const { stdin } = render( |
| 84 | <Selector |
| 85 | title="Error Test" |
| 86 | options={options} |
| 87 | selectedIndex={0} |
| 88 | loading={false} |
| 89 | error="Something went wrong" |
| 90 | onSelect={mockOnSelect} |
| 91 | onCancel={mockOnCancel} |
| 92 | onNavigate={vi.fn()} |
| 93 | />, |
| 94 | ); |
| 95 | |
| 96 | // Simulate Ctrl+C during error |
| 97 | stdin.write("\u0003"); |
| 98 | |
| 99 | expect(mockOnCancel).toHaveBeenCalledTimes(1); |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | describe("ConfigSelector component", () => { |
| 104 | it("calls onCancel when Ctrl+C is pressed", () => { |
| 105 | const { stdin } = render( |
| 106 | <ConfigSelector onSelect={mockOnSelect} onCancel={mockOnCancel} />, |
| 107 | ); |
| 108 | |
| 109 | // Simulate Ctrl+C |
| 110 | stdin.write("\u0003"); |
| 111 | |
| 112 | expect(mockOnCancel).toHaveBeenCalledTimes(1); |
| 113 | }); |
| 114 | }); |
| 115 | }); |