import { Test, TestingModule } from '@nestjs/testing' import { datatype, random } from 'faker' import { pick } from 'lodash' import { factory } from '../../../../test/factories' import { MockedModule } from '../../../../test/types' import { Countries, MeasurementTotem, Nutritionist, Plans, TotemPatient, TOTEM_PATIENT_PROPERTIES } from '../domain' import { Patient } from '../../../models' import { MeasurementTotemController } from './measurement-totem.controller' describe('MeasurementTotemController', () => { let measurementTotemController: MeasurementTotemController let measurementTotem: MockedModule let nutritionist: MockedModule let countries: MockedModule let plans: MockedModule beforeEach(async () => { measurementTotem = { getPatientAndCredits: jest.fn(), getSmartFitUnitTeachers: jest.fn(), } nutritionist = { getNutritionists: jest.fn(), } countries = { getCountries: jest.fn(), } plans = { getPlans: jest.fn(), subscribe: jest.fn(), confirmAndSubscribe: jest.fn(), } const app: TestingModule = await Test.createTestingModule({ controllers: [MeasurementTotemController], providers: [ { provide: MeasurementTotem, useValue: measurementTotem, }, { provide: Nutritionist, useValue: nutritionist, }, { provide: Countries, useValue: countries, }, { provide: Plans, useValue: plans, }, ], }).compile() measurementTotemController = app.get(MeasurementTotemController) }) afterEach(() => { jest.resetAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#getPatient', () => { let patient: Patient let totemPatient: TotemPatient let bioimpedance_credit: number beforeAll(async () => { patient = await factory.build('patient') totemPatient = { ...pick(patient, TOTEM_PATIENT_PROPERTIES), gender: patient.genderInitial, subscriber: patient.subscriber, } as TotemPatient bioimpedance_credit = datatype.number() }) beforeEach(() => { measurementTotem.getPatientAndCredits.mockResolvedValue({ patient: totemPatient, bioimpedance_credit }) }) it('calls to get patient with clean document number', async () => { await measurementTotemController.getPatient({ login: '000.000.000-00', unit_acronym: random.word() }) expect(measurementTotem.getPatientAndCredits).toHaveBeenCalledWith('00000000000', false) }) it('returns TotemPatient type data', async () => { await expect( measurementTotemController.getPatient({ login: '000.000.000-00', unit_acronym: random.word() }), ).resolves.toEqual({ bioimpedance_credit, patient: totemPatient, }) }) }) describe('#subscribe', () => { it('calls to subscribe with clean document number', async () => { await measurementTotemController.subscribe({ login: '000.000.000-00', unit_acronym: random.word() }) expect(plans.subscribe).toHaveBeenCalledWith('00000000000') }) }) describe('#confirmSubscription', () => { const email = datatype.uuid() const plan_id = datatype.uuid() beforeEach(() => { plans.confirmAndSubscribe.mockResolvedValue({ confirmed: true }) }) it('calls to confirm subscription with clean document number and token', async () => { await measurementTotemController.confirmSubscription({ login: '000.000.000-00', email, plan_id }) expect(plans.confirmAndSubscribe).toHaveBeenCalledWith( expect.objectContaining({ login: '00000000000', email, plan_id }), ) }) it('returns confirmed with true value', async () => { expect(await measurementTotemController.confirmSubscription({ login: datatype.uuid(), email, plan_id })).toEqual( expect.objectContaining({ confirmed: true, }), ) }) }) describe('#getSmartfitUnitTeachers', () => { const id = `${datatype.number({ min: 0 })}` it('calls to get SmartFit unit teachers with number id', async () => { await measurementTotemController.getSmartfitUnitTeachers({ id }) expect(measurementTotem.getSmartFitUnitTeachers).toHaveBeenCalledWith(+id) }) }) })