0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-06 22:40:14 -05:00

Cleaned newsletter e2e tests, improved framework

- attempting to make our framework a one-stop-shop for all requires (except assert)
- using utils for tests instead of lots of requires makes it easier to reason about how our tests interface with our code
- helps with refactoring later, and making sure that tests really do what we expect
This commit is contained in:
Hannah Wolfe 2022-05-06 12:15:12 +01:00
parent 865d57c4be
commit 7618f925e1
No known key found for this signature in database
GPG key ID: AB586C3B5AE5C037
3 changed files with 21 additions and 28 deletions

View file

@ -1,22 +1,11 @@
const DatabaseInfo = require('@tryghost/database-info');
const {any} = require('@tryghost/express-test').snapshot;
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyUuid, anyISODateTime, anyLocationFor} = matchers;
const configUtils = require('../../utils/configUtils');
const uuid = require('uuid');
const urlUtils = require('../../../core/shared/url-utils');
const db = require('../../../core/server/data/db');
const knex = db.knex;
require('should');
const assert = require('assert');
const models = require('../../../core/server/models');
const {agentProvider, mockManager, fixtureManager, configUtils, dbUtils, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyUuid, anyISODateTime, anyLocationFor, anyNumber} = matchers;
const assertMemberRelationCount = async (newsletterId, expectedCount) => {
const newsletter = await models.Newsletter.findOne({id: newsletterId}, {withRelated: 'members'});
const relations = await dbUtils.knex('members_newsletters').where({newsletter_id: newsletterId}).pluck('id');
assert.equal(newsletter.related('members').length, expectedCount);
assert.equal(relations.length, expectedCount);
};
const newsletterSnapshot = {
@ -31,12 +20,11 @@ const newsletterSnapshotWithoutSortOrder = {
uuid: anyUuid,
created_at: anyISODateTime,
updated_at: anyISODateTime,
sort_order: any(Number)
sort_order: anyNumber
};
let agent;
describe('Newsletters API', function () {
let agent;
let mailMocks;
before(async function () {
@ -102,12 +90,11 @@ describe('Newsletters API', function () {
});
it('Can add a newsletter', async function () {
const siteUrl = urlUtils.getSiteUrl();
const siteUrl = configUtils.config.getSiteUrl();
const relativePath = 'content/images/2022/05/cover-image.jpg';
const absolutePath = siteUrl + relativePath;
const transformReadyPath = '__GHOST_URL__/' + relativePath;
const newsletter = {
uuid: uuid.v4(),
name: 'My test newsletter',
sender_name: 'Test',
sender_email: null,
@ -132,7 +119,7 @@ describe('Newsletters API', function () {
})
.expect(({body}) => {
// Should still be absolute
body.newsletters[0].header_image.should.equal(absolutePath);
assert.equal(body.newsletters[0].header_image, absolutePath);
})
.matchHeaderSnapshot({
etag: anyEtag,
@ -142,13 +129,12 @@ describe('Newsletters API', function () {
const id = body2.newsletters[0].id;
// Check with a database query if the header_image is saved correctly with a 'transformReady' path
const [header_image] = await knex('newsletters').where('id', id).pluck('header_image');
header_image.should.equal(transformReadyPath);
const [header_image] = await dbUtils.knex('newsletters').where('id', id).pluck('header_image');
assert.equal(header_image, transformReadyPath);
});
it('Can include members & posts counts when adding a newsletter', async function () {
const newsletter = {
uuid: uuid.v4(),
name: 'My test newsletter 2',
sender_name: 'Test',
sender_email: null,
@ -212,7 +198,6 @@ describe('Newsletters API', function () {
it('Can add a newsletter - with custom sender_email', async function () {
const newsletter = {
uuid: uuid.v4(),
name: 'My test newsletter with custom sender_email',
sender_name: 'Test',
sender_email: 'test@example.com',
@ -250,7 +235,6 @@ describe('Newsletters API', function () {
it('Can add a newsletter - and subscribe existing members', async function () {
const newsletter = {
uuid: uuid.v4(),
name: 'New newsletter with existing members subscribed',
sender_name: 'Test',
sender_email: null,
@ -442,10 +426,11 @@ describe('Newsletters API', function () {
});
it('Can add a newsletter - with custom sender_email and subscribe existing members', async function () {
if (DatabaseInfo.isSQLite(db.knex)) {
if (dbUtils.isSQLite()) {
// This breaks snapshot tests if you don't update snapshot tests on MySQL + make sure this is the last ADD test
return;
}
const newsletter = {
name: 'My test newsletter with custom sender_email and subscribe existing',
sender_name: 'Test',

View file

@ -213,3 +213,5 @@ module.exports.initData = async () => {
await urlServiceUtils.init();
await urlServiceUtils.isFinished();
};
module.exports.knex = db.knex;

View file

@ -279,6 +279,7 @@ module.exports = {
matchers: {
anyString: any(String),
anyArray: any(Array),
anyNumber: any(Number),
anyISODateTime: stringMatching(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000Z/),
anyISODate: stringMatching(/\d{4}-\d{2}-\d{2}/),
anyISODateTimeWithTZ: stringMatching(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.000\+\d{2}:\d{2}/),
@ -290,5 +291,10 @@ module.exports = {
return stringMatching(new RegExp(`https?://.*?/${resource}/[a-f0-9]{24}/`));
},
stringMatching
}
},
// utilities
configUtils: require('./configUtils'),
dbUtils: require('./db-utils'),
urlUtils: require('./urlUtils')
};