|
| 1 | +import React from 'react' |
| 2 | +import { instance, mock } from 'ts-mockito' |
| 3 | +import { act } from '@testing-library/react' |
| 4 | +import { fireEvent, render, screen } from 'uiSrc/utils/test-utils' |
| 5 | +import { |
| 6 | + DEFAULT_TIMEOUT, |
| 7 | + SubmitBtnText, |
| 8 | +} from 'uiSrc/pages/home/constants' |
| 9 | +import ManualConnectionFrom, { Props as ManualConnectionFromProps } from |
| 10 | + 'uiSrc/pages/home/components/manual-connection/manual-connection-form/ManualConnectionForm' |
| 11 | +import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry' |
| 12 | +import SentinelConnectionWrapper from 'uiSrc/pages/home/components/sentinel-connection' |
| 13 | +import ManualConnectionWrapper, { |
| 14 | + Props, |
| 15 | +} from './ManualConnectionWrapper' |
| 16 | + |
| 17 | +const mockedProps = mock<Props>() |
| 18 | + |
| 19 | +jest.mock('./manual-connection-form/ManualConnectionForm', () => ({ |
| 20 | + __esModule: true, |
| 21 | + namedExport: jest.fn(), |
| 22 | + default: jest.fn(), |
| 23 | +})) |
| 24 | + |
| 25 | +jest.mock('uiSrc/telemetry', () => ({ |
| 26 | + ...jest.requireActual('uiSrc/telemetry'), |
| 27 | + sendEventTelemetry: jest.fn(), |
| 28 | +})) |
| 29 | + |
| 30 | +const mockManualConnectionFrom = (props: ManualConnectionFromProps) => ( |
| 31 | + <div> |
| 32 | + <button |
| 33 | + type="button" |
| 34 | + onClick={() => props.onHostNamePaste('redis-12000.cluster.local:12000')} |
| 35 | + data-testid="onHostNamePaste-btn" |
| 36 | + > |
| 37 | + onHostNamePaste |
| 38 | + </button> |
| 39 | + <button |
| 40 | + type="button" |
| 41 | + onClick={() => props.onClose()} |
| 42 | + data-testid="onClose-btn" |
| 43 | + > |
| 44 | + onClose |
| 45 | + </button> |
| 46 | + <button |
| 47 | + type="submit" |
| 48 | + data-testid="btn-submit" |
| 49 | + onClick={() => props.onSubmit({})} |
| 50 | + > |
| 51 | + {props.submitButtonText} |
| 52 | + </button> |
| 53 | + <button |
| 54 | + type="button" |
| 55 | + onClick={() => props.setIsCloneMode(!props.isCloneMode)} |
| 56 | + data-testid="onClone-btn" |
| 57 | + > |
| 58 | + onClone |
| 59 | + </button> |
| 60 | + </div> |
| 61 | +) |
| 62 | + |
| 63 | +describe('ManualConnectionWrapper', () => { |
| 64 | + beforeAll(() => { |
| 65 | + ManualConnectionFrom.mockImplementation(mockManualConnectionFrom) |
| 66 | + }) |
| 67 | + it('should render', () => { |
| 68 | + expect( |
| 69 | + render(<ManualConnectionWrapper {...instance(mockedProps)} />) |
| 70 | + ).toBeTruthy() |
| 71 | + }) |
| 72 | + |
| 73 | + it('should call onHostNamePaste', () => { |
| 74 | + const component = render(<ManualConnectionWrapper {...instance(mockedProps)} />) |
| 75 | + fireEvent.click(screen.getByTestId('onHostNamePaste-btn')) |
| 76 | + expect(component).toBeTruthy() |
| 77 | + }) |
| 78 | + |
| 79 | + it('should call onClose', () => { |
| 80 | + const onClose = jest.fn() |
| 81 | + render(<ManualConnectionWrapper {...instance(mockedProps)} onClose={onClose} />) |
| 82 | + fireEvent.click(screen.getByTestId('onClose-btn')) |
| 83 | + expect(onClose).toBeCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('should have add database submit button', () => { |
| 87 | + render(<ManualConnectionWrapper {...instance(mockedProps)} />) |
| 88 | + expect(screen.getByTestId('btn-submit')).toHaveTextContent(SubmitBtnText.AddDatabase) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should have edit database submit button', () => { |
| 92 | + render(<ManualConnectionWrapper {...instance(mockedProps)} editMode />) |
| 93 | + expect(screen.getByTestId('btn-submit')).toHaveTextContent(SubmitBtnText.EditDatabase) |
| 94 | + }) |
| 95 | + |
| 96 | + it('should have edit database submit button', () => { |
| 97 | + render(<ManualConnectionWrapper {...instance(mockedProps)} editMode />) |
| 98 | + act(() => { |
| 99 | + fireEvent.click(screen.getByTestId('onClone-btn')) |
| 100 | + }) |
| 101 | + expect(screen.getByTestId('btn-submit')).toHaveTextContent(SubmitBtnText.CloneDatabase) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should call proper telemetry event on Add database', () => { |
| 105 | + const sendEventTelemetryMock = jest.fn() |
| 106 | + |
| 107 | + sendEventTelemetry.mockImplementation(() => sendEventTelemetryMock) |
| 108 | + |
| 109 | + sendEventTelemetry.mockRestore() |
| 110 | + render(<ManualConnectionWrapper {...instance(mockedProps)} />) |
| 111 | + act(() => { |
| 112 | + fireEvent.click(screen.getByTestId('btn-submit')) |
| 113 | + }) |
| 114 | + |
| 115 | + expect(sendEventTelemetry).toBeCalledWith({ |
| 116 | + event: TelemetryEvent.CONFIG_DATABASES_MANUALLY_SUBMITTED, |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + it('should call proper telemetry event on Clone database', () => { |
| 121 | + const sendEventTelemetryMock = jest.fn() |
| 122 | + |
| 123 | + sendEventTelemetry.mockImplementation(() => sendEventTelemetryMock) |
| 124 | + |
| 125 | + sendEventTelemetry.mockRestore() |
| 126 | + render(<ManualConnectionWrapper {...instance(mockedProps)} editMode />) |
| 127 | + act(() => { |
| 128 | + fireEvent.click(screen.getByTestId('onClone-btn')) |
| 129 | + }) |
| 130 | + act(() => { |
| 131 | + fireEvent.click(screen.getByTestId('onClose-btn')) |
| 132 | + }) |
| 133 | + |
| 134 | + expect(sendEventTelemetry).toBeCalledWith({ |
| 135 | + event: TelemetryEvent.CONFIG_DATABASES_DATABASE_CLONE_CANCELLED, |
| 136 | + eventData: { databaseId: undefined } |
| 137 | + }) |
| 138 | + }) |
| 139 | +}) |
0 commit comments