import { expect } from 'chai'; import { PaymentType } from '../../../src/types/subscription'; import { Offer } from '../../../src/models/offer'; import { mapPaymentOptions } from '../../../src/mappers/payment-options'; import offer from '../../fixtures/offer'; describe('mapPaymentOptions', () => { let options: any; let mockOffer: any; beforeEach(() => { mockOffer = JSON.parse(JSON.stringify(offer.data.offerById)); options = { paymentType: PaymentType.CREDITCARD, paymentMethodId: '1234567890', paymentGateway: 'WorldPay - GBP', paymentTerm: 'P1Y', billingCountry: 'GBR', offer: new Offer(mockOffer), option: 'HD', startDate: 'date' }; }); it('should throw an error if charge can not be found', () => { options.offer.pricing = []; expect(() => { mapPaymentOptions(options); }).to.throw(); }); it('should map default options', () => { const result = mapPaymentOptions(options); expect(result).to.contain({ paymentType: options.paymentType.toUpperCase(), paymentMethodId: options.paymentMethodId, paymentGateway: options.paymentGateway, price: '550.00', currencyCode: 'GBP', offerId: options.offer.id }); expect(result).to.have.property('subscriptionTerm'); expect(result.subscriptionTerm).to.deep.equal({ iso8601Duration: 'P1Y', termType: 'Evergreen', autoRenewTerm: true }); }); describe('PayPal', () => { beforeEach(() => { options.paypalBAID = '1234567890'; options.paypalEmail = 'email@example.com'; }); it('should not have PayPal information', () => { expect(mapPaymentOptions(options)).to.not.haveOwnProperty('paypalBAID'); expect(mapPaymentOptions(options)).to.not.haveOwnProperty('paypalEmail'); }); it('should have PayPal information if PaymentType is correct', () => { options.paymentType = PaymentType.PAYPAL; expect(mapPaymentOptions(options)).to.contain({ paypalBAID: options.paypalBAID, paypalEmail: options.paypalEmail }); }); }); describe('Apple Pay', () => { it('should not have Apple Pay information', () => { expect(mapPaymentOptions(options)).to.not.haveOwnProperty('paymentToken'); expect(mapPaymentOptions(options)).to.not.haveOwnProperty('transactionIdentifier'); }); it('should have Apple Pay information if PaymentType is correct', () => { options.paymentType = PaymentType.APPLEPAY; options.paymentMethodId = '{ "paymentData": "paymentData", "transactionIdentifier": "transactionIdentifier"}'; expect(mapPaymentOptions(options)).to.contain({ paymentMethodId: 'apple-pay', paymentToken: 'paymentData', transactionIdentifier: 'transactionIdentifier' }); }); }); describe('Trial', () => { it('should not have trial information when offer not trial', () => { options.offer.type = 'DIGITAL'; expect(mapPaymentOptions(options)).to.not.haveOwnProperty('term'); expect(mapPaymentOptions(options)).to.not.haveOwnProperty('trial'); }); it('should have trial information when offer is trial', () => { options.offer.type = 'TRIAL'; options.paymentTerm = 'P1M'; options.offer.pricing[1].charges[0].paymentDue = { after: 'P1M' }; options.offer.pricing[1].trialTotal = { 'currency': 'GBP', 'nativeSymbol': '£', 'symbol': '£', 'value': '1.00' }; const result = mapPaymentOptions(options); expect(result).to.contain({ term: 'trial', }); expect(result).to.have.property('subscriptionTerm'); expect(result.subscriptionTerm).to.deep.equal({ iso8601Duration: 'P1M', termType: 'Evergreen', autoRenewTerm: true }); }); }); describe('Single term subscriptions', () => { it('should set `subscriptionTerm.termType` type to \'Termed\'', () => { options.singleTermSubscription = true; const result = mapPaymentOptions(options); expect(result.subscriptionTerm.termType).to.equal('Termed'); }); it('should set `subscriptionTerm.autoRenewTerm` to false', () => { options.singleTermSubscription = true; const result = mapPaymentOptions(options); expect(result.subscriptionTerm.autoRenewTerm).to.equal(false); }); }); describe('Delivery', () => { it('should not contain delivery information if offer not print or bundle', () => { options.offer.isPrint = () => false; options.offer.isBundle = () => false; expect(mapPaymentOptions(options)).to.not.haveOwnProperty('startDate'); expect(mapPaymentOptions(options)).to.not.haveOwnProperty('option'); }); it('should contain delivery information for print', () => { options.offer.isPrint = () => true; options.offer.isBundle = () => false; expect(mapPaymentOptions(options)).to.contain({ startDate: options.startDate, option: options.option }); }); it('should contain delivery information for bundle', () => { options.offer.isPrint = () => false; options.offer.isBundle = () => true; expect(mapPaymentOptions(options)).to.contain({ startDate: options.startDate, option: options.option }); }); }); describe('Segment ID', () => { it('should not contain a segment id', () => { expect(mapPaymentOptions(options)).to.not.haveOwnProperty('segmentId'); }); it('should map segment id if provided', () => { options.segmentId = 'test'; expect(mapPaymentOptions(options)).to.contain({ segmentId: options.segmentId }); }); }); describe('Send confirmation email', ()=>{ it('should set `sendConfirmationEmail` to `true` when `options.sendConfirmationEmail` is `true`', () => { options.sendConfirmationEmail = true; expect(mapPaymentOptions(options)).to.contain({ sendConfirmationEmail: true }); }); it('should default `sendConfirmationEmail` to `true` when `options.sendConfirmationEmail` is undefined (not set)', () => { options.sendConfirmationEmail = undefined; expect(mapPaymentOptions(options)).to.contain({ sendConfirmationEmail: true }); }); it('should set `sendConfirmationEmail` to `false` when `options.sendConfirmationEmail` is `false`', () => { options.sendConfirmationEmail = false; expect(mapPaymentOptions(options)).to.contain({ sendConfirmationEmail: false }); }); it('should throw an error if `options.sendConfirmationEmail` is not undefined or a boolean', () => { options.sendConfirmationEmail = 'false'; expect(() => { mapPaymentOptions(options); }).to.throw(); }); }); });