-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,63 @@ | ||
import { handlerPath } from './handler-resolver'; | ||
|
||
describe('handlerPath', () => { | ||
beforeEach(() => { | ||
jest.spyOn(process, 'cwd').mockReturnValue('/User/test/feedback-api'); | ||
}); | ||
|
||
describe('handlerPath success cases', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should return a relative UNIX-style path from a UNIX-style absolute path', () => { | ||
const inputPath = '/User/test/feedback-api/summary/handler.ts'; | ||
expect(handlerPath(inputPath)).toBe('summary/handler.ts'); | ||
}); | ||
|
||
it('should return a relative UNIX-style path from a Windows-style absolute path', () => { | ||
jest.spyOn(process, 'cwd').mockReturnValue('C:\\User\\test\\feedback-api'); | ||
const inputPath = 'C:\\User\\test\\feedback-api\\src\\handler.js'; | ||
expect(handlerPath(inputPath)).toBe('src/handler.js'); | ||
const testCases = [ | ||
{ | ||
name: 'should return a relative UNIX-style path from a UNIX-style absolute path', | ||
cwd: '/User/test/feedback-api', | ||
inputPath: '/User/test/feedback-api/summary/handler.ts', | ||
expected: 'summary/handler.ts' | ||
}, | ||
{ | ||
name: 'should return a relative UNIX-style path from a Windows-style absolute path', | ||
cwd: 'C:\\User\\test\\feedback-api', | ||
inputPath: 'C:\\User\\test\\feedback-api\\src\\handler.js', | ||
expected: 'src/handler.js' | ||
}, | ||
{ | ||
name: 'should return an empty string if the input path is the same as cwd path', | ||
cwd: '/User/test/feedback-api', | ||
inputPath: '/User/test/feedback-api', | ||
expected: '' | ||
}, | ||
{ | ||
name: 'should return an empty string if cwd path is an empty string', | ||
cwd: '', | ||
inputPath: 'C:\\User\\test\\feedback-api\\src\\handler.js', | ||
expected: '' | ||
} | ||
]; | ||
it.each(testCases)('$name', ({ cwd, inputPath, expected }) => { | ||
jest.spyOn(process, 'cwd').mockReturnValue(cwd); | ||
expect(handlerPath(inputPath)).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('should return an empty string if the input path is the same as cwd path', () => { | ||
const inputPath = '/User/test/feedback-api'; | ||
expect(handlerPath(inputPath)).toBe(''); | ||
describe('handlerPath error cases', () => { | ||
beforeEach(() => { | ||
jest.spyOn(process, 'cwd').mockReturnValue('/User/test/feedback-api'); | ||
}); | ||
|
||
it('should return an empty string if cwd path is an empty string', () => { | ||
jest.spyOn(process, 'cwd').mockReturnValue(''); | ||
const inputPath = 'C:\\User\\test\\feedback-api\\src\\handler.js'; | ||
expect(handlerPath(inputPath)).toBe(''); | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should throw an error if the context path and cwd path do not share the same starting path', () => { | ||
const inputPath = '/different/path/not/matching/cwd/handler.ts'; | ||
expect(() => handlerPath(inputPath)).toThrow(TypeError); | ||
}); | ||
const errorTestCases = [ | ||
{ | ||
name: 'should throw an error if the context path and cwd path do not share the same starting path', | ||
inputPath: '/different/path/not/matching/cwd/handler.ts' | ||
}, | ||
{ | ||
name: 'should throw an error if the context path is an empty string', | ||
inputPath: '' | ||
} | ||
]; | ||
|
||
it('should throw an error if the context path is an empty string', () => { | ||
const inputPath = ''; | ||
it.each(errorTestCases)('$name', ({ inputPath }) => { | ||
expect(() => handlerPath(inputPath)).toThrow(TypeError); | ||
}); | ||
}); |