@sprucelabs/spruce-event-utils
Version:
Some helpful utilities to speed up working with Mercury! 🚅
39 lines (38 loc) • 2.22 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const test_utils_1 = require("@sprucelabs/test-utils");
const eventResponse_utility_1 = __importDefault(require("../../utilities/eventResponse.utility"));
const eventAssertUtil = {
assertError(error, expectedCode, expectedPartialOptions) {
test_utils_1.errorAssert.assertError(error, 'MERCURY_RESPONSE_ERROR');
if (error?.options?.responseErrors?.length > 1) {
test_utils_1.assert.fail(`Mercury response has more than 1 error and I was expecting only 1.\n\nReceived:\n\n${test_utils_1.assertUtil.stringify(error?.options?.responseErrors)}`);
}
test_utils_1.errorAssert.assertError(error.options.responseErrors[0], expectedCode, expectedPartialOptions);
return error.options.responseErrors[0];
},
assertIncludesError(error, expectedCode, expectedPartialOptions) {
test_utils_1.errorAssert.assertError(error, 'MERCURY_RESPONSE_ERROR');
const codes = [];
for (const err of error.options.responseErrors) {
if (err.options.code === expectedCode) {
test_utils_1.errorAssert.assertError(err, expectedCode, expectedPartialOptions);
return;
}
codes.push(err.options.code);
}
test_utils_1.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 = test_utils_1.assert.doesThrow(() => eventResponse_utility_1.default.getFirstResponseOrThrow(response));
return this.assertError(err, expectedCode, expectedPartialOptions);
},
assertResponseIncludesError(response, expectedCode, expectedPartialOptions) {
const err = test_utils_1.assert.doesThrow(() => eventResponse_utility_1.default.getFirstResponseOrThrow(response));
this.assertIncludesError(err, expectedCode, expectedPartialOptions);
},
};
exports.default = eventAssertUtil;