-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandMenu.test.tsx
More file actions
78 lines (66 loc) · 2.09 KB
/
CommandMenu.test.tsx
File metadata and controls
78 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Text } from 'ink';
import { render } from 'ink-testing-library';
interface MockSelectPromptProps {
highlightText: string;
onChange: (value: string) => void;
options: { label: string; value: string }[];
}
const { mockSelectPrompt } = vi.hoisted(() => ({
mockSelectPrompt: vi.fn<(props: MockSelectPromptProps) => void>(),
}));
vi.mock('../SelectPrompt', () => ({
SelectPrompt: (props: MockSelectPromptProps) => {
mockSelectPrompt(props);
return (
<>
{props.options.map(({ label, value }) => (
<Text key={value}>{label}</Text>
))}
</>
);
},
}));
import { CommandMenu } from './CommandMenu';
describe('CommandMenu', () => {
beforeEach(() => {
mockSelectPrompt.mockReset();
});
it('returns null when input does not start with a slash', () => {
const onSubmit = vi.fn();
const { lastFrame } = render(
<CommandMenu input="hello" onSubmit={onSubmit} />,
);
expect(lastFrame()).toBe('');
expect(mockSelectPrompt).not.toHaveBeenCalled();
});
it('returns null when no commands match the slash input', () => {
const onSubmit = vi.fn();
const { lastFrame } = render(
<CommandMenu input="/x" onSubmit={onSubmit} />,
);
expect(lastFrame()).toBe('');
expect(mockSelectPrompt).not.toHaveBeenCalled();
});
it('renders matching commands and forwards selection', () => {
const onSubmit = vi.fn();
const { lastFrame } = render(
<CommandMenu input="/m" onSubmit={onSubmit} />,
);
expect(lastFrame()).toContain('/model - switch the model');
expect(lastFrame()).not.toContain('/clear - clear the current session');
expect(mockSelectPrompt).toHaveBeenCalledTimes(1);
const [firstCall] = mockSelectPrompt.mock.calls;
expect(firstCall).toBeDefined();
const [props] = firstCall;
expect(props.highlightText).toBe('/m');
expect(props.options).toEqual([
{
label: '/model - switch the model',
value: '/model',
},
]);
const { onChange } = props;
onChange('/model');
expect(onSubmit).toHaveBeenCalledWith('/model');
});
});