|
| 1 | +import { expect } from 'chai'; |
| 2 | +import Checkout from '../lib/checkout'; |
| 3 | +import Stripe from 'stripe'; |
| 4 | + |
| 5 | +const stripe = {} as Stripe; // mock |
| 6 | +const checkout = new Checkout(stripe); |
| 7 | + |
| 8 | +/* |
| 9 | + default_payment_method: { |
| 10 | + brand: 'visa', |
| 11 | + exp_month: 1, |
| 12 | + exp_year: 2020, |
| 13 | + last4: 4242, |
| 14 | + } as unknown as Stripe.PaymentMethod |
| 15 | +*/ |
| 16 | + |
| 17 | +const customer = { |
| 18 | + id: 'customerid', |
| 19 | + |
| 20 | + name: 'Foo Bar', |
| 21 | + invoice_settings: { |
| 22 | + default_payment_method: null, |
| 23 | + }, |
| 24 | + sources: { |
| 25 | + data: [] |
| 26 | + }, |
| 27 | + tax_ids: { |
| 28 | + data: [] |
| 29 | + }, |
| 30 | + subscriptions: { |
| 31 | + data: [] |
| 32 | + }, |
| 33 | +} as Stripe.Customer; |
| 34 | + |
| 35 | +describe('checkout', function () { |
| 36 | + |
| 37 | + describe('.parseSubscription', function () { |
| 38 | + |
| 39 | + it('customer', async function () { |
| 40 | + const sub = await checkout.parseSubscription(customer); |
| 41 | + expect(sub).to.deep.equal({ |
| 42 | + id: null, |
| 43 | + valid: false, |
| 44 | + cancelled: false, |
| 45 | + periodEnd: null, |
| 46 | + card: null, |
| 47 | + plan: null, |
| 48 | + customer: { |
| 49 | + id: 'customerid', |
| 50 | + |
| 51 | + name: 'Foo Bar', |
| 52 | + country: null, |
| 53 | + postcode: null, |
| 54 | + vat: null |
| 55 | + } |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('customer with address', async function () { |
| 60 | + const customerWithAddress = Object.assign({}, customer, { |
| 61 | + address: { |
| 62 | + country: 'SE', |
| 63 | + postal_code: '12345' |
| 64 | + } |
| 65 | + }); |
| 66 | + const sub = await checkout.parseSubscription(customerWithAddress); |
| 67 | + expect(sub.customer.country).to.equal('SE'); |
| 68 | + expect(sub.customer.postcode).to.equal('12345'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('customer with tax id', async function () { |
| 72 | + const customerWithTaxId = Object.assign({}, customer, { |
| 73 | + tax_ids: { |
| 74 | + data: [ |
| 75 | + { value: 'SE1234567890' } |
| 76 | + ] |
| 77 | + } |
| 78 | + }); |
| 79 | + const sub = await checkout.parseSubscription(customerWithTaxId); |
| 80 | + expect(sub.customer.vat).to.equal('SE1234567890'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('customer with default payment method', async function () { |
| 84 | + const customerWithDefaultPaymentMethod = Object.assign({}, customer, { |
| 85 | + invoice_settings: { |
| 86 | + default_payment_method: { |
| 87 | + card: { |
| 88 | + brand: 'visa', |
| 89 | + exp_month: 1, |
| 90 | + exp_year: 2020, |
| 91 | + last4: 4242, |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + const sub = await checkout.parseSubscription(customerWithDefaultPaymentMethod); |
| 97 | + expect(sub.card).to.not.equal(null); |
| 98 | + }); |
| 99 | + |
| 100 | + it('customer with subscription', async function () { |
| 101 | + // TODO |
| 102 | + }); |
| 103 | + |
| 104 | + //-------------------------------------------------------------------------------- |
| 105 | + // Main sub statuses |
| 106 | + //-------------------------------------------------------------------------------- |
| 107 | + |
| 108 | + it('active subscription', async function () { |
| 109 | + const customerWithActiveSubscription = Object.assign({}, customer, { |
| 110 | + subscriptions: { |
| 111 | + data: [{ |
| 112 | + status: 'active', |
| 113 | + current_period_end: 1000000, |
| 114 | + cancelled: false, |
| 115 | + cancel_at_period_end: false, |
| 116 | + }] |
| 117 | + } |
| 118 | + }); |
| 119 | + const sub = await checkout.parseSubscription(customerWithActiveSubscription); |
| 120 | + expect(sub.status).to.equal('Renews on Jan 12, 1970'); |
| 121 | + expect(sub.valid).to.equal(true); |
| 122 | + expect(sub.cancelled).to.equal(false); |
| 123 | + expect(sub.periodEnd).to.equal(1000000); |
| 124 | + }); |
| 125 | + |
| 126 | + it('trialing subscription', async function () { |
| 127 | + const customerWithTrialingSubscription = Object.assign({}, customer, { |
| 128 | + subscriptions: { |
| 129 | + data: [{ |
| 130 | + status: 'trialing', |
| 131 | + trial_end: 1000000, |
| 132 | + cancel_at_period_end: false, |
| 133 | + cancelled: false, |
| 134 | + }] |
| 135 | + } |
| 136 | + }); |
| 137 | + const sub = await checkout.parseSubscription(customerWithTrialingSubscription); |
| 138 | + expect(sub.status).to.equal('Trial ends Jan 12, 1970'); |
| 139 | + expect(sub.valid).to.equal(true); |
| 140 | + expect(sub.cancelled).to.equal(false); |
| 141 | + }); |
| 142 | + |
| 143 | + it('incomplete subscription', async function () { |
| 144 | + // TODO |
| 145 | + }); |
| 146 | + |
| 147 | + it('past_due subscription', async function () { |
| 148 | + // TODO |
| 149 | + }); |
| 150 | + |
| 151 | + //-------------------------------------------------------------------------------- |
| 152 | + // Main sub statuses + cancel_at_period_end |
| 153 | + //-------------------------------------------------------------------------------- |
| 154 | + |
| 155 | + it('cancel_at_period_end + active subscription', async function () { |
| 156 | + const customerWithActiveCancellingSubscription = Object.assign({}, customer, { |
| 157 | + subscriptions: { |
| 158 | + data: [{ |
| 159 | + status: 'active', |
| 160 | + current_period_end: 1000000, |
| 161 | + cancel_at_period_end: true, |
| 162 | + cancelled: false, |
| 163 | + }] |
| 164 | + } |
| 165 | + }); |
| 166 | + const sub = await checkout.parseSubscription(customerWithActiveCancellingSubscription); |
| 167 | + expect(sub.status).to.equal('Cancels on Jan 12, 1970'); |
| 168 | + expect(sub.valid).to.equal(true); |
| 169 | + expect(sub.cancelled).to.equal(true); |
| 170 | + }); |
| 171 | + |
| 172 | + it('cancel_at_period_end + trialing subscription', async function () { |
| 173 | + const customerWithTrialingCancellingSubscription = Object.assign({}, customer, { |
| 174 | + subscriptions: { |
| 175 | + data: [{ |
| 176 | + status: 'trialing', |
| 177 | + current_period_end: 1000000, |
| 178 | + cancel_at_period_end: true, |
| 179 | + cancelled: false, |
| 180 | + }] |
| 181 | + } |
| 182 | + }); |
| 183 | + const sub = await checkout.parseSubscription(customerWithTrialingCancellingSubscription); |
| 184 | + expect(sub.status).to.equal('Cancels on Jan 12, 1970'); |
| 185 | + expect(sub.valid).to.equal(true); |
| 186 | + expect(sub.cancelled).to.equal(true); |
| 187 | + }); |
| 188 | + |
| 189 | + it('cancel_at_period_end + incomplete subscription', async function () { |
| 190 | + const customerWithIncompleteCancellingSubscription = Object.assign({}, customer, { |
| 191 | + subscriptions: { |
| 192 | + data: [{ |
| 193 | + status: 'incomplete', |
| 194 | + current_period_end: 1000000, |
| 195 | + cancel_at_period_end: true, |
| 196 | + cancelled: false, |
| 197 | + }] |
| 198 | + } |
| 199 | + }); |
| 200 | + const sub = await checkout.parseSubscription(customerWithIncompleteCancellingSubscription); |
| 201 | + expect(sub.status).to.equal('Cancels on Jan 12, 1970'); |
| 202 | + expect(sub.valid).to.equal(false); |
| 203 | + expect(sub.cancelled).to.equal(true); |
| 204 | + }); |
| 205 | + |
| 206 | + it('cancel_at_period_end + past_due subscription', async function () { |
| 207 | + const customerWithPastDueCancellingSubscription = Object.assign({}, customer, { |
| 208 | + subscriptions: { |
| 209 | + data: [{ |
| 210 | + status: 'past_due', |
| 211 | + current_period_end: 1000000, |
| 212 | + cancel_at_period_end: true, |
| 213 | + cancelled: false, |
| 214 | + }] |
| 215 | + } |
| 216 | + }); |
| 217 | + const sub = await checkout.parseSubscription(customerWithPastDueCancellingSubscription); |
| 218 | + expect(sub.status).to.equal('Cancels on Jan 12, 1970'); |
| 219 | + expect(sub.valid).to.equal(false); |
| 220 | + expect(sub.cancelled).to.equal(true); |
| 221 | + }); |
| 222 | + |
| 223 | + }); |
| 224 | + |
| 225 | +}); |
0 commit comments