import { Test, TestingModule } from '@nestjs/testing' import { datatype } from 'faker' import { BioimpedanceService } from '../../../services/bioimpedance' import { CloudWatchService } from '../../../services/cloudwatch' import { MockedModule } from '../../../../test/types' import { NotFoundError } from '../../../errors' import { AnthropometriesFilter } from '../../../repositories' import { Bioimpedance } from './bioimpedance' import { PatientAnthropomentry } from './models' describe('Bioimpedance', () => { let subject: Bioimpedance let bioimpedanceService: MockedModule let cloudWatch: MockedModule beforeAll(async () => { bioimpedanceService = { getPatientAnthropometriesCache: jest.fn(), } cloudWatch = { putLog: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: BioimpedanceService, useValue: bioimpedanceService, }, { provide: CloudWatchService, useValue: cloudWatch, }, Bioimpedance, ], }).compile() subject = app.get(Bioimpedance) }) afterEach(() => { jest.resetAllMocks() }) describe('#getAllAnthropometries()', () => { let login: string let filter: AnthropometriesFilter beforeAll(() => { login = datatype.uuid() filter = 'bioimpedances' }) describe('when there is cache', () => { let result: PatientAnthropomentry[] beforeEach(async () => { bioimpedanceService.getPatientAnthropometriesCache.mockResolvedValue([]) cloudWatch.putLog.mockResolvedValue(null) result = await subject.getAllAnthropometries(login, filter) }) it('send cache telemetry', () => { expect(cloudWatch.putLog).toBeCalled() }) it('returns cached value', () => { expect(result).toEqual([]) }) }) describe('when there is no cache', () => { beforeEach(() => { bioimpedanceService.getPatientAnthropometriesCache.mockResolvedValue(null) }) it('throws an error', async () => { await expect(subject.getAllAnthropometries(login, filter)).rejects.toThrowError(NotFoundError) }) }) }) })