@sprucelabs/spruce-event-utils
Version:
Some helpful utilities to speed up working with Mercury! 🚅
35 lines (34 loc) • 2.1 kB
JavaScript
import { assert, assertUtil, errorAssert } from '@sprucelabs/test-utils';
import eventResponseUtil from '../../utilities/eventResponse.utility.js';
const eventAssertUtil = {
assertError(error, expectedCode, expectedPartialOptions) {
var _a, _b, _c;
errorAssert.assertError(error, 'MERCURY_RESPONSE_ERROR');
if (((_b = (_a = error === null || error === void 0 ? void 0 : error.options) === null || _a === void 0 ? void 0 : _a.responseErrors) === null || _b === void 0 ? void 0 : _b.length) > 1) {
assert.fail(`Mercury response has more than 1 error and I was expecting only 1.\n\nReceived:\n\n${assertUtil.stringify((_c = error === null || error === void 0 ? void 0 : error.options) === null || _c === void 0 ? void 0 : _c.responseErrors)}`);
}
errorAssert.assertError(error.options.responseErrors[0], expectedCode, expectedPartialOptions);
return error.options.responseErrors[0];
},
assertIncludesError(error, expectedCode, expectedPartialOptions) {
errorAssert.assertError(error, 'MERCURY_RESPONSE_ERROR');
const codes = [];
for (const err of error.options.responseErrors) {
if (err.options.code === expectedCode) {
errorAssert.assertError(err, expectedCode, expectedPartialOptions);
return;
}
codes.push(err.options.code);
}
assert.fail(`The response does not include an error with the code: ${expectedCode}.\n\nCodes found were:\n\n${codes.join('\n')}`);
},
assertErrorFromResponse(response, expectedCode, expectedPartialOptions) {
const err = assert.doesThrow(() => eventResponseUtil.getFirstResponseOrThrow(response));
return this.assertError(err, expectedCode, expectedPartialOptions);
},
assertResponseIncludesError(response, expectedCode, expectedPartialOptions) {
const err = assert.doesThrow(() => eventResponseUtil.getFirstResponseOrThrow(response));
this.assertIncludesError(err, expectedCode, expectedPartialOptions);
},
};
export default eventAssertUtil;