import { factory } from './../../../../test/factories' import { AddonStatus } from './addon.enum' import { Addon } from './addon.model' describe('Addon', () => { let addon: Addon describe('#constructor', () => { const attributes = [ 'id', 'addonId', 'purchaseId', 'createdAt', 'updatedAt', 'expiresAt', 'freeDaysExpiredAt', 'originalPrice', 'origin', 'subscribeLocationId', 'freeDays', ] let args: Partial describe("with no arguments as 'new Addon()'", () => { beforeAll(() => { args = undefined addon = new Addon(args) }) it('initializes an empty addon', () => { expect(addon).toEqual(new Addon()) }) }) attributes.forEach(attr => { describe(`with ${attr} as 'new Addon({${attr}: value})'`, () => { let value: any let fixture: Partial beforeEach(async () => { fixture = await factory.build('addon') args = {} value = fixture[attr] args[attr] = value addon = new Addon(args) }) it(`initializes with ${attr}`, () => { expect(addon[attr]).toEqual(value) }) attributes .filter(other => other !== attr) .forEach(other => { it(`initializes without ${other}`, () => { expect(addon[other]).toEqual(undefined) }) }) }) }) }) describe('#isAlive', () => { describe('when addon is in trial', () => { beforeAll(async () => { addon = await factory.build('addon:trial') }) it('returns true', () => expect(addon.isAlive).toEqual(true)) }) describe('when addon is expired', () => { beforeAll(async () => { addon = await factory.build('addon:cancelled') }) it('returns false', () => expect(addon.isAlive).toEqual(false)) }) describe('when addon isn`t expired', () => { describe('with a future date of expiration', () => { beforeAll(async () => { addon = await factory.build('addon:in_cancellation') }) it('returns true', () => expect(addon.isAlive).toEqual(true)) }) describe('without a date of expiration', () => { beforeAll(async () => { addon = await factory.build('addon:active') }) it('returns true', () => expect(addon.isAlive).toEqual(true)) }) describe('with a trial expired', () => { beforeAll(async () => { addon = await factory.build('addon:trial_expired') }) it('returns true', () => expect(addon.isAlive).toEqual(true)) }) }) }) describe('.build()', () => { beforeEach(() => { addon = Addon.build({ id: 1159424, addon_id: 3, purchase_id: 10921615, created_at: '2020-05-03T10:36:43.000-03:00', updated_at: '2020-05-03T10:36:43.000-03:00', expires_at: '2020-05-18', free_days_expired_at: '2020-05-17', original_price: '9.9', origin: 'equipment', subscribe_location_id: 417, free_days: 15, cancelled_at: '2020-05-19', }) }) it('builds an addon from the given data', () => { expect(addon).toEqual( new Addon({ id: '1159424', addonId: '3', purchaseId: '10921615', createdAt: new Date('2020-05-03T10:36:43.000-03:00'), updatedAt: new Date('2020-05-03T10:36:43.000-03:00'), expiresAt: new Date('2020-05-18'), freeDaysExpiredAt: new Date('2020-05-17'), originalPrice: 9.9, origin: 'equipment', subscribeLocationId: 417, freeDays: 15, cancelledAt: new Date('2020-05-19'), }), ) }) }) describe('#status', () => { describe.each` status | expected ${'active'} | ${AddonStatus.ACTIVE} ${'in_cancellation'} | ${AddonStatus.IN_CANCELLATION} ${'cancelled'} | ${AddonStatus.CANCELLED} ${'trial'} | ${AddonStatus.TRIAL} `('when addon is "$status"', ({ status, expected }) => { beforeEach(async () => { addon = await factory.build(`addon:${status}`) }) it(`returns "${expected}"`, () => { expect(addon.status).toEqual(expected) }) }) }) })