import { handleCreatePhoneLead } from '../createPhoneLead' import * as CreateLeadService from '@@/src/infrastructure/create-lead' import { default as kaliEnvironment } from '@@/src/ui/utils/environment' jest.mock('@@/src/ui/utils/environment') const interactorCallbacks = { respondWithSuccess: jest.fn(), respondWithError: jest.fn(), } describe('handleCreatePhoneLead', () => { beforeEach(() => { jest.clearAllMocks() ;(kaliEnvironment as jest.Mocked).env = 'staging' }) afterEach(() => { jest.clearAllMocks() }) test('responds with success', async () => { const spy = jest .spyOn(CreateLeadService, 'createLead') .mockResolvedValue(undefined) const payload = { phone: '666666666', url: 'test', isCallMeBack: false, businessUnit: 'electricity', } await handleCreatePhoneLead().execute(payload, interactorCallbacks) expect(spy).toHaveBeenCalledTimes(1) expect(interactorCallbacks.respondWithError).toHaveBeenCalledTimes(0) expect(interactorCallbacks.respondWithSuccess).toHaveBeenCalledTimes(1) }) test('responds error', async () => { const spy = jest .spyOn(CreateLeadService, 'createLead') .mockRejectedValue({ response: { status: 500 } }) const payload = { phone: '666666666', url: 'localhost', isCallMeBack: false, businessUnit: 'electricity', } await handleCreatePhoneLead().execute(payload, interactorCallbacks) expect(spy).toHaveBeenCalledTimes(1) expect(interactorCallbacks.respondWithSuccess).toHaveBeenCalledTimes(0) expect(interactorCallbacks.respondWithError).toHaveBeenCalledTimes(1) }) })