import { TestingModule, Test } from '@nestjs/testing' import { datatype } from 'faker' import { BioimpedanceCredit, CreditReason, Patient, Products } from '../../models' import { BioimpedanceCreditRepository, CreditPendingRepository } from '../../repositories' import { factory } from '../../../test/factories' import { MockedModule } from '../../../test/types' import { BioimpedanceCreditService } from './bioimpedance_credit.service' describe('BioimpedanceCreditService', () => { let subject: BioimpedanceCreditService let creditRepository: MockedModule let creditPendingRepository: MockedModule beforeAll(async () => { creditRepository = { create: jest.fn(), } creditPendingRepository = { create: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: BioimpedanceCreditRepository, useValue: creditRepository, }, { provide: CreditPendingRepository, useValue: creditPendingRepository, }, BioimpedanceCreditService, ], }).compile() subject = app.get(BioimpedanceCreditService) }) afterEach(() => { jest.resetAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#createTransaction', () => { const id = datatype.uuid() const value = 1 let reason: CreditReason beforeAll(() => { reason = 'other' }) 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 BioimpedanceCredit', async () => { await subject.createTransaction({ patientId, reason, value, author: patient, }) expect(creditRepository.create).toBeCalledWith(expect.any(BioimpedanceCredit)) }) }) describe('when has not a patient id', () => { it('creates a CreditPending to bioimpedance product', async () => { await subject.createTransaction({ reason, value, author: { type: 'system' }, identifier: { documentNumber: datatype.uuid() }, }) expect(creditPendingRepository.create).toBeCalledWith( expect.objectContaining({ product: Products.BIOIMPEDANCE }), ) }) }) }) })