import { TestingModule, Test } from '@nestjs/testing' import { Patient } from '../../models' import { factory } from '../../../test/factories' import { MockedModule } from '../../../test/types' import { RedisService } from '../redis/redis.service' import { PatientRepository } from '../../repositories' import { PatientService } from './patient.service' describe('PatientService', () => { let subject: PatientService let patientRepository: MockedModule let redisService: MockedModule beforeAll(async () => { patientRepository = { findByLogin: jest.fn(), } redisService = { get: jest.fn(), set: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: PatientRepository, useValue: patientRepository, }, { provide: RedisService, useValue: redisService, }, PatientService, ], }).compile() subject = app.get(PatientService) }) afterEach(() => { jest.resetAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#getPatientCache', () => { let patient: Patient beforeAll(async () => { patient = await factory.build('patient') patientRepository.findByLogin.mockResolvedValue(patient) }) describe('when cache not exists', () => { beforeAll(() => { redisService.get.mockResolvedValue(null) }) it('returns null', async () => { await expect(subject.getPatientCache(patient.login)).resolves.toBeNull() }) }) describe('when cache exists but is an empty object', () => { beforeAll(() => { redisService.get.mockResolvedValue('{}') }) it('returns an empty object', async () => { await expect(subject.getPatientCache(patient.login)).resolves.toEqual({}) }) }) describe('when cache exists with an error object', () => { const errors = { error: 'not-found' } beforeAll(() => { redisService.get.mockResolvedValue(JSON.stringify(errors)) }) it('returns null', async () => { await expect(subject.getPatientCache(patient.login)).resolves.toBeNull() }) }) describe('when cache is not an object', () => { beforeAll(() => { redisService.get.mockResolvedValue(JSON.stringify('')) }) it('returns an empty object', async () => { await expect(subject.getPatientCache(patient.login)).resolves.toEqual({}) }) }) }) describe('#createPatientCache', () => { let patient: Patient let setCacheSpy: jest.SpyInstance beforeAll(async () => { patient = await factory.build('patient:subscriber') patientRepository.findByLogin.mockResolvedValue(patient) }) beforeEach(() => { setCacheSpy = jest.spyOn(subject, 'setCache') }) describe('when patient not exists', () => { beforeAll(async () => { patientRepository.findByLogin.mockResolvedValue(null) }) it('writes empty object', async () => { await subject.createPatientCache(patient) expect(setCacheSpy).toBeCalledWith(`patient:${patient.login}`, {}) }) }) describe('when patient exists', () => { describe('when patient is a subscriber', () => { beforeAll(async () => { patient = await factory.build('patient:subscriber') patientRepository.findByLogin.mockResolvedValue(patient) }) it('writes cache with subscriber true', async () => { await subject.createPatientCache(patient) expect(setCacheSpy).toBeCalledWith(`patient:${patient.login}`, expect.objectContaining({ subscriber: true })) }) }) describe('when patient is not a subscriber', () => { beforeAll(() => { patient.subscription = null patientRepository.findByLogin.mockResolvedValue(patient) }) it('writes cache with subscriber false', async () => { await subject.createPatientCache(patient) expect(setCacheSpy).toBeCalledWith(`patient:${patient.login}`, expect.objectContaining({ subscriber: false })) }) }) }) }) })