|
| 1 | +import React from 'react' |
| 2 | +import { useFormikContext } from 'formik' |
| 3 | +import { render, fireEvent, screen } from 'uiSrc/utils/test-utils' |
| 4 | +import { MOCK_RDI_PIPELINE_DATA } from 'uiSrc/mocks/data/rdi' |
| 5 | +import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry' |
| 6 | +import ConfirmLeavePagePopup, { Props } from './ConfirmLeavePagePopup' |
| 7 | + |
| 8 | +const mockProps: Props = { |
| 9 | + onClose: jest.fn(), |
| 10 | + onConfirm: jest.fn() |
| 11 | +} |
| 12 | + |
| 13 | +jest.mock('formik') |
| 14 | + |
| 15 | +jest.mock('uiSrc/telemetry', () => ({ |
| 16 | + ...jest.requireActual('uiSrc/telemetry'), |
| 17 | + sendEventTelemetry: jest.fn() |
| 18 | +})) |
| 19 | + |
| 20 | +describe('ConfirmLeavePagePopup', () => { |
| 21 | + beforeEach(() => { |
| 22 | + const mockUseFormikContext = { |
| 23 | + setFieldValue: jest.fn, |
| 24 | + values: MOCK_RDI_PIPELINE_DATA, |
| 25 | + }; |
| 26 | + (useFormikContext as jest.Mock).mockReturnValue(mockUseFormikContext) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should render', () => { |
| 30 | + expect(render(<ConfirmLeavePagePopup {...mockProps} />)).toBeTruthy() |
| 31 | + }) |
| 32 | + |
| 33 | + it('should call proper telemetry event', async () => { |
| 34 | + const sendEventTelemetryMock = jest.fn(); |
| 35 | + (sendEventTelemetry as jest.Mock).mockImplementation(() => sendEventTelemetryMock) |
| 36 | + |
| 37 | + render(<ConfirmLeavePagePopup {...mockProps} />) |
| 38 | + |
| 39 | + expect(sendEventTelemetry).toBeCalledWith({ |
| 40 | + event: TelemetryEvent.RDI_UNSAVED_CHANGES_MESSAGE_DISPLAYED, |
| 41 | + eventData: { |
| 42 | + id: 'rdiInstanceId', |
| 43 | + } |
| 44 | + }); |
| 45 | + (sendEventTelemetry as jest.Mock).mockRestore() |
| 46 | + }) |
| 47 | + |
| 48 | + it('should call onConfirm', async () => { |
| 49 | + const onConfirmMock = jest.fn() |
| 50 | + render(<ConfirmLeavePagePopup {...mockProps} onConfirm={onConfirmMock} />) |
| 51 | + |
| 52 | + fireEvent.click(screen.getByTestId('confirm-leave-page')) |
| 53 | + |
| 54 | + expect(onConfirmMock).toBeCalled() |
| 55 | + }) |
| 56 | +}) |
0 commit comments