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