| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const modulePath =
- '../../../../app/src/Features/Subscription/RecurlyEventHandler'
- describe('RecurlyEventHandler', function () {
- beforeEach(function () {
- this.userId = '123abc234bcd456cde567def'
- this.planCode = 'collaborator-annual'
- this.eventData = {
- account: {
- account_code: this.userId,
- },
- subscription: {
- uuid: '8435ad98c1ce45da99b07f6a6a2e780f',
- plan: {
- plan_code: 'collaborator-annual',
- },
- quantity: 1,
- state: 'active',
- trial_started_at: new Date('2021-01-01 12:34:56'),
- trial_ends_at: new Date('2021-01-08 12:34:56'),
- current_period_started_at: new Date('2021-01-01 12:34:56'),
- current_period_ends_at: new Date('2021-01-08 12:34:56'),
- },
- }
- this.RecurlyEventHandler = SandboxedModule.require(modulePath, {
- requires: {
- './SubscriptionEmailHandler': (this.SubscriptionEmailHandler = {
- sendTrialOnboardingEmail: sinon.stub(),
- }),
- '../Analytics/AnalyticsManager': (this.AnalyticsManager = {
- recordEventForUser: sinon.stub(),
- setUserPropertyForUser: sinon.stub(),
- }),
- '../SplitTests/SplitTestHandler': (this.SplitTestHandler = {
- promises: {
- getAssignmentForUser: sinon.stub().resolves({
- variant: 'default',
- }),
- },
- }),
- },
- })
- })
- it('with new_subscription_notification - free trial', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'new_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-started',
- {
- plan_code: this.planCode,
- quantity: 1,
- is_trial: true,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-plan-code',
- this.planCode
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-state',
- 'active'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-is-trial',
- true
- )
- })
- it('sends free trial onboarding email if user starting a trial', async function () {
- await this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'new_subscription_notification',
- this.eventData
- )
- sinon.assert.called(this.SubscriptionEmailHandler.sendTrialOnboardingEmail)
- })
- it('with new_subscription_notification - no free trial', function () {
- this.eventData.subscription.current_period_started_at = new Date(
- '2021-02-10 12:34:56'
- )
- this.eventData.subscription.current_period_ends_at = new Date(
- '2021-02-17 12:34:56'
- )
- this.eventData.subscription.quantity = 3
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'new_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-started',
- {
- plan_code: this.planCode,
- quantity: 3,
- is_trial: false,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-state',
- 'active'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-is-trial',
- false
- )
- })
- it('with updated_subscription_notification', function () {
- this.planCode = 'new-plan-code'
- this.eventData.subscription.plan.plan_code = this.planCode
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'updated_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-updated',
- {
- plan_code: this.planCode,
- quantity: 1,
- is_trial: true,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-plan-code',
- this.planCode
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-state',
- 'active'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-is-trial',
- true
- )
- })
- it('with canceled_subscription_notification', async function () {
- this.eventData.subscription.state = 'cancelled'
- await this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'canceled_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.SplitTestHandler.promises.getAssignmentForUser,
- this.userId,
- 'design-system-updates'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-cancelled',
- {
- plan_code: this.planCode,
- quantity: 1,
- is_trial: true,
- subscriptionId: this.eventData.subscription.uuid,
- 'split-test-design-system-updates': 'default',
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-state',
- 'cancelled'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-is-trial',
- true
- )
- })
- it('with expired_subscription_notification', function () {
- this.eventData.subscription.state = 'expired'
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'expired_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-expired',
- {
- plan_code: this.planCode,
- quantity: 1,
- is_trial: true,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-plan-code',
- this.planCode
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-state',
- 'expired'
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.setUserPropertyForUser,
- this.userId,
- 'subscription-is-trial',
- true
- )
- })
- it('with renewed_subscription_notification', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'renewed_subscription_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-renewed',
- {
- plan_code: this.planCode,
- quantity: 1,
- is_trial: true,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- })
- it('with reactivated_account_notification', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'reactivated_account_notification',
- this.eventData
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-reactivated',
- {
- plan_code: this.planCode,
- quantity: 1,
- subscriptionId: this.eventData.subscription.uuid,
- }
- )
- })
- it('with paid_charge_invoice_notification', function () {
- const invoice = {
- invoice_number: 1234,
- currency: 'USD',
- state: 'paid',
- total_in_cents: 720,
- tax_in_cents: 12,
- address: {
- country: 'Liurnia',
- },
- collection_method: 'automatic',
- subscription_ids: ['abcd1234', 'defa3214'],
- }
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'paid_charge_invoice_notification',
- {
- account: {
- account_code: this.userId,
- },
- invoice,
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-invoice-collected',
- {
- invoiceNumber: invoice.invoice_number,
- currency: invoice.currency,
- totalInCents: invoice.total_in_cents,
- taxInCents: invoice.tax_in_cents,
- country: invoice.address.country,
- collectionMethod: invoice.collection_method,
- subscriptionId1: invoice.subscription_ids[0],
- subscriptionId2: invoice.subscription_ids[1],
- }
- )
- })
- it('with paid_charge_invoice_notification and total_in_cents 0', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'paid_charge_invoice_notification',
- {
- account: {
- account_code: this.userId,
- },
- invoice: {
- state: 'paid',
- total_in_cents: 0,
- },
- }
- )
- sinon.assert.notCalled(this.AnalyticsManager.recordEventForUser)
- })
- it('with closed_invoice_notification', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'closed_invoice_notification',
- {
- account: {
- account_code: this.userId,
- },
- invoice: {
- state: 'collected',
- total_in_cents: 720,
- },
- }
- )
- sinon.assert.calledWith(
- this.AnalyticsManager.recordEventForUser,
- this.userId,
- 'subscription-invoice-collected'
- )
- })
- it('with closed_invoice_notification and total_in_cents 0', function () {
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'closed_invoice_notification',
- {
- account: {
- account_code: this.userId,
- },
- invoice: {
- state: 'collected',
- total_in_cents: 0,
- },
- }
- )
- sinon.assert.notCalled(this.AnalyticsManager.recordEventForUser)
- })
- it('nothing is called with invalid account code', function () {
- this.eventData.account.account_code = 'foo_bar'
- this.RecurlyEventHandler.sendRecurlyAnalyticsEvent(
- 'new_subscription_notification',
- this.eventData
- )
- sinon.assert.notCalled(this.AnalyticsManager.recordEventForUser)
- sinon.assert.notCalled(this.AnalyticsManager.setUserPropertyForUser)
- sinon.assert.notCalled(this.AnalyticsManager.setUserPropertyForUser)
- sinon.assert.notCalled(this.AnalyticsManager.setUserPropertyForUser)
- })
- })
|