import { expect } from 'chai'; import { addDays, subDays, formatISO } from 'date-fns'; import { transitionEvent } from '../../fixtures/states'; import { ensureDiscount, findFutureEvent } from '../../../src/helpers/state'; describe('state helpers', () => { const oneMonthAway = formatISO(addDays(new Date(), 30)); const oneWeekAway = formatISO(addDays(new Date(), 7)); const oneMonthAgo = formatISO(subDays(new Date(), 30)); describe('ensureDiscount', () => { it('returns false if no subscriptionBefore', () => { expect(ensureDiscount({ subscriptionBefore: undefined, subscriptionAfter: 'something' } as any)).to.equal(false); }); it('returns false if no subscriptionAfter', () => { expect(ensureDiscount({ subscriptionBefore: 'something', subscriptionAfter: undefined } as any)).to.equal(false); }); it('returns false if the nextPrice is the same than current price given the term has changed', () => { const event = transitionEvent({ oldProducts: ['N6D'], newProducts: ['N6D'], oldPrice: 50, newPrice: 150, term: 'P1M', newTerm: 'P13W' } as any); expect(ensureDiscount(event as any)).to.equal(false); }); it('returns false if the nextPrice is more than current price', () => { const event = transitionEvent({ newPrice: 1000000 } as any); expect(ensureDiscount(event as any)).to.equal(false); }); it('returns false if the subscription after doesnt match the same products', () => { const event = transitionEvent({ oldProducts: ['N5D', 'P2'], newProducts: ['P2'] } as any); expect(ensureDiscount(event as any)).to.equal(false); }); it('returns true if product is cheaper overall even if its more expensive due to term', () => { const event = transitionEvent({ oldPrice: 20, newPrice: 200, term: 'P1Y' } as any); expect(ensureDiscount(event as any)).to.equal(true); }); it('returns true otherwise', () => { const event = transitionEvent({ oldPrice: 29, newPrice: 28 } as any); expect(ensureDiscount(event as any)).to.equal(true); }); }); describe('findFutureEvent', () => { it('finds the first event with the specified type that is in the future', () => { const events = [ transitionEvent({ effectiveDate: oneMonthAway } as any), transitionEvent({ effectiveDate: oneWeekAway } as any) ]; const foundEvent = findFutureEvent(events as any, 'Transition'); expect(foundEvent).to.have.property('eventEffectiveDate', oneMonthAway); }); it('does not return past events', () => { const events = [ transitionEvent({ effectiveDate: oneMonthAgo } as any) ]; const foundEvent = findFutureEvent(events as any, 'Transition'); expect(foundEvent).to.have.equal(undefined); }); it('returns undefined if there are no events that match', () => { const events = [ transitionEvent({ effectiveDate: oneMonthAway } as any) ]; const foundEvent = findFutureEvent(events as any, 'Cancellation'); }); }); });