SubscriptionEmailHandler.test.mjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath =
  4. '../../../../app/src/Features/Subscription/SubscriptionEmailHandler'
  5. describe('SubscriptionEmailHandler', function () {
  6. beforeEach(async function (ctx) {
  7. ctx.userId = '123456789abcde'
  8. ctx.email = 'test@test.com'
  9. vi.doMock('../../../../app/src/Features/Email/EmailHandler', () => ({
  10. default: (ctx.EmailHandler = {
  11. promises: {
  12. sendEmail: sinon.stub().resolves({}),
  13. },
  14. }),
  15. }))
  16. vi.doMock('../../../../app/src/Features/User/UserGetter', () => ({
  17. default: (ctx.UserGetter = {
  18. promises: {
  19. getUser: sinon
  20. .stub()
  21. .resolves({ _id: ctx.userId, email: 'test@test.com' }),
  22. },
  23. }),
  24. }))
  25. vi.doMock('../../../../app/src/Features/Subscription/PlansLocator', () => ({
  26. default: (ctx.PlansLocator = {
  27. findLocalPlanInSettings: sinon.stub().returns({
  28. name: 'foo',
  29. features: { collaborators: 42 },
  30. }),
  31. }),
  32. }))
  33. vi.doMock('@overleaf/settings', () => ({
  34. default: (ctx.Settings = {
  35. enableOnboardingEmails: true,
  36. }),
  37. }))
  38. ctx.SubscriptionEmailHandler = (await import(modulePath)).default
  39. })
  40. describe('when onboarding emails are disabled', function () {
  41. beforeEach(function (ctx) {
  42. ctx.Settings.enableOnboardingEmails = false
  43. })
  44. it('does not send a trial onboarding email', async function (ctx) {
  45. await ctx.SubscriptionEmailHandler.sendTrialOnboardingEmail(
  46. ctx.userId,
  47. 'foo-plan-code'
  48. )
  49. expect(ctx.EmailHandler.promises.sendEmail).to.not.have.been.called
  50. })
  51. })
  52. describe('when onboarding emails are enabled', function () {
  53. it('sends trial onboarding email', async function (ctx) {
  54. await ctx.SubscriptionEmailHandler.sendTrialOnboardingEmail(
  55. ctx.userId,
  56. 'foo-plan-code'
  57. )
  58. expect(ctx.PlansLocator.findLocalPlanInSettings).to.have.been.calledWith(
  59. 'foo-plan-code'
  60. )
  61. expect(ctx.EmailHandler.promises.sendEmail.lastCall.args).to.deep.equal([
  62. 'trialOnboarding',
  63. {
  64. to: ctx.email,
  65. sendingUser_id: ctx.userId,
  66. planName: 'foo',
  67. features: { collaborators: 42 },
  68. },
  69. ])
  70. })
  71. })
  72. })