import { Test, TestingModule } from '@nestjs/testing' import { datatype } from 'faker' import { Anthropometry, BioimpedanceCredit, BioimpedanceImportation, SmartfitUnit } from '../../../models' import { AnthropometryRepository, BioimpedanceCreditRepository, BioimpedanceImportationRepository, PatientRepository, } from '../../../repositories' import { CravaService } from '../../../services/crava/crava.service' import { factory } from '../../../../test/factories' import { MockedModule } from '../../../../test/types' import { Credits } from './credits' describe('Credits', () => { let subject: Credits let cravaService: MockedModule let anthropometryRepository: MockedModule let bioimpedanceImportationRepository: MockedModule let bioimpedanceCreditRepository: MockedModule let patientRepository: MockedModule beforeAll(async () => { cravaService = { redeemBenefict: jest.fn(), } anthropometryRepository = { findByBioimpedance: jest.fn(), } bioimpedanceImportationRepository = { findByAnthropometry: jest.fn(), } bioimpedanceCreditRepository = { findCreditsByAuthorType: jest.fn(), } patientRepository = { get: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: CravaService, useValue: cravaService, }, { provide: AnthropometryRepository, useValue: anthropometryRepository, }, { provide: BioimpedanceImportationRepository, useValue: bioimpedanceImportationRepository, }, { provide: BioimpedanceCreditRepository, useValue: bioimpedanceCreditRepository, }, { provide: PatientRepository, useValue: patientRepository, }, Credits, ], }).compile() subject = app.get(Credits) }) afterEach(() => { jest.resetAllMocks() }) describe('#notifyCravaSystemThatBioimpedanceCreditWasUsed', () => { let credit: BioimpedanceCredit beforeAll(async () => { credit = await factory.build('bioimpedance_credit') }) describe('when credit created is a addition', () => { beforeAll(() => { credit.value = 1 }) it('does not notificate the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).not.toBeCalled() }) }) describe('when credit created is a substract', () => { let anthropometry: Partial let bioimpedanceImportation: Partial let unit: SmartfitUnit beforeAll(async () => { credit.value = -1 unit = await factory.build('smartfit_unit') anthropometry = { id: datatype.uuid() } bioimpedanceImportation = { smartfit_unit: unit, anthropometry_id: anthropometry.id } }) beforeEach(() => { anthropometryRepository.findByBioimpedance.mockResolvedValue(anthropometry) bioimpedanceImportationRepository.findByAnthropometry.mockResolvedValue(bioimpedanceImportation) }) describe('when the unit origin of bioimpedance is not a Centauro unit', () => { beforeAll(() => { bioimpedanceImportation.smartfit_unit.acronym = `SMRT${datatype.number()}` }) it('does not notificate the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).not.toBeCalled() }) }) describe('when the unit origin of bioimpedance is a Centauro unit', () => { let bioimpedanceCredits: BioimpedanceCredit[] beforeAll(() => { bioimpedanceCredits = [] bioimpedanceImportation.smartfit_unit.acronym = `CTARO${datatype.number()}` }) beforeEach(() => { bioimpedanceCreditRepository.findCreditsByAuthorType.mockResolvedValue(bioimpedanceCredits) }) describe('when the patient has not credit from crava', () => { it('does not notificate the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).not.toBeCalled() }) }) describe('when the patient has credit from crava', () => { beforeAll(async () => { bioimpedanceCredits = [await factory.build('bioimpedance_credit')] }) beforeEach(() => { patientRepository.get.mockResolvedValue({ document_number: datatype.uuid() }) }) it('notificates the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).toBeCalled() }) }) }) describe('when the bioimpedance importation has not a unit', () => { beforeAll(() => { bioimpedanceImportation.smartfit_unit = null }) it('does not notificate the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).not.toBeCalled() }) }) describe('when not found the bioimpedance importation', () => { beforeAll(() => { bioimpedanceImportation = null }) it('does not notificate the crava system', async () => { await subject.notifyCravaSystemThatBioimpedanceCreditWasUsed(credit) expect(cravaService.redeemBenefict).not.toBeCalled() }) }) }) }) })