/* eslint-disable @typescript-eslint/no-empty-function */ import { render, fireEvent } from '@@/test/kalimotxo-test-utils' import CallMe from './CallMe.vue' import * as CreateLeadService from '@@/src/infrastructure/create-lead' import { default as kaliEnvironment } from '@@/src/ui/utils/environment' describe('CallMe', () => { beforeEach(() => { jest.clearAllMocks() ;(kaliEnvironment as jest.Mocked).env = 'staging' }) afterEach(() => { jest.clearAllMocks() }) test('Renders with default label', () => { const { input } = renderComponent() expect(input).toBeInTheDocument() }) test('Does not create lead if privacy policy is not accepted', async () => { const spy = jest.spyOn(CreateLeadService, 'createLead') const { input } = renderComponent() await fireEvent.update(input, '666666666') expect(spy).not.toHaveBeenCalled() }) test('Creates lead if privacy policy is accepted', async () => { const spy = jest.spyOn(CreateLeadService, 'createLead') const { input, legalCheckbox, button } = renderComponent() await fireEvent.update(input, '666666666') await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect(spy).toHaveBeenCalledTimes(1) }) test('Feedback is shown on ok submit form', async () => { jest.spyOn(CreateLeadService, 'createLead').mockResolvedValue(undefined) const { input, legalCheckbox, button, findByText } = renderComponent() await fireEvent.update(input, '666666666') await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect( await findByText('¡Recibido! Ahora mismo te llamamos'), ).toBeInTheDocument() }) test.each([ ['empty', ''], ['short', '666'], ['long', '6666666666'], ['invalid', '66A666666'], ])('Create lead is not called for %s phone number', async (_, number) => { const spy = jest.spyOn(CreateLeadService, 'createLead') const { input, button, legalCheckbox } = renderComponent() await fireEvent.update(input, number) await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect(spy).not.toHaveBeenCalled() }) test.each([ ['regular', '666666666'], ['with spaces', '666 666 666'], ])('Create lead is called with a %s phone number', async (_, number) => { const spy = jest.spyOn(CreateLeadService, 'createLead') const { input, button, legalCheckbox } = renderComponent() await fireEvent.update(input, number) await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect(spy).toHaveBeenCalledTimes(1) }) test.each` isWorkTime | isCallMeBack ${{ electricity: true }} | ${false} ${{ electricity: false }} | ${true} `( 'createPhoneLead is called with isCallMeBack $isCallMeBack with isWorkTime $isWorkTime', async ({ isWorkTime, isCallMeBack }) => { const spy = jest.spyOn(CreateLeadService, 'createLead') const { input, button, legalCheckbox } = renderComponent({ isWorkTime, }) await fireEvent.update(input, '666666666') await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect(spy).toHaveBeenCalledWith( expect.objectContaining({ isCallMeBack }), ) }, ) test('Emit event when button is clicked', async () => { const { input, button, legalCheckbox, emitted } = renderComponent() await fireEvent.update(input, '666666666') await fireEvent.click(legalCheckbox) await fireEvent.click(button) expect(emitted()).toHaveProperty('submitted') expect(emitted().submitted).toHaveLength(1) }) }) function renderComponent({ isWorkTime = { electricity: true } } = {}) { const view = render(CallMe, { props: { eventLabel: 'irrelevant', eventCategory: 'irrelevant', }, mocks: { $isWorkTime: Promise.resolve(isWorkTime), }, }) const input = view.queryByLabelText( 'Si lo prefieres, te llamamos gratis', ) as HTMLInputElement const button = view.queryByRole('button') as HTMLButtonElement const legalCheckbox = view.getByText(/acepto recibir/i) as HTMLInputElement return { input, button, legalCheckbox, ...view } }