Skip to content

Commit

Permalink
test(Scenarios): make tests more extensible
Browse files Browse the repository at this point in the history
* test(Scenarios): make tests more extensible

* refactor(Scenarios): make prop-types more explicit

* fix(matchMock): ensure param type is array

* chore(package): add Jacob Franklin to contributors

Co-authored-by: Jonny Adshead <[email protected]>
  • Loading branch information
amexjake and JAdshead authored Feb 21, 2022
1 parent 659f103 commit ac7e433
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"contributors": [
"Jack Cross <[email protected]>",
"Nathan Force <[email protected]>",
"Jason Schapiro"
"Jason Schapiro",
"Jacob Franklin <[email protected]>"
],
"scripts": {
"postinstall": "lerna bootstrap",
Expand Down
14 changes: 14 additions & 0 deletions packages/parrot-core/__tests__/utils/matchMock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,18 @@ describe('matchMock', () => {
const req = { path: '/squawk', 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(
'mocks is not an array as expected. What was passed: [object Object]'
);
});

it('throws when mocks is an empty array', () => {
const mocks = [];
expect(() => matchMock({}, {}, mocks)).toThrow(
'mocks is empty, and likely none are defined for the current scenario.'
);
});
});
9 changes: 9 additions & 0 deletions packages/parrot-core/src/utils/matchMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ function match(normalizedRequest) {

export default function matchMock(normalizedRequest, platformRequest, mocks) {
let matchedMock;

if (!Array.isArray(mocks)) {
throw new TypeError(`mocks is not an array as expected. What was passed: ${mocks}`);
}

if (mocks.length === 0) {
throw new TypeError('mocks is empty, and likely none are defined for the current scenario.');
}

for (let index = 0; index < mocks.length; index += 1) {
const mock = mocks[index];
if (typeof mock === 'function') {
Expand Down

0 comments on commit ac7e433

Please sign in to comment.