18d307f52SEvan Baconimport spawnAsync from '@expo/spawn-async'; 28d307f52SEvan Baconimport editors from 'env-editor'; 38d307f52SEvan Bacon 4fa5bc561SWill Schurmanimport { asMock } from '../../__tests__/asMock'; 58d307f52SEvan Baconimport { guessEditor, openInEditorAsync } from '../editor'; 68d307f52SEvan Bacon 78d307f52SEvan Baconjest.mock('../../log'); 88d307f52SEvan Bacon 9*873c6c5aSEvan Baconconst original_EXPO_EDITOR = process.env.EXPO_EDITOR; 10*873c6c5aSEvan Bacon 11*873c6c5aSEvan BaconafterAll(() => { 12*873c6c5aSEvan Bacon process.env.EXPO_EDITOR = original_EXPO_EDITOR; 13*873c6c5aSEvan Bacon}); 14*873c6c5aSEvan Bacon 158d307f52SEvan Bacondescribe(guessEditor, () => { 16*873c6c5aSEvan Bacon beforeEach(() => { 17*873c6c5aSEvan Bacon delete process.env.EXPO_EDITOR; 18*873c6c5aSEvan Bacon }); 19*873c6c5aSEvan Bacon 20*873c6c5aSEvan Bacon it(`uses EXPO_EDITOR as the highest priority setting if defined`, () => { 21*873c6c5aSEvan Bacon process.env.EXPO_EDITOR = 'bacon'; 22*873c6c5aSEvan Bacon guessEditor(); 23*873c6c5aSEvan Bacon 24*873c6c5aSEvan Bacon expect(editors.getEditor).toBeCalledWith('bacon'); 25*873c6c5aSEvan Bacon }); 26*873c6c5aSEvan Bacon 278d307f52SEvan Bacon it(`defaults to vscode if the default editor cannot be guessed`, () => { 284c50faceSEvan Bacon asMock(editors.defaultEditor).mockImplementationOnce(() => { 298d307f52SEvan Bacon throw new Error('Could not guess default editor'); 308d307f52SEvan Bacon }); 318d307f52SEvan Bacon guessEditor(); 328d307f52SEvan Bacon expect(editors.getEditor).toBeCalledWith('vscode'); 338d307f52SEvan Bacon }); 348d307f52SEvan Bacon}); 358d307f52SEvan Bacon 368d307f52SEvan Bacondescribe(openInEditorAsync, () => { 378d307f52SEvan Bacon it(`fails to open in a given editor that does not exist`, async () => { 384c50faceSEvan Bacon asMock(editors.defaultEditor).mockReturnValueOnce({ 398d307f52SEvan Bacon name: 'my-editor', 408d307f52SEvan Bacon binary: 'my-editor-binary', 418d307f52SEvan Bacon id: 'my-editor-id', 428d307f52SEvan Bacon } as any); 434c50faceSEvan Bacon asMock(spawnAsync).mockImplementationOnce(() => { 448d307f52SEvan Bacon throw new Error('failed'); 458d307f52SEvan Bacon }); 468d307f52SEvan Bacon 478d307f52SEvan Bacon await expect(openInEditorAsync('/foo/bar')).resolves.toBe(false); 488d307f52SEvan Bacon 498d307f52SEvan Bacon expect(spawnAsync).toBeCalledWith('my-editor-binary', ['/foo/bar']); 508d307f52SEvan Bacon expect(spawnAsync).toBeCalledTimes(1); 518d307f52SEvan Bacon }); 528d307f52SEvan Bacon}); 53