From ac7e4336b1ad40965483f3c03c43e4b9c64fc9d6 Mon Sep 17 00:00:00 2001 From: Jake Date: Mon, 21 Feb 2022 15:20:29 -0700 Subject: [PATCH] test(Scenarios): make tests more extensible * 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 --- package.json | 3 ++- .../parrot-core/__tests__/utils/matchMock.spec.js | 14 ++++++++++++++ packages/parrot-core/src/utils/matchMock.js | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d79b166c..934be6ba 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,8 @@ "contributors": [ "Jack Cross ", "Nathan Force ", - "Jason Schapiro" + "Jason Schapiro", + "Jacob Franklin " ], "scripts": { "postinstall": "lerna bootstrap", diff --git a/packages/parrot-core/__tests__/utils/matchMock.spec.js b/packages/parrot-core/__tests__/utils/matchMock.spec.js index 89d5a7f3..3889390a 100644 --- a/packages/parrot-core/__tests__/utils/matchMock.spec.js +++ b/packages/parrot-core/__tests__/utils/matchMock.spec.js @@ -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.' + ); + }); }); diff --git a/packages/parrot-core/src/utils/matchMock.js b/packages/parrot-core/src/utils/matchMock.js index 0bf73108..2f426478 100644 --- a/packages/parrot-core/src/utils/matchMock.js +++ b/packages/parrot-core/src/utils/matchMock.js @@ -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') {