0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added event mocking to e2e framework

- Allows mocking lib/common/events - stubs event.emit
- Means we can check that events fired, but their sideeffects won't happen, useful for things like bulk email
- I also reorganised the mock manager file to group related functions together
This commit is contained in:
Hannah Wolfe 2022-03-25 11:44:03 +00:00
parent 15da07f324
commit ccef6dc44e
No known key found for this signature in database
GPG key ID: AB586C3B5AE5C037

View file

@ -12,6 +12,11 @@ let emailCount = 0;
// Mockable services
const mailService = require('../../core/server/services/mail/index');
const labs = require('../../core/shared/labs');
const events = require('../../core/server/lib/common/events');
/**
* Stripe Mocks
*/
const disableStripe = async () => {
// This must be required _after_ startGhost has been called, because the models will
@ -21,10 +26,16 @@ const disableStripe = async () => {
await stripeService.disconnect();
};
const mockStripe = () => {
nock.disableNetConnect();
};
/**
* Email Mocks & Assertions
*/
/**
*
* @param {String|Object} response
* @returns
*/
const mockMail = (response = 'Mail is disabled') => {
mocks.mail = sinon
@ -34,36 +45,6 @@ const mockMail = (response = 'Mail is disabled') => {
return mocks.mail;
};
const mockStripe = () => {
nock.disableNetConnect();
};
const mockLabsEnabled = (flag, alpha = true) => {
// We assume we should enable alpha experiments unless explicitly told not to!
if (!alpha) {
configUtils.set('enableDeveloperExperiments', true);
}
if (!mocks.labs) {
mocks.labs = sinon.stub(labs, 'isSet');
}
mocks.labs.withArgs(flag).returns(true);
};
const mockLabsDisabled = (flag, alpha = true) => {
// We assume we should enable alpha experiments unless explicitly told not to!
if (!alpha) {
configUtils.set('enableDeveloperExperiments', true);
}
if (!mocks.labs) {
mocks.labs = sinon.stub(labs, 'isSet');
}
mocks.labs.withArgs(flag).returns(false);
};
const sentEmailCount = (count) => {
if (!mocks.mail) {
throw new errors.IncorrectUsageError({
@ -97,6 +78,47 @@ const sentEmail = (matchers) => {
});
};
/**
* Events Mocks & Assertions
*/
const mockEvents = () => {
mocks.events = sinon.stub(events, 'emit');
};
const emittedEvent = (name) => {
sinon.assert.calledWith(mocks.events, name);
};
/**
* Labs Mocks
*/
const mockLabsEnabled = (flag, alpha = true) => {
// We assume we should enable alpha experiments unless explicitly told not to!
if (!alpha) {
configUtils.set('enableDeveloperExperiments', true);
}
if (!mocks.labs) {
mocks.labs = sinon.stub(labs, 'isSet');
}
mocks.labs.withArgs(flag).returns(true);
};
const mockLabsDisabled = (flag, alpha = true) => {
// We assume we should enable alpha experiments unless explicitly told not to!
if (!alpha) {
configUtils.set('enableDeveloperExperiments', true);
}
if (!mocks.labs) {
mocks.labs = sinon.stub(labs, 'isSet');
}
mocks.labs.withArgs(flag).returns(false);
};
const restore = () => {
configUtils.restore();
sinon.restore();
@ -107,6 +129,7 @@ const restore = () => {
};
module.exports = {
mockEvents,
mockMail,
disableStripe,
mockStripe,
@ -115,6 +138,7 @@ module.exports = {
restore,
assert: {
sentEmailCount,
sentEmail
sentEmail,
emittedEvent
}
};