import { Test, TestingModule } from '@nestjs/testing' import { internet, phone, datatype } from 'faker' import { subYears } from 'date-fns' import { Accounts } from '../domain/accounts' import { Credits } from '../domain/credits' import { AuthTokenResult, Patient } from '../../../models' import { OS } from '../../../params' import { PatientService } from '../../../services' import { PatientsController } from './patients.controller' describe('PatientsController', () => { let spy: any let patientsController: PatientsController const email = internet.email() const cellPhone = phone.phoneNumber() const resultToken = 'y412y2' const document_number = '87979789899' const authTokenResult = { email: email, cellPhone: cellPhone, } const account = { sendTokenToPatient: async (_login: string): Promise => authTokenResult, createPatientAndSendToken: async (_patient: Patient): Promise => authTokenResult, validatePatientToken: async (_login: string, _token: string): Promise => resultToken, } const credits = { finishPendingCredits: async (_patient: Patient): Promise => undefined, } const patientService = {} beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [PatientsController], providers: [ { provide: Accounts, useValue: account, }, { provide: Credits, useValue: credits, }, { provide: PatientService, useValue: patientService, }, ], }).compile() patientsController = app.get(PatientsController) }) describe('.sendToken', () => { const input = { login: document_number, app_signature: datatype.uuid() } it("returns the user's email and cellphone", async () => { await expect(patientsController.sendToken({ input }, { os: OS.ANDROID, version: null })).resolves.toStrictEqual({ email: email, cellPhone: cellPhone, }) }) it('call accounts with correct params', async () => { spy = jest.spyOn(account, 'sendTokenToPatient') await patientsController.sendToken({ input }, { os: OS.ANDROID, version: null }) expect(spy).toHaveBeenCalledWith(document_number, { os: OS.ANDROID, appSignature: input.app_signature }) }) }) describe('.signUp', () => { const input = { patient: { document_number, birth_date: subYears(new Date(), 20), email: email, name: 'John', cell_phone: cellPhone, }, app_signature: datatype.uuid(), } it("returns the user's email and cellphone", async () => { await expect(patientsController.signUp({ input }, { os: OS.ANDROID, version: null })).resolves.toStrictEqual({ email: email, cellPhone: cellPhone, }) }) it('call accounts with correct params', async () => { spy = jest.spyOn(account, 'createPatientAndSendToken') await patientsController.signUp({ input }, { os: OS.ANDROID, version: null }) expect(spy).toHaveBeenCalledWith(expect.objectContaining({ email }), { os: OS.ANDROID, appSignature: input.app_signature, }) }) }) describe('.validateToken', () => { const login = document_number const token = '79799' const input = { login, token } it("returns the user's token", async () => { await expect(patientsController.validateToken({ input })).resolves.toStrictEqual({ token: resultToken, }) }) it('call accounts with correct params', async () => { spy = jest.spyOn(account, 'validatePatientToken') await patientsController.validateToken({ input }) expect(spy).toHaveBeenCalledWith(login, token) }) }) })