|
| 1 | +import { getProjectPathFromArgs } from '../utils'; |
| 2 | +import { getAbsolutePath } from '../getAbsolutePath'; |
| 3 | +import { mocked } from 'jest-mock'; |
| 4 | +import { isFileOnPath } from '../isFileOnPath'; |
| 5 | + |
| 6 | +jest.mock('../utils', () => ({ |
| 7 | + ...jest.requireActual('../utils'), |
| 8 | + getProjectPathFromArgs: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +jest.mock('../getAbsolutePath', () => ({ |
| 12 | + getAbsolutePath: jest.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +const getProjectPathFromArgsMock = mocked(getProjectPathFromArgs); |
| 16 | +const getAbsolutePathMock = mocked(getAbsolutePath); |
| 17 | + |
| 18 | +const projectPathFromArgs = './defined_project_path'; |
| 19 | + |
| 20 | +describe('isFileOnPath', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.resetAllMocks(); |
| 23 | + getProjectPathFromArgsMock.mockReturnValue(projectPathFromArgs); |
| 24 | + getAbsolutePathMock.mockReturnValue('/some_file_path/lib_dir'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('when projectPath argument used, prefer argument value', () => { |
| 28 | + // given |
| 29 | + const projectPath = '/some_path'; |
| 30 | + |
| 31 | + // when |
| 32 | + isFileOnPath({ |
| 33 | + filePath: '/some_file_path/lib_dir/file.ts', |
| 34 | + targetPath: './lib_dir', |
| 35 | + projectPath, |
| 36 | + }); |
| 37 | + |
| 38 | + // then |
| 39 | + expect(getAbsolutePathMock).toHaveBeenCalledTimes(1); |
| 40 | + expect(getAbsolutePathMock).toHaveBeenCalledWith(projectPath, './lib_dir'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('when getProjectPathFromArgs is defined, use defined value', () => { |
| 44 | + //when |
| 45 | + isFileOnPath({ filePath: '/some_file_path/lib_dir/file.ts', targetPath: './lib_dir' }); |
| 46 | + |
| 47 | + // then |
| 48 | + expect(getAbsolutePathMock).toHaveBeenCalledTimes(1); |
| 49 | + expect(getAbsolutePathMock).toHaveBeenCalledWith(projectPathFromArgs, './lib_dir'); |
| 50 | + expect(projectPathFromArgs).not.toEqual(process.cwd()); |
| 51 | + }); |
| 52 | + |
| 53 | + it('when getProjectPathFromArgs is undefined, fallback to current working directory to match', () => { |
| 54 | + // given |
| 55 | + getProjectPathFromArgsMock.mockReturnValue(undefined); |
| 56 | + const cwd = process.cwd(); |
| 57 | + |
| 58 | + // when |
| 59 | + isFileOnPath({ filePath: '/some_file_path/lib_dir/file.ts', targetPath: './lib_dir' }); |
| 60 | + |
| 61 | + // then |
| 62 | + expect(getAbsolutePathMock).toHaveBeenCalledTimes(1); |
| 63 | + expect(getAbsolutePathMock).toHaveBeenCalledWith(cwd, './lib_dir'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments