import { TestingModule, Test } from '@nestjs/testing' import { datatype, random } from 'faker' import { TeleconsultationsCredit, CreditReason, Patient } from '../../models' import { TeleconsultationsCreditRepository, CreditPendingRepository } from '../../repositories' import { factory } from '../../../test/factories' import { MockedModule } from '../../../test/types' import { UnprocessableEntityError } from '../../errors' import { TeleconsultationsCreditService } from './teleconsultations_credit.service' describe('TeleconsultationsCreditService', () => { let subject: TeleconsultationsCreditService let creditRepository: MockedModule let creditPendingRepository: MockedModule beforeAll(async () => { creditRepository = { create: jest.fn(), getSeparateCredits: jest.fn(), } creditPendingRepository = { create: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: TeleconsultationsCreditRepository, useValue: creditRepository, }, { provide: CreditPendingRepository, useValue: creditPendingRepository, }, TeleconsultationsCreditService, ], }).compile() subject = app.get(TeleconsultationsCreditService) }) afterEach(() => { jest.resetAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#createTransaction', () => { const id = datatype.uuid() const value = 1 let reason: CreditReason beforeAll(() => { reason = 'plan_subscription' }) beforeEach(() => { creditRepository.create.mockResolvedValue(id) }) describe('when has a patient id', () => { const patientId = datatype.uuid() let patient: Patient beforeAll(async () => { patient = await factory.build('patient') }) it('creates a TeleconsultationsCredit', async () => { await subject.createTransaction({ patientId, reason, value, author: patient, }) expect(creditRepository.create).toBeCalledWith(expect.any(TeleconsultationsCredit)) }) describe('when reason is other', () => { beforeAll(() => { reason = 'other' }) describe('and has other_reason field', () => { it('saves other_reason field', async () => { const otherReason = random.words() await subject.createTransaction({ patientId, reason, value, otherReason, author: patient, }) expect(creditRepository.create).toBeCalledWith(expect.objectContaining({ other_reason: otherReason })) }) }) describe(`and hasn't other_reason field`, () => { it('throws an error', async () => { await expect( subject.createTransaction({ patientId, reason, value, author: patient, }), ).rejects.toThrowError(UnprocessableEntityError) }) }) }) }) describe('when has not a patient id', () => { it('creates a CreditPending to teleconsultations product', async () => { await subject.createTransaction({ reason: 'plan_subscription', value, author: { type: 'system' }, identifier: { documentNumber: datatype.uuid() }, }) expect(creditRepository.create).not.toBeCalledWith(expect.objectContaining({ other_reason: undefined }), [ 'other_reason', ]) }) }) }) describe('getSeparateCredits', () => { const patientId = datatype.uuid() const separateCredits = { teleconsultation: datatype.number({ min: 0, max: 10 }), } beforeAll(() => { creditRepository.getSeparateCredits.mockResolvedValue(separateCredits) }) it('returns an object with separated credits', async () => { await expect(subject.getSeparateCredits(patientId)).resolves.toEqual(separateCredits) }) }) })