import { addDays, endOfDay, startOfDay, subDays } from 'date-fns' import { factory } from '../../test/factories' import { DiscountCoupon } from './discount_coupon.model' describe('DiscountCoupon', () => { let subject: DiscountCoupon beforeEach(async () => { subject = new DiscountCoupon(await factory.build('discount_coupon')) }) describe('#apply', () => { describe.each` amount | discount_amount | discount_type | result ${1000.0} | ${20} | ${'percentage'} | ${800.0} ${91} | ${30} | ${'percentage'} | ${63.7} ${1000.0} | ${20} | ${'total'} | ${980.0} `( '"$discount_amount" "$discount_type" of discount from "$amount"', ({ amount, discount_amount, discount_type, result }) => { beforeEach(() => { subject.discountAmount = discount_amount subject.discountType = discount_type }) it(`results in "${result}"`, () => { expect(subject.apply(amount)).toEqual(result) }) }, ) }) describe('#isActive', () => { const now = new Date() describe.each` beginsAt | endsAt | active ${subDays(now, 1)} | ${null} | ${true} ${subDays(now, 1)} | ${addDays(now, 1)} | ${true} ${now} | ${addDays(now, 1)} | ${true} ${subDays(now, 1)} | ${endOfDay(now)} | ${true} ${startOfDay(now)} | ${endOfDay(now)} | ${true} ${addDays(now, 1)} | ${null} | ${false} ${subDays(now, 10)} | ${subDays(now, 1)} | ${false} `('when cupom begins at "$beginsAt" and ends at "$endsAt"', ({ beginsAt, endsAt, active }) => { it(`returns "${active}"`, () => { subject.beginsAt = beginsAt subject.endsAt = endsAt expect(subject.isActive).toEqual(active) }) }) }) describe('#isValid', () => { const now = new Date() let planId: string const isValid = (): boolean => { return subject.isValid({ planId }) } describe('when it is inactive', () => { beforeEach(() => { subject.endsAt = subDays(now, 1) }) it('is invalid', () => { expect(isValid()).toBeFalsy() }) }) describe('when it doesnt allows some plan', () => { beforeEach(() => { jest.spyOn(subject, 'allows').mockImplementationOnce(() => false) }) it('is invalid', () => { expect(isValid()).toBeFalsy() }) }) describe('when it is active and it allows plan', () => { beforeEach(() => { jest.spyOn(subject, 'allows').mockImplementationOnce(() => true) }) it('is invalid', () => { expect(isValid()).toBeTruthy() }) }) }) })