Skip to content

Commit bfb1487

Browse files
#RI-5009 - add tests
1 parent 370b69f commit bfb1487

File tree

4 files changed

+179
-5
lines changed

4 files changed

+179
-5
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
})

redisinsight/ui/src/pages/home/components/sentinel-connection/SentinelConnectionWrapper.spec.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { instance, mock } from 'ts-mockito'
33
import { fireEvent, render, screen } from 'uiSrc/utils/test-utils'
44
import SentinelConnectionForm, { Props as SentinelConnectionFormProps } from
55
'uiSrc/pages/home/components/sentinel-connection/sentinel-connection-form/SentinelConnectionForm'
6+
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
67
import SentinelConnectionWrapper, {
78
Props,
89
} from './SentinelConnectionWrapper'
@@ -15,6 +16,11 @@ jest.mock('./sentinel-connection-form/SentinelConnectionForm', () => ({
1516
default: jest.fn(),
1617
}))
1718

19+
jest.mock('uiSrc/telemetry', () => ({
20+
...jest.requireActual('uiSrc/telemetry'),
21+
sendEventTelemetry: jest.fn(),
22+
}))
23+
1824
const mockSentinelConnectionForm = (props: SentinelConnectionFormProps) => (
1925
<div>
2026
<button
@@ -26,7 +32,7 @@ const mockSentinelConnectionForm = (props: SentinelConnectionFormProps) => (
2632
</button>
2733
<button
2834
type="button"
29-
onClick={() => props.onSubmit()}
35+
onClick={() => props.onSubmit({})}
3036
data-testid="onSubmit-btn"
3137
>
3238
onSubmit
@@ -63,4 +69,20 @@ describe('SentinelConnectionWrapper', () => {
6369
fireEvent.click(screen.getByTestId('onClose-btn'))
6470
expect(onClose).toBeCalled()
6571
})
72+
73+
it('Should call proper telemetry event', () => {
74+
const sendEventTelemetryMock = jest.fn()
75+
76+
sendEventTelemetry.mockImplementation(() => sendEventTelemetryMock)
77+
78+
render(<SentinelConnectionWrapper {...instance(mockedProps)} />)
79+
80+
fireEvent.click(screen.getByTestId('onSubmit-btn'))
81+
82+
expect(sendEventTelemetry).toBeCalledWith({
83+
event: TelemetryEvent.CONFIG_DATABASES_REDIS_SENTINEL_AUTODISCOVERY_SUBMITTED,
84+
})
85+
86+
sendEventTelemetry.mockRestore()
87+
})
6688
})
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
3-
import { render } from 'uiSrc/utils/test-utils'
3+
import { act, fireEvent, render, screen } from 'uiSrc/utils/test-utils'
44
import SentinelConnectionForm, { Props } from './SentinelConnectionForm'
55

66
const mockedProps = mock<Props>()
77

8+
const mockValues = {
9+
host: 'host',
10+
port: '123'
11+
}
12+
813
describe('SentinelConnectionForm', () => {
914
it('should render', () => {
1015
expect(
1116
render(<SentinelConnectionForm {...instance(mockedProps)} />)
1217
).toBeTruthy()
1318
})
19+
20+
it('should call submit form on press Enter', async () => {
21+
const mockSubmit = jest.fn()
22+
render(<SentinelConnectionForm {...instance(mockedProps)} onSubmit={mockSubmit} initialValues={mockValues} />)
23+
24+
await act(() => {
25+
fireEvent.keyDown(screen.getByTestId('form'), { key: 'Enter', code: 13, charCode: 13 })
26+
})
27+
28+
expect(mockSubmit).toBeCalled()
29+
})
1430
})

redisinsight/ui/src/pages/home/components/sentinel-connection/sentinel-connection-form/SentinelConnectionForm.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import ReactDOM from 'react-dom'
1313

1414
import validationErrors from 'uiSrc/constants/validationErrors'
1515
import { useResizableFormField } from 'uiSrc/services'
16-
1716
import {
1817
fieldDisplayNames,
19-
SubmitBtnText,
2018
} from 'uiSrc/pages/home/constants'
2119
import { getFormErrors, getSubmitButtonContent } from 'uiSrc/pages/home/utils'
2220
import { DbConnectionInfo, ISubmitButton, IPasswordType } from 'uiSrc/pages/home/interfaces'
@@ -28,7 +26,6 @@ import {
2826

2927
export interface Props {
3028
width: number
31-
submitButtonText?: SubmitBtnText
3229
loading: boolean
3330
initialValues: DbConnectionInfo
3431
certificates: { id: string; name: string }[],

0 commit comments

Comments
 (0)