Ghost/ghost/milestones/test/Milestone.test.js
Aileen Booker eeb7546abb
Added handling for initial and skipped Milestones (#16405)
refs
https://www.notion.so/ghost/Marketing-Milestone-email-campaigns-1d2c9dee3cfa4029863edb16092ad5c4?pvs=4

- When milestones will be activated we would send out emails to users
that are way above the achieved milestone, as we didn't record
milestones before
- The plan is to implement a 0 milestone and don't send an email for
achieving those and also add all achieved milestones in the first run
until a first milestone is stored in the DB, then increment from there.
- This change takes care of two cases:
1. Milestones gets enabled and runs initially. We don't want to send
emails unless there's already at least one milestone achieved. For that
we add a 0 milestone helper and add a `initial` reason to the meta
object for the milestone event, so we can choose not to ping Slack and
also disable email sending for all milestones achieved in this initial
run.
2. All achieved milestones will be stored in the DB, even when that
means we skip some. This introduces the `skipped` reason which also
doesn't send emails for the skipped milestones, but will do for
correctly achieved milestones (always the highest one).
- Added handling for slack notifications to not attempt sending when
reason is `skipped` or `initial`
2023-03-13 19:01:11 +02:00

141 lines
4.4 KiB
JavaScript

const assert = require('assert');
const ObjectID = require('bson-objectid');
const Milestone = require('../lib/Milestone');
const validInputARR = {
type: 'arr',
value: 100
};
const validInputMembers = {
type: 'members',
value: 300
};
describe('Milestone', function () {
describe('toJSON', function () {
it('Returns an object with the expected properties', async function () {
const milestone = await Milestone.create(validInputARR);
const actual = Object.keys(milestone.toJSON());
const expected = [
'id',
'name',
'type',
'value',
'currency',
'createdAt',
'emailSentAt'
];
assert.deepEqual(actual, expected);
});
});
describe('create', function () {
it('Will error with invalid inputs', async function () {
const invalidInputs = [
{id: 'Invalid ID provided for Milestone'},
{id: 124},
{value: undefined},
{value: 'Invalid Value'},
{createdAt: 'Invalid Date'},
{emailSentAt: 'Invalid Date'}
];
for (const invalidInput of invalidInputs) {
let errored = false;
try {
await Milestone.create({
...validInputARR,
...invalidInput
});
await Milestone.create({
...validInputMembers,
...invalidInput
});
} catch (err) {
errored = true;
} finally {
if (!errored) {
assert.fail(`Should have errored with invalid input ${JSON.stringify(invalidInput)}`);
}
}
}
});
it('Will not error with valid inputs', async function () {
const validInputs = [
{id: new ObjectID()},
{id: new ObjectID().toString()},
{id: null},
{value: 0},
{value: 25000},
{type: 'something'},
{name: 'testing'},
{name: 'members-10000000'},
{createdAt: new Date()},
{createdAt: '2023-01-01T00:00:00Z'},
{emailSentAt: new Date()},
{emailSentAt: '2023-01-01T00:00:00Z'},
{emailSentAt: null},
{currency: 'usd'},
{currency: null},
{currency: 1234},
{currency: 'not-a-currency'}
];
for (const localValidInput of validInputs) {
await Milestone.create({
...validInputARR,
...localValidInput
});
await Milestone.create({
...validInputMembers,
...localValidInput
});
}
});
it('Will generate a valid name for ARR milestone', async function () {
const milestone = await Milestone.create({
...validInputARR,
value: 0,
type: 'arr',
currency: 'aud'
});
assert(milestone.name === 'arr-0-aud');
});
it('Will generate a valid name for Members milestone', async function () {
const milestone = await Milestone.create({
...validInputMembers,
value: 100,
type: 'members'
});
assert(milestone.name === 'members-100');
});
it('Will create event for new milestone but not for existing one', async function () {
const milestoneOne = await Milestone.create({
...validInputMembers,
value: 500,
type: 'members'
});
assert(milestoneOne.events.length >= 1);
// simulate creating an existing milestone
const id = new ObjectID();
const milestoneTwo = await Milestone.create({
...validInputMembers,
id,
value: 500,
type: 'members'
});
assert(milestoneTwo.events.length === 0);
});
});
});