Skip to content

Commit

Permalink
combined tests with it.each
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnoo committed Feb 21, 2024
1 parent 948aabe commit e97702b
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions src/libs/handler-resolver.test.ts
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);
});
});

0 comments on commit e97702b

Please sign in to comment.