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:
parent
15da07f324
commit
ccef6dc44e
1 changed files with 57 additions and 33 deletions
|
@ -12,6 +12,11 @@ let emailCount = 0;
|
||||||
// Mockable services
|
// Mockable services
|
||||||
const mailService = require('../../core/server/services/mail/index');
|
const mailService = require('../../core/server/services/mail/index');
|
||||||
const labs = require('../../core/shared/labs');
|
const labs = require('../../core/shared/labs');
|
||||||
|
const events = require('../../core/server/lib/common/events');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stripe Mocks
|
||||||
|
*/
|
||||||
|
|
||||||
const disableStripe = async () => {
|
const disableStripe = async () => {
|
||||||
// This must be required _after_ startGhost has been called, because the models will
|
// This must be required _after_ startGhost has been called, because the models will
|
||||||
|
@ -21,10 +26,16 @@ const disableStripe = async () => {
|
||||||
await stripeService.disconnect();
|
await stripeService.disconnect();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const mockStripe = () => {
|
||||||
|
nock.disableNetConnect();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Email Mocks & Assertions
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param {String|Object} response
|
* @param {String|Object} response
|
||||||
* @returns
|
|
||||||
*/
|
*/
|
||||||
const mockMail = (response = 'Mail is disabled') => {
|
const mockMail = (response = 'Mail is disabled') => {
|
||||||
mocks.mail = sinon
|
mocks.mail = sinon
|
||||||
|
@ -34,36 +45,6 @@ const mockMail = (response = 'Mail is disabled') => {
|
||||||
return mocks.mail;
|
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) => {
|
const sentEmailCount = (count) => {
|
||||||
if (!mocks.mail) {
|
if (!mocks.mail) {
|
||||||
throw new errors.IncorrectUsageError({
|
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 = () => {
|
const restore = () => {
|
||||||
configUtils.restore();
|
configUtils.restore();
|
||||||
sinon.restore();
|
sinon.restore();
|
||||||
|
@ -107,6 +129,7 @@ const restore = () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
mockEvents,
|
||||||
mockMail,
|
mockMail,
|
||||||
disableStripe,
|
disableStripe,
|
||||||
mockStripe,
|
mockStripe,
|
||||||
|
@ -115,6 +138,7 @@ module.exports = {
|
||||||
restore,
|
restore,
|
||||||
assert: {
|
assert: {
|
||||||
sentEmailCount,
|
sentEmailCount,
|
||||||
sentEmail
|
sentEmail,
|
||||||
|
emittedEvent
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue