1import spawnAsync from '@expo/spawn-async';
2import editors from 'env-editor';
3
4import { guessEditor, openInEditorAsync } from '../editor';
5
6const asMock = <T extends (...args: any[]) => any>(fn: T): jest.MockedFunction<T> =>
7  fn as jest.MockedFunction<T>;
8
9jest.mock('../../log');
10
11describe(guessEditor, () => {
12  it(`defaults to vscode if the default editor cannot be guessed`, () => {
13    asMock(editors.defaultEditor)
14      .mockClear()
15      .mockImplementationOnce(() => {
16        throw new Error('Could not guess default editor');
17      });
18    guessEditor();
19    expect(editors.getEditor).toBeCalledWith('vscode');
20  });
21});
22
23describe(openInEditorAsync, () => {
24  it(`fails to open in a given editor that does not exist`, async () => {
25    asMock(editors.defaultEditor)
26      .mockClear()
27      .mockReturnValueOnce({
28        name: 'my-editor',
29        binary: 'my-editor-binary',
30        id: 'my-editor-id',
31      } as any);
32    asMock(spawnAsync)
33      .mockClear()
34      .mockImplementationOnce(() => {
35        throw new Error('failed');
36      });
37
38    await expect(openInEditorAsync('/foo/bar')).resolves.toBe(false);
39
40    expect(spawnAsync).toBeCalledWith('my-editor-binary', ['/foo/bar']);
41    expect(spawnAsync).toBeCalledTimes(1);
42  });
43});
44