mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-23 02:41:50 +03:00
6161f94910
refs: https://github.com/TryGhost/Toolbox/issues/595 We're rolling out new rules around the node assert library, the first of which is enforcing the use of assert/strict. This means we don't need to use the strict version of methods, as the standard version will work that way by default. This caught some gotchas in our existing usage of assert where the lack of strict mode had unexpected results: - Url matching needs to be done on `url.href` seeaa58b354a4
- Null and undefined are not the same thing, there were a few cases of this being confused - Particularly questionable changes in [PostExporter tests](c1a468744b
) tracked [here](https://github.com/TryGhost/Team/issues/3505). - A typo seeeaac9c293a
Moving forward, using assert strict should help us to catch unexpected behaviour, particularly around nulls and undefineds during implementation.
141 lines
4.4 KiB
JavaScript
141 lines
4.4 KiB
JavaScript
const assert = require('assert/strict');
|
|
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);
|
|
});
|
|
});
|
|
});
|