Skip to content

Commit

Permalink
fix(matchMock): handle regex mock paths directly
Browse files Browse the repository at this point in the history
  • Loading branch information
smackfu committed Nov 27, 2024
1 parent 6882800 commit 06b5f75
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/parrot-core/__tests__/utils/matchMock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ describe('matchMock', () => {
expect(matchMock(req, {}, mocks)).toEqual(mocks[0]);
});

it('matches mock object when path variable', () => {
const mocks = [
{ request: { path: '/squawk/:id', headers: 'ahoy', 'Keep-Alive': 'timeout=5' } },
];
const req = { path: '/squawk/123', headers: 'ahoy', 'Keep-Alive': 'timeout=5' };
expect(matchMock(req, {}, mocks)).toEqual(mocks[0]);
});

it('matches mock object when regex path', () => {
const mocks = [
{ request: { path: /^\/squawk\/\d?/, headers: 'ahoy', 'Keep-Alive': 'timeout=5' } },
];
const req = { path: '/squawk/123', headers: 'ahoy', 'Keep-Alive': 'timeout=5' };
expect(matchMock(req, {}, mocks)).toEqual(mocks[0]);
});

it('throws when mocks is not an array', () => {
const mocks = {};
expect(() => matchMock({}, {}, mocks)).toThrow(
Expand Down
3 changes: 3 additions & 0 deletions packages/parrot-core/src/utils/matchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function matchRequest(normalizedRequest) {
return request =>
Object.keys(request).every(property => {
if (property === 'path') {
if (request.path instanceof RegExp) {
return request.path.test(normalizedRequest.path);
}
const matchRoute = match(request.path);
const result = matchRoute(normalizedRequest.path);
return result !== false;
Expand Down

0 comments on commit 06b5f75

Please sign in to comment.