Added Newsletter Events Test to Members (#19653)

refs
https://linear.app/tryghost/issue/ENG-604/🐛-members-events-show-member-subscribed-to-archived-newsletter

- added a tests to avoid a potential regression
This commit is contained in:
Ronald Langeveld 2024-02-05 15:54:10 +02:00 committed by GitHub
parent 612ea2f5f2
commit 58c156001c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 86 additions and 1 deletions

View File

@ -576,7 +576,7 @@ module.exports = class MemberRepository {
.filter(newsletter => newsletter.attributes.status !== 'archived') .filter(newsletter => newsletter.attributes.status !== 'archived')
.map(newsletter => newsletter.id); .map(newsletter => newsletter.id);
const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id); const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id);
// make sure newslettersToAdd does not contain newslettersToIgnore (archived newsletters since that creates false events) // make sure newslettersToAdd does not contain archived newsletters (since that creates false events)
newslettersToAdd = _.differenceWith(_.differenceWith(incomingNewsletterIds, existingNewsletterIds), archivedNewsletters); newslettersToAdd = _.differenceWith(_.differenceWith(incomingNewsletterIds, existingNewsletterIds), archivedNewsletters);
newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds); newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds);
} }

View File

@ -143,6 +143,91 @@ describe('MemberRepository', function () {
}); });
}); });
describe('newsletter subscriptions', function () {
let Member;
let MemberProductEvent;
let productRepository;
let stripeAPIService;
let existingNewsletters;
let MemberSubscribeEvent;
beforeEach(async function () {
sinon.spy();
existingNewsletters = [
{
id: 'newsletter_id_123',
attributes: {
status: 'active'
},
get: sinon.stub().withArgs('status').returns('active')
},
{
id: 'newsletter_id_1234_archive',
attributes: {
status: 'archived'
},
get: sinon.stub().withArgs('status').returns('archived')
}
];
Member = {
findOne: sinon.stub().resolves({
get: sinon.stub().returns('member_id_123'),
related: sinon.stub().withArgs('newsletters').returns({
models: existingNewsletters
}),
toJSON: sinon.stub().returns({})
}),
edit: sinon.stub().resolves({
attributes: {},
_previousAttributes: {}
})
};
stripeAPIService = {
configured: false
};
MemberSubscribeEvent = {
add: sinon.stub().resolves()
};
});
it('Does not create false archived newsletter events', async function () {
const repo = new MemberRepository({
Member,
MemberProductEvent,
productRepository,
stripeAPIService,
MemberSubscribeEventModel: MemberSubscribeEvent,
OfferRedemption: mockOfferRedemption
});
await repo.update({
email: 'test@email.com',
newsletters: [{
id: 'newsletter_id_123'
},
{
id: 'newsletter_id_456'
},
{
id: 'newsletter_id_new'
},
{
id: 'newsletter_id_1234_archive'
}]
},{
transacting: {
executionPromise: Promise.resolve()
},
context: {}
});
MemberSubscribeEvent.add.calledTwice.should.be.true();
});
});
describe('linkSubscription', function (){ describe('linkSubscription', function (){
let Member; let Member;
let MemberPaidSubscriptionEvent; let MemberPaidSubscriptionEvent;