|
| 1 | +import { when } from 'jest-when'; |
| 2 | +import { determineEditor } from './editor'; |
| 3 | +import * as envModule from './env'; |
| 4 | +import * as miscUtils from './misc-utils'; |
| 5 | + |
| 6 | +jest.mock('./env'); |
| 7 | +jest.mock('./misc-utils'); |
| 8 | + |
| 9 | +describe('editor', () => { |
| 10 | + describe('determineEditor', () => { |
| 11 | + it('returns information about the editor from EDITOR if it resolves to an executable', async () => { |
| 12 | + jest |
| 13 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 14 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 15 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 16 | + .calledWith('editor') |
| 17 | + .mockResolvedValue('/path/to/resolved-editor'); |
| 18 | + |
| 19 | + expect(await determineEditor()).toStrictEqual({ |
| 20 | + path: '/path/to/resolved-editor', |
| 21 | + args: [], |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('falls back to VSCode if it exists and if EDITOR does not point to an executable', async () => { |
| 26 | + jest |
| 27 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 28 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 29 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 30 | + .calledWith('editor') |
| 31 | + .mockResolvedValue(null) |
| 32 | + .calledWith('code') |
| 33 | + .mockResolvedValue('/path/to/code'); |
| 34 | + |
| 35 | + expect(await determineEditor()).toStrictEqual({ |
| 36 | + path: '/path/to/code', |
| 37 | + args: ['--wait'], |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns null if resolving EDITOR returns null and resolving VSCode returns null', async () => { |
| 42 | + jest |
| 43 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 44 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 45 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 46 | + .calledWith('editor') |
| 47 | + .mockResolvedValue(null) |
| 48 | + .calledWith('code') |
| 49 | + .mockResolvedValue(null); |
| 50 | + |
| 51 | + expect(await determineEditor()).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns null if resolving EDITOR returns null and resolving VSCode throws', async () => { |
| 55 | + jest |
| 56 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 57 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 58 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 59 | + .calledWith('editor') |
| 60 | + .mockResolvedValue(null) |
| 61 | + .calledWith('code') |
| 62 | + .mockRejectedValue(new Error('some error')); |
| 63 | + |
| 64 | + expect(await determineEditor()).toBeNull(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns null if resolving EDITOR throws and resolving VSCode returns null', async () => { |
| 68 | + jest |
| 69 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 70 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 71 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 72 | + .calledWith('editor') |
| 73 | + .mockRejectedValue(new Error('some error')) |
| 74 | + .calledWith('code') |
| 75 | + .mockResolvedValue(null); |
| 76 | + |
| 77 | + expect(await determineEditor()).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns null if resolving EDITOR throws and resolving VSCode throws', async () => { |
| 81 | + jest |
| 82 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 83 | + .mockReturnValue({ EDITOR: 'editor', TODAY: undefined }); |
| 84 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 85 | + .calledWith('editor') |
| 86 | + .mockRejectedValue(new Error('some error')) |
| 87 | + .calledWith('code') |
| 88 | + .mockRejectedValue(new Error('some error')); |
| 89 | + |
| 90 | + expect(await determineEditor()).toBeNull(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns null if EDITOR is unset and resolving VSCode returns null', async () => { |
| 94 | + jest |
| 95 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 96 | + .mockReturnValue({ EDITOR: undefined, TODAY: undefined }); |
| 97 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 98 | + .calledWith('code') |
| 99 | + .mockResolvedValue(null); |
| 100 | + |
| 101 | + expect(await determineEditor()).toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns null if EDITOR is unset and resolving VSCode throws', async () => { |
| 105 | + jest |
| 106 | + .spyOn(envModule, 'getEnvironmentVariables') |
| 107 | + .mockReturnValue({ EDITOR: undefined, TODAY: undefined }); |
| 108 | + when(jest.spyOn(miscUtils, 'resolveExecutable')) |
| 109 | + .calledWith('code') |
| 110 | + .mockRejectedValue(new Error('some error')); |
| 111 | + |
| 112 | + expect(await determineEditor()).toBeNull(); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments