Added E2E tests for comment CTA

fixes https://github.com/TryGhost/Team/issues/2727

Adds some E2E tests and snapshots for comment CTA in all possible situations.
This commit is contained in:
Simon Backx 2023-03-17 11:00:08 +01:00
parent ddc6d60927
commit 005b5f20fb
2 changed files with 3217 additions and 3 deletions

File diff suppressed because one or more lines are too long

View File

@ -13,6 +13,7 @@ const {settingsCache} = require('../../../../core/server/services/settings-helpe
const DomainEvents = require('@tryghost/domain-events'); const DomainEvents = require('@tryghost/domain-events');
const emailService = require('../../../../core/server/services/email-service'); const emailService = require('../../../../core/server/services/email-service');
const should = require('should'); const should = require('should');
const {mockSetting} = require('../../../utils/e2e-framework-mock-manager');
const mobileDocExample = '{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"Hello world"]]]],"ghostVersion":"4.0"}'; const mobileDocExample = '{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"Hello world"]]]],"ghostVersion":"4.0"}';
const mobileDocWithPaywall = '{"version":"0.3.1","markups":[],"atoms":[],"cards":[["paywall",{}]],"sections":[[1,"p",[[0,[],0,"Free content"]]],[10,0],[1,"p",[[0,[],0,"Members content"]]]]}'; const mobileDocWithPaywall = '{"version":"0.3.1","markups":[],"atoms":[],"cards":[["paywall",{}]],"sections":[[1,"p",[[0,[],0,"Free content"]]],[10,0],[1,"p",[[0,[],0,"Members content"]]]]}';
@ -25,6 +26,7 @@ const mobileDocWithReplacements = '{"version":"0.3.1","atoms":[],"cards":[["emai
let agent; let agent;
let stubbedSend; let stubbedSend;
let frontendAgent; let frontendAgent;
let lastEmailModel;
function sortBatches(a, b) { function sortBatches(a, b) {
const aId = a.get('provider_id'); const aId = a.get('provider_id');
@ -93,6 +95,8 @@ async function sendEmail(settings, email_recipient_filter) {
await emailModel.refresh(); await emailModel.refresh();
assert.equal(emailModel.get('status'), 'submitted'); assert.equal(emailModel.get('status'), 'submitted');
lastEmailModel = emailModel;
// Get the email that was sent // Get the email that was sent
return {emailModel, ...(await getLastEmail())}; return {emailModel, ...(await getLastEmail())};
} }
@ -120,6 +124,7 @@ async function getLastEmail() {
} }
return { return {
emailModel: lastEmailModel,
...messageData, ...messageData,
html, html,
plaintext, plaintext,
@ -140,7 +145,7 @@ function testCleanedSnapshot(html, ignoreReplacements) {
async function lastEmailMatchSnapshot() { async function lastEmailMatchSnapshot() {
const lastEmail = await getLastEmail(); const lastEmail = await getLastEmail();
const defaultNewsletter = await getDefaultNewsletter(); const defaultNewsletter = await lastEmail.emailModel.getLazyRelation('newsletter');
const linkRegexp = /http:\/\/127\.0\.0\.1:2369\/r\/\w+/g; const linkRegexp = /http:\/\/127\.0\.0\.1:2369\/r\/\w+/g;
const ignoreReplacements = [ const ignoreReplacements = [
@ -149,8 +154,16 @@ async function lastEmailMatchSnapshot() {
replacement: 'requested-newsletter-uuid' replacement: 'requested-newsletter-uuid'
}, },
{ {
match: lastEmail.recipientData.uuid, match: lastEmail.emailModel.get('post_id'),
replacement: 'member-uuid' replacement: 'post-id'
},
{
match: (await lastEmail.emailModel.getLazyRelation('post')).get('uuid'),
replacement: 'post-uuid'
},
{
match: linkRegexp,
replacement: 'http://127.0.0.1:2369/r/xxxxxx'
}, },
{ {
match: linkRegexp, match: linkRegexp,
@ -158,6 +171,21 @@ async function lastEmailMatchSnapshot() {
} }
]; ];
if (lastEmail.recipientData.uuid) {
ignoreReplacements.push({
match: lastEmail.recipientData.uuid,
replacement: 'member-uuid'
});
} else {
// Sometimes uuid is not used if link tracking is disabled
// Need to replace unsubscribe url instead (uuid is missing but it is inside the usubscribe url, causing snapshot updates)
// Need to use unshift to make replacement work before newsletter uuid
ignoreReplacements.unshift({
match: lastEmail.recipientData.unsubscribe_url,
replacement: 'unsubscribe_url'
});
}
testCleanedSnapshot(lastEmail.html, ignoreReplacements); testCleanedSnapshot(lastEmail.html, ignoreReplacements);
testCleanedSnapshot(lastEmail.plaintext, ignoreReplacements); testCleanedSnapshot(lastEmail.plaintext, ignoreReplacements);
} }
@ -914,5 +942,97 @@ describe('Batch sending tests', function () {
assert.match(plaintext2, /This is a test post title/); assert.match(plaintext2, /This is a test post title/);
await lastEmailMatchSnapshot(); await lastEmailMatchSnapshot();
}); });
it('Shows 2 comment buttons for published posts without feedback enabled', async function () {
mockSetting('comments_enabled', 'all');
mockSetting('email_track_clicks', false); // Disable link replacement for this test
const defaultNewsletter = await getDefaultNewsletter();
assert(defaultNewsletter.get('show_comment_cta'), 'show_comment_cta should be true for this test');
assert(!defaultNewsletter.get('feedback_enabled'), 'feedback_enabled should be off for this test');
const {html} = await sendEmail({
title: 'This is a test post title',
mobiledoc: mobileDocExample
});
// Currently the link is not present in plaintext version (because no text)
assert.equal(html.match(/#ghost-comments/g).length, 2, 'Every email should have two buttons to comments');
await lastEmailMatchSnapshot();
});
it('Shows 2 comment buttons for published posts with feedback enabled', async function () {
mockSetting('comments_enabled', 'all');
mockSetting('email_track_clicks', false); // Disable link replacement for this test
const defaultNewsletter = await getDefaultNewsletter();
assert(defaultNewsletter.get('show_comment_cta'), 'show_comment_cta should be true for this test');
await models.Newsletter.edit({feedback_enabled: true}, {id: defaultNewsletter.id});
const {html} = await sendEmail({
title: 'This is a test post title',
mobiledoc: mobileDocExample
});
// Currently the link is not present in plaintext version (because no text)
assert.equal(html.match(/#ghost-comments/g).length, 2, 'Every email should have two buttons to comments');
await lastEmailMatchSnapshot();
// undo
await models.Newsletter.edit({feedback_enabled: false}, {id: defaultNewsletter.id});
});
it('Hides comments button for email only posts', async function () {
mockSetting('comments_enabled', 'all');
mockSetting('email_track_clicks', false); // Disable link replacement for this test
const defaultNewsletter = await getDefaultNewsletter();
assert(defaultNewsletter.get('show_comment_cta'), 'show_comment_cta should be true for this test');
const {html} = await sendEmail({
title: 'This is a test post title',
mobiledoc: mobileDocExample,
email_only: true
});
// Check does not contain post title section
assert.doesNotMatch(html, /#ghost-comments/);
await lastEmailMatchSnapshot();
});
it('Hides comments button if comments disabled', async function () {
mockSetting('comments_enabled', 'off');
mockSetting('email_track_clicks', false); // Disable link replacement for this test
const defaultNewsletter = await getDefaultNewsletter();
assert(defaultNewsletter.get('show_comment_cta'), 'show_comment_cta should be true for this test');
const {html} = await sendEmail({
title: 'This is a test post title',
mobiledoc: mobileDocExample
});
assert.doesNotMatch(html, /#ghost-comments/);
await lastEmailMatchSnapshot();
});
it('Hides comments button if disabled in newsletter', async function () {
mockSetting('comments_enabled', 'all');
mockSetting('email_track_clicks', false); // Disable link replacement for this test
const defaultNewsletter = await getDefaultNewsletter();
await models.Newsletter.edit({show_comment_cta: false}, {id: defaultNewsletter.id});
const {html} = await sendEmail({
title: 'This is a test post title',
mobiledoc: mobileDocExample
});
assert.doesNotMatch(html, /#ghost-comments/);
await lastEmailMatchSnapshot();
// undo
await models.Newsletter.edit({show_comment_cta: true}, {id: defaultNewsletter.id});
});
}); });
}); });