Skip to content

Commit 7387456

Browse files
committed
#RI-3401 - add unit tests
1 parent 27a6f53 commit 7387456

File tree

23 files changed

+919
-62
lines changed

23 files changed

+919
-62
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ module.exports = {
5454
],
5555
coverageThreshold: {
5656
global: {
57-
statements: 70,
58-
branches: 50,
59-
functions: 60,
60-
lines: 72,
57+
statements: 77,
58+
branches: 55,
59+
functions: 65,
60+
lines: 75,
6161
},
6262
// './redisinsight/ui/src/slices/**/*.ts': {
6363
// statements: 90,
Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,49 @@
1-
import * as React from 'react';
1+
import React, { useEffect } from 'react';
22

33
export default function MonacoEditor(props) {
4-
return <div {...props} data-testid="monaco"/>;
4+
useEffect(() => {
5+
props.editorDidMount && props.editorDidMount(
6+
// editor
7+
{
8+
addCommand: jest.fn(),
9+
getContribution: jest.fn(),
10+
onKeyDown: jest.fn(),
11+
onMouseDown: jest.fn(),
12+
addAction: jest.fn(),
13+
getAction: jest.fn(),
14+
deltaDecorations: jest.fn(),
15+
createContextKey: jest.fn(),
16+
focus: jest.fn(),
17+
onDidChangeCursorPosition: jest.fn(),
18+
executeEdits: jest.fn()
19+
},
20+
// monaco
21+
{
22+
Range: jest.fn().mockImplementation(() => { return {} }),
23+
languages: {
24+
getLanguages: jest.fn(),
25+
register: jest.fn(),
26+
registerCompletionItemProvider: jest.fn().mockReturnValue({
27+
dispose: jest.fn()
28+
}),
29+
registerSignatureHelpProvider: jest.fn().mockReturnValue({
30+
dispose: jest.fn()
31+
}),
32+
setLanguageConfiguration: jest.fn(),
33+
setMonarchTokensProvider: jest.fn(),
34+
},
35+
KeyMod: {},
36+
KeyCode: {}
37+
})
38+
}, [])
39+
return <input {...props} data-testid="monaco"/>;
40+
}
41+
42+
export const languages = {
43+
CompletionItemKind: {
44+
Function: 1
45+
},
46+
CompletionItemInsertTextRule: {
47+
InsertAsSnippet: 4
48+
}
549
}

redisinsight/ui/src/components/query-card/QueryCardCliPlugin/QueryCardCliPlugin.spec.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { cloneDeep } from 'lodash'
22
import React from 'react'
33
import { instance, mock } from 'ts-mockito'
4+
import { PluginEvents } from 'uiSrc/plugins/pluginEvents'
5+
import { pluginApi } from 'uiSrc/services/PluginAPI'
46
import { cleanup, mockedStore, render } from 'uiSrc/utils/test-utils'
57
import QueryCardCliPlugin, { Props } from './QueryCardCliPlugin'
68

@@ -13,6 +15,28 @@ beforeEach(() => {
1315
store.clearActions()
1416
})
1517

18+
jest.mock('uiSrc/services/PluginAPI', () => ({
19+
pluginApi: {
20+
onEvent: jest.fn()
21+
}
22+
}))
23+
24+
jest.mock('uiSrc/slices/app/plugins', () => ({
25+
...jest.requireActual('uiSrc/slices/app/plugins'),
26+
appPluginsSelector: jest.fn().mockReturnValue({
27+
visualizations: [
28+
{
29+
id: '1',
30+
uniqId: '1',
31+
name: 'test',
32+
plugin: '',
33+
activationMethod: 'render',
34+
matchCommands: ['*'],
35+
}
36+
]
37+
}),
38+
}))
39+
1640
jest.mock('uiSrc/services', () => ({
1741
...jest.requireActual('uiSrc/services'),
1842
sessionStorageService: {
@@ -25,4 +49,20 @@ describe('QueryCardCliPlugin', () => {
2549
it('should render', () => {
2650
expect(render(<QueryCardCliPlugin {...instance(mockedProps)} />)).toBeTruthy()
2751
})
52+
53+
it('should subscribes on events', () => {
54+
const onEventMock = jest.fn();
55+
56+
(pluginApi.onEvent as jest.Mock).mockImplementation(onEventMock)
57+
58+
render(<QueryCardCliPlugin {...instance(mockedProps)} id="1" />)
59+
60+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.heightChanged, expect.any(Function))
61+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.loaded, expect.any(Function))
62+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.error, expect.any(Function))
63+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.setHeaderText, expect.any(Function))
64+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.executeRedisCommand, expect.any(Function))
65+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.getState, expect.any(Function))
66+
expect(onEventMock).toBeCalledWith(expect.any(String), PluginEvents.setState, expect.any(Function))
67+
})
2868
})

redisinsight/ui/src/components/query/Query/Query.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ const Query = (props: Props) => {
7373
query = '',
7474
activeMode,
7575
resultsMode,
76-
setQuery,
77-
onKeyDown,
78-
onSubmit,
79-
setQueryEl,
80-
setIsCodeBtnDisabled = () => { },
81-
onQueryChangeMode,
82-
onChangeGroupMode
76+
setQuery = () => {},
77+
onKeyDown = () => {},
78+
onSubmit = () => {},
79+
setQueryEl = () => {},
80+
setIsCodeBtnDisabled = () => {},
81+
onQueryChangeMode = () => {},
82+
onChangeGroupMode = () => {}
8383
} = props
8484
let contribution: Nullable<ISnippetController> = null
8585
const [isDedicatedEditorOpen, setIsDedicatedEditorOpen] = useState(false)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
3+
import { ITableColumn } from 'uiSrc/components/virtual-table/interfaces'
34
import { render } from 'uiSrc/utils/test-utils'
45
import VirtualGrid from './VirtualGrid'
56
import { IProps } from './interfaces'
67

78
const mockedProps = mock<IProps>()
89

10+
const columns: ITableColumn[] = [
11+
{
12+
id: 'name',
13+
label: 'Member',
14+
staySearchAlwaysOpen: true,
15+
initialSearchValue: '',
16+
truncateText: true,
17+
minWidth: 50
18+
},
19+
]
20+
21+
const items = ['member1', 'member2']
22+
923
describe('VirtualGrid', () => {
1024
it('should render', () => {
1125
expect(
1226
render(<VirtualGrid {...instance(mockedProps)} />)
1327
).toBeTruthy()
1428
})
29+
30+
it('should render rows', () => {
31+
expect(
32+
render(
33+
<VirtualGrid
34+
{...instance(mockedProps)}
35+
items={items}
36+
columns={columns}
37+
loading={false}
38+
loadMoreItems={jest.fn()}
39+
totalItemsCount={items.length}
40+
/>
41+
)
42+
).toBeTruthy()
43+
})
1544
})

redisinsight/ui/src/pages/browser/components/stream-details/messages-view/MessageAckPopover/MessageAckPopover.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface Props {
1212
}
1313

1414
const AckPopover = (props: Props) => {
15-
const { id, isOpen, closePopover, showPopover, acknowledge } = props
15+
const { id, isOpen, closePopover = () => {}, showPopover = () => {}, acknowledge = () => {} } = props
1616
return (
1717
<EuiPopover
1818
key={id}

redisinsight/ui/src/pages/home/components/SearchDatabasesList/SearchDatabasesList.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('SearchDatabasesList', () => {
5454
expect(render(<SearchDatabasesList />)).toBeTruthy()
5555
})
5656

57-
it.skip('should call loadInstancesSuccess with after typing', async () => {
57+
it('should call loadInstancesSuccess with after typing', async () => {
5858
const state: RootState = store.getState();
5959
(useSelector as jest.Mock).mockImplementation((callback: (arg0: RootState) => RootState) => callback({
6060
...state,
@@ -79,9 +79,10 @@ describe('SearchDatabasesList', () => {
7979
)
8080
})
8181

82+
newInstancesMock[0].visible = false
8283
newInstancesMock[1].visible = false
8384

8485
const expectedActions = [loadInstancesSuccess(newInstancesMock)]
85-
expect(storeMock.getActions()).toEqual(expect.arrayContaining(expectedActions))
86+
expect(storeMock.getActions()).toEqual(expectedActions)
8687
})
8788
})

redisinsight/ui/src/pages/redisCloudSubscriptions/RedisCloudSubscriptionsPage.spec.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import React from 'react'
2+
import { cloudSelector } from 'uiSrc/slices/instances/cloud'
23
import { render } from 'uiSrc/utils/test-utils'
34

45
import RedisCloudSubscriptionsPage from './RedisCloudSubscriptionsPage'
56

7+
jest.mock('uiSrc/slices/instances/cloud', () => ({
8+
...jest.requireActual('uiSrc/slices/instances/cloud'),
9+
cloudSelector: jest.fn().mockReturnValue({
10+
...jest.requireActual('uiSrc/slices/instances/cloud').initialState
11+
})
12+
}))
13+
614
/**
715
* RedisCloudSubscriptionsPage tests
816
*
@@ -12,4 +20,26 @@ describe('RedisCloudSubscriptionsPage', () => {
1220
it('should render', () => {
1321
expect(render(<RedisCloudSubscriptionsPage />)).toBeTruthy()
1422
})
23+
24+
it('should render with subscriptions', () => {
25+
const cloudSelectorMock = jest.fn().mockReturnValue({
26+
credentials: null,
27+
loading: false,
28+
error: '',
29+
loaded: { instances: false },
30+
account: { error: '', data: [] },
31+
subscriptions: [
32+
{
33+
id: 123,
34+
name: 'name',
35+
numberOfDatabases: 123,
36+
provider: 'provider',
37+
region: 'region',
38+
status: 'active',
39+
},
40+
]
41+
})
42+
cloudSelector.mockImplementation(cloudSelectorMock)
43+
expect(render(<RedisCloudSubscriptionsPage />)).toBeTruthy()
44+
})
1545
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import Router from 'uiSrc/Router'
3+
import { render } from 'uiSrc/utils/test-utils'
4+
import RouteWithSubRoutes from 'uiSrc/utils/routerWithSubRoutes'
5+
import ROUTES from 'uiSrc/components/main-router/constants/commonRoutes'
6+
7+
import ProtectedRoute from './ProtectedRoute'
8+
9+
describe('ProtectedRoute', () => {
10+
it('should render', () => {
11+
expect(render(
12+
<Router>
13+
<ProtectedRoute>
14+
<RouteWithSubRoutes
15+
key={1}
16+
{...ROUTES[0]}
17+
/>
18+
</ProtectedRoute>
19+
</Router>
20+
))
21+
.toBeTruthy()
22+
})
23+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react'
2+
import HtmlToJsxString from '../formatter/HtmlToJsxString'
3+
4+
describe('HtmlToJsxString', () => {
5+
it('should return proper string', async () => {
6+
const div = (<div />)
7+
const formatter = new HtmlToJsxString()
8+
const result = await formatter.format(div)
9+
expect(result).toEqual(String(div))
10+
})
11+
})

0 commit comments

Comments
 (0)