-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
editor.test.mts
146 lines (117 loc) · 4.6 KB
/
editor.test.mts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { describe, it, expect, afterEach, vi } from 'vitest';
import { render } from '@inquirer/testing';
import { editAsync } from 'external-editor';
import editor from './src/index.mjs';
vi.mock('external-editor');
afterEach(() => {
vi.mocked(editAsync).mockClear();
});
async function editorAction(error: undefined | Error, value?: string) {
const { lastCall } = vi.mocked(editAsync).mock;
if (!lastCall) throw new Error("editor wasn't open");
// Bugfix: The callback error value is nullable.
const editCallback = lastCall[1] as (error: undefined | Error, value: string) => void;
await editCallback(error, value ?? '');
}
describe('editor prompt', () => {
it('open editor after pressing enter', async () => {
const { answer, events, getScreen } = await render(editor, {
message: 'Add a description',
});
expect(getScreen()).toMatchInlineSnapshot(
`"? Add a description Press <enter> to launch your preferred editor."`,
);
expect(editAsync).not.toHaveBeenCalled();
events.keypress('enter');
expect(editAsync).toHaveBeenCalledWith('', expect.any(Function), { postfix: '.txt' });
await editorAction(undefined, 'value from editor');
await expect(answer).resolves.toEqual('value from editor');
expect(getScreen()).toMatchInlineSnapshot(`"? Add a description"`);
});
it('open editor immediately', async () => {
const { answer, getScreen } = await render(editor, {
message: 'Add a description',
waitForUseInput: false,
});
expect(editAsync).toHaveBeenCalledWith('', expect.any(Function), { postfix: '.txt' });
await editorAction(undefined, 'value from editor');
await expect(answer).resolves.toEqual('value from editor');
expect(getScreen()).toMatchInlineSnapshot(`"? Add a description"`);
});
it('allow setting a default value & postfix', async () => {
const { answer, events, getScreen } = await render(editor, {
message: 'Add a description',
default: 'default description',
postfix: '.md',
});
expect(editAsync).not.toHaveBeenCalled();
events.keypress('enter');
expect(editAsync).toHaveBeenCalledWith('default description', expect.any(Function), {
postfix: '.md',
});
await editorAction(undefined, 'value from editor');
await expect(answer).resolves.toEqual('value from editor');
expect(getScreen()).toMatchInlineSnapshot(`"? Add a description"`);
});
it('handles validation', async () => {
const { answer, events, getScreen } = await render(editor, {
message: 'Add a description',
validate(value) {
switch (value) {
case '1': {
return true;
}
case '2': {
return '"2" is not an allowed value';
}
default: {
return false;
}
}
},
});
expect(editAsync).not.toHaveBeenCalled();
events.keypress('enter');
// Test default error message
const editPromise = editorAction(undefined, '3');
events.type('foo'); // Ignored events while validation runs
await editPromise;
expect(getScreen()).toMatchInlineSnapshot(`
"? Add a description Press <enter> to launch your preferred editor.
> You must provide a valid value"
`);
expect(editAsync).toHaveBeenCalledOnce();
events.keypress('enter');
expect(editAsync).toHaveBeenCalledTimes(2);
// Test user defined error message
await editorAction(undefined, '2');
expect(getScreen()).toMatchInlineSnapshot(`
"? Add a description Press <enter> to launch your preferred editor.
> "2" is not an allowed value"
`);
events.keypress('enter');
expect(editAsync).toHaveBeenCalledTimes(3);
await editorAction(undefined, '1');
await expect(answer).resolves.toEqual('1');
expect(getScreen()).toMatchInlineSnapshot(`"? Add a description"`);
});
it('surfaces external-editor errors', async () => {
const { answer, events, getScreen } = await render(editor, {
message: 'Add a description',
});
expect(editAsync).not.toHaveBeenCalled();
events.keypress('enter');
await editorAction(new Error('$EDITOR failed!'), '');
expect(getScreen()).toMatchInlineSnapshot(`
"? Add a description Press <enter> to launch your preferred editor.
> Error: $EDITOR failed!"
`);
expect(editAsync).toHaveBeenCalledOnce();
events.keypress('enter');
expect(editAsync).toHaveBeenCalledTimes(2);
// Test user defined error message
await editorAction(undefined, 'new value');
await expect(answer).resolves.toEqual('new value');
expect(getScreen()).toMatchInlineSnapshot(`"? Add a description"`);
});
});