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).mockImplementationOnce(() => {
14      throw new Error('Could not guess default editor');
15    });
16    guessEditor();
17    expect(editors.getEditor).toBeCalledWith('vscode');
18  });
19});
20
21describe(openInEditorAsync, () => {
22  it(`fails to open in a given editor that does not exist`, async () => {
23    asMock(editors.defaultEditor).mockReturnValueOnce({
24      name: 'my-editor',
25      binary: 'my-editor-binary',
26      id: 'my-editor-id',
27    } as any);
28    asMock(spawnAsync).mockImplementationOnce(() => {
29      throw new Error('failed');
30    });
31
32    await expect(openInEditorAsync('/foo/bar')).resolves.toBe(false);
33
34    expect(spawnAsync).toBeCalledWith('my-editor-binary', ['/foo/bar']);
35    expect(spawnAsync).toBeCalledTimes(1);
36  });
37});
38