import { Test, TestingModule } from '@nestjs/testing' import { internet, datatype } from 'faker' import { plainToClass } from 'class-transformer' import { LanguageCountryCode } from '../../../utils/countries' import { PatientRepository, CampaignMessageRepository } from '../../../repositories' import { BioimpedanceImportation, Patient } from '../../../models' import { MockedModule } from '../../../../test/types' import { factory } from '../../../../test/factories' import { NotificationService } from '../notification.service' import { AwsService } from '../../aws/aws.service' import { TranslateNotificationServiceHelper } from '../i18n' import { BioimpedanceNotificationService } from './bioimpedance_notification.service' describe('BioimpedanceNotificationService', () => { let subject: BioimpedanceNotificationService let campaignMessageRepository: MockedModule let patientRepository: MockedModule let notificationService: MockedModule let awsService: MockedModule const translateNotificationServiceHelper = new TranslateNotificationServiceHelper() beforeAll(async () => { notificationService = { prepareMessage: jest.fn(), } awsService = { sendSQS: jest.fn(), } campaignMessageRepository = { scheduleCheckPushConversion: jest.fn(), } patientRepository = { findByLogin: jest.fn(), getMany: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [], providers: [ { provide: NotificationService, useValue: notificationService, }, { provide: AwsService, useValue: awsService, }, { provide: CampaignMessageRepository, useValue: campaignMessageRepository, }, { provide: PatientRepository, useValue: patientRepository, }, { provide: TranslateNotificationServiceHelper, useValue: translateNotificationServiceHelper, }, BioimpedanceNotificationService, ], }).compile() subject = app.get(BioimpedanceNotificationService) }) afterEach(() => { jest.clearAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#sendNotificatonForBioImports', () => { let countryCode = LanguageCountryCode.PT_BR let rowsInserted = [ plainToClass(BioimpedanceImportation, { id: datatype.uuid(), document_number: '1', email: internet.email(), country_code: LanguageCountryCode.PT_BR, }), plainToClass(BioimpedanceImportation, { id: datatype.uuid(), document_number: '2', email: internet.email(), country_code: LanguageCountryCode.PT_BR, }), plainToClass(BioimpedanceImportation, { id: datatype.uuid(), document_number: '3', email: internet.email(), country_code: LanguageCountryCode.PT_BR, }), ] let sendSpy: jest.SpyInstance let patient: Patient beforeAll(async () => { patient = await factory.build('patient') sendSpy = jest.spyOn(subject, 'send') }) afterEach(() => { sendSpy.mockClear() }) afterAll(() => { sendSpy.mockRestore() }) beforeEach(() => { patientRepository.findByLogin.mockResolvedValue(patient) sendSpy.mockResolvedValue(null) }) describe('when patient exists', () => { it('send notification', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) rowsInserted.forEach(() => expect(sendSpy).toHaveBeenCalledWith(patient)) }) describe('and at least one has a device id', () => { beforeAll(() => { patientRepository.findByLogin .mockResolvedValueOnce({ ...patient, device_id: null }) .mockResolvedValue(patient) }) it('checks push conversion', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) expect(campaignMessageRepository.scheduleCheckPushConversion).toHaveBeenCalledWith([patient.id, patient.id]) }) }) describe('and hasn`t a device id', () => { beforeAll(() => { patient.device_id = null }) it('doesn`t checks push conversion', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) expect(campaignMessageRepository.scheduleCheckPushConversion).not.toHaveBeenCalled() }) }) }) describe('when patient not exists', () => { beforeAll(() => { patient = null }) it('sends a notification', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) rowsInserted.forEach(value => expect(sendSpy).toHaveBeenCalledWith( plainToClass(Patient, { ...value, country_code: countryCode, id: null }), ), ) }) }) describe('when is es_DO country code', () => { beforeAll(async () => { countryCode = LanguageCountryCode.ES_DO patient = await factory.build('patient', { country_code: countryCode }) rowsInserted = rowsInserted.map(row => { row['country_code'] = countryCode return row }) }) describe('when patient exists', () => { it('send notification', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) rowsInserted.forEach(value => expect(sendSpy).toHaveBeenCalledWith(patient, value.id, 'bio_importation_email'), ) }) }) describe('when patient not exists', () => { beforeAll(() => { patient = null }) it('sends a notification', async () => { await subject.sendNotificatonForBioImports(rowsInserted, countryCode) rowsInserted.forEach(value => expect(sendSpy).toHaveBeenCalledWith( plainToClass(Patient, { ...value, country_code: countryCode, id: null }), value.id, 'bio_importation_email', ), ) }) }) }) }) })