|
| 1 | +import Organisation, { EMAIL_PROCESSING, EMAIL_NOOP } from 'lib/models/organisation'; |
| 2 | +import moment from 'moment'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { |
| 5 | + WEEK_BEFORE_NOTIFICATION, |
| 6 | + EXPIRATION_NOTIFICATION |
| 7 | +} from 'lib/constants/expirationNotifications'; |
| 8 | +import expirationNotificationEmails from './expirationNotificationEmails'; |
| 9 | + |
| 10 | +describe('expirationNotificationEmails', () => { |
| 11 | + beforeEach(async () => { |
| 12 | + await Organisation.remove({}); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should send weekBefore email', async () => { |
| 16 | + const organisation = await Organisation.create({ |
| 17 | + expiration: moment().add(1, 'day').toDate() |
| 18 | + }); |
| 19 | + |
| 20 | + let publishCalled = false; |
| 21 | + await expirationNotificationEmails({ |
| 22 | + publish: ({ |
| 23 | + payload |
| 24 | + }) => { |
| 25 | + publishCalled = true; |
| 26 | + expect(payload.organisationId).to.equal(organisation._id.toString()); |
| 27 | + expect(payload.emailType).to.equal(WEEK_BEFORE_NOTIFICATION); |
| 28 | + }, |
| 29 | + dontExit: true |
| 30 | + }); |
| 31 | + |
| 32 | + expect(publishCalled).to.equal(true); |
| 33 | + |
| 34 | + const newOrg = await Organisation.findById(organisation._id); |
| 35 | + expect(newOrg.expirationNotifications.weekBeforeNotificationSent).to.equal(EMAIL_PROCESSING); |
| 36 | + expect(newOrg.expirationNotifications.expirationNotificationSent).to.equal(EMAIL_NOOP); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should not send any email', async () => { |
| 40 | + const organisation = await Organisation.create({ |
| 41 | + expiration: moment().add(9, 'day').toDate() |
| 42 | + }); |
| 43 | + |
| 44 | + let publishCalled = false; |
| 45 | + await expirationNotificationEmails({ |
| 46 | + publish: () => { |
| 47 | + publishCalled = true; |
| 48 | + }, |
| 49 | + dontExit: true |
| 50 | + }); |
| 51 | + |
| 52 | + expect(publishCalled).to.equal(false); |
| 53 | + |
| 54 | + const newOrg = await Organisation.findById(organisation._id); |
| 55 | + expect(newOrg.expirationNotifications.weekBeforeNotificationSent).to.equal(EMAIL_NOOP); |
| 56 | + expect(newOrg.expirationNotifications.expirationNotificationSent).to.equal(EMAIL_NOOP); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should send expiration email', async () => { |
| 60 | + const organisation = await Organisation.create({ |
| 61 | + expiration: moment().subtract(1, 'day').toDate() |
| 62 | + }); |
| 63 | + |
| 64 | + let publishCalled = false; |
| 65 | + await expirationNotificationEmails({ |
| 66 | + publish: ({ |
| 67 | + payload |
| 68 | + }) => { |
| 69 | + publishCalled = true; |
| 70 | + expect(payload.organisationId).to.equal(organisation._id.toString()); |
| 71 | + expect(payload.emailType).to.equal(EXPIRATION_NOTIFICATION); |
| 72 | + }, |
| 73 | + dontExit: true |
| 74 | + }); |
| 75 | + |
| 76 | + expect(publishCalled).to.equal(true); |
| 77 | + |
| 78 | + const newOrg = await Organisation.findById(organisation._id); |
| 79 | + expect(newOrg.expirationNotifications.weekBeforeNotificationSent).to.equal(EMAIL_NOOP); |
| 80 | + expect(newOrg.expirationNotifications.expirationNotificationSent).to.equal(EMAIL_PROCESSING); |
| 81 | + }); |
| 82 | +}); |
0 commit comments