|
| 1 | +import type { BillingPlanResource, BillingSubscriptionItemResource, BillingSubscriptionPlanPeriod } from '@clerk/types'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { getPricingFooterState } from './pricing-footer-state'; |
| 5 | + |
| 6 | +const basePlan: BillingPlanResource = { |
| 7 | + id: 'plan_1', |
| 8 | + name: 'Pro', |
| 9 | + fee: { amount: 1000, amountFormatted: '10.00', currency: 'USD', currencySymbol: '$' }, |
| 10 | + annualFee: { amount: 10000, amountFormatted: '100.00', currency: 'USD', currencySymbol: '$' }, |
| 11 | + annualMonthlyFee: { amount: 833, amountFormatted: '8.33', currency: 'USD', currencySymbol: '$' }, |
| 12 | + description: 'desc', |
| 13 | + isDefault: false, |
| 14 | + isRecurring: true, |
| 15 | + hasBaseFee: true, |
| 16 | + forPayerType: 'user', |
| 17 | + publiclyVisible: true, |
| 18 | + slug: 'pro', |
| 19 | + avatarUrl: '', |
| 20 | + features: [], |
| 21 | + freeTrialDays: 14, |
| 22 | + freeTrialEnabled: true, |
| 23 | + pathRoot: '', |
| 24 | + reload: async () => undefined as any, |
| 25 | +}; |
| 26 | + |
| 27 | +const makeSub = (overrides: Partial<BillingSubscriptionItemResource>): BillingSubscriptionItemResource => ({ |
| 28 | + id: 'si_1', |
| 29 | + plan: basePlan, |
| 30 | + planPeriod: 'month', |
| 31 | + status: 'active', |
| 32 | + createdAt: new Date('2021-01-01'), |
| 33 | + paymentSourceId: 'src_1', |
| 34 | + pastDueAt: null, |
| 35 | + periodStart: new Date('2021-01-01'), |
| 36 | + periodEnd: new Date('2021-01-31'), |
| 37 | + canceledAt: null, |
| 38 | + isFreeTrial: false, |
| 39 | + cancel: async () => undefined as any, |
| 40 | + pathRoot: '', |
| 41 | + reload: async () => undefined as any, |
| 42 | + ...overrides, |
| 43 | +}); |
| 44 | + |
| 45 | +const run = (args: { |
| 46 | + subscription?: BillingSubscriptionItemResource; |
| 47 | + plan?: BillingPlanResource; |
| 48 | + planPeriod?: BillingSubscriptionPlanPeriod; |
| 49 | + forOrganizations?: boolean; |
| 50 | + hasActiveOrganization?: boolean; |
| 51 | +}) => |
| 52 | + getPricingFooterState({ |
| 53 | + subscription: args.subscription, |
| 54 | + plan: args.plan ?? basePlan, |
| 55 | + planPeriod: args.planPeriod ?? 'month', |
| 56 | + forOrganizations: args.forOrganizations, |
| 57 | + hasActiveOrganization: args.hasActiveOrganization ?? false, |
| 58 | + }); |
| 59 | + |
| 60 | +describe('usePricingFooterState', () => { |
| 61 | + it('hides footer when org plans and no active org', () => { |
| 62 | + const res = run({ subscription: undefined, forOrganizations: true, hasActiveOrganization: false }); |
| 63 | + expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('shows footer when no subscription and user plans', () => { |
| 67 | + const res = run({ subscription: undefined, forOrganizations: false }); |
| 68 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('shows notice when subscription is upcoming', () => { |
| 72 | + const res = run({ subscription: makeSub({ status: 'upcoming' }) }); |
| 73 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: true }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('shows footer when active but canceled', () => { |
| 77 | + const res = run({ subscription: makeSub({ status: 'active', canceledAt: new Date('2021-02-01') }) }); |
| 78 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('shows footer when switching period to paid annual', () => { |
| 82 | + const res = run({ |
| 83 | + subscription: makeSub({ status: 'active', planPeriod: 'month' }), |
| 84 | + planPeriod: 'annual', |
| 85 | + plan: basePlan, |
| 86 | + }); |
| 87 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('shows notice when active free trial', () => { |
| 91 | + const res = run({ subscription: makeSub({ status: 'active', isFreeTrial: true }) }); |
| 92 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('hides footer when active and matching period without trial', () => { |
| 96 | + const res = run({ subscription: makeSub({ status: 'active', planPeriod: 'month', isFreeTrial: false }) }); |
| 97 | + expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('shows footer when switching period to paid monthly', () => { |
| 101 | + const res = run({ |
| 102 | + subscription: makeSub({ status: 'active', planPeriod: 'annual' }), |
| 103 | + planPeriod: 'month', |
| 104 | + plan: basePlan, |
| 105 | + }); |
| 106 | + expect(res).toEqual({ shouldShowFooter: true, shouldShowFooterNotice: false }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('does not show footer when switching period if annualMonthlyFee is 0', () => { |
| 110 | + const freeAnnualPlan: BillingPlanResource = { |
| 111 | + ...basePlan, |
| 112 | + annualMonthlyFee: { ...basePlan.annualMonthlyFee, amount: 0, amountFormatted: '0.00' }, |
| 113 | + }; |
| 114 | + const res = run({ |
| 115 | + subscription: makeSub({ status: 'active', planPeriod: 'month' }), |
| 116 | + planPeriod: 'annual', |
| 117 | + plan: freeAnnualPlan, |
| 118 | + }); |
| 119 | + expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('hides footer when subscription is past_due', () => { |
| 123 | + const res = run({ subscription: makeSub({ status: 'past_due' }) }); |
| 124 | + expect(res).toEqual({ shouldShowFooter: false, shouldShowFooterNotice: false }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments