Skip to content

Commit ca02933

Browse files
committed
#RI-31 - Logical databases
1 parent 6577d79 commit ca02933

File tree

4 files changed

+211
-0
lines changed

4 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import React from 'react'
2+
import { mock } from 'ts-mockito'
3+
import { Mock } from 'vitest'
4+
5+
import { KeyTypes, SelectedKeyActionType } from 'uiSrc/constants'
6+
import * as utils from 'uiSrc/utils'
7+
import { apiService, vscodeApi } from 'uiSrc/services'
8+
import * as useContext from 'uiSrc/store/hooks/use-context/useContext'
9+
import * as useSelectedKeyStore from 'uiSrc/store/hooks/use-selected-key-store/useSelectedKeyStore'
10+
import { Database } from 'uiSrc/store'
11+
import { constants, fireEvent, render, waitForStack } from 'testSrc/helpers'
12+
import { LogicalDatabaseWrapper, Props } from './LogicalDatabaseWrapper'
13+
import * as useKeys from '../../hooks/useKeys'
14+
15+
const mockDatabase = constants.DATABASE
16+
const mockedProps = {
17+
...mock<Props>(<div />),
18+
database: mockDatabase,
19+
}
20+
beforeEach(() => {
21+
apiService.get = vi.fn().mockResolvedValue({ status: 200, data: {} })
22+
})
23+
24+
vi.spyOn(utils, 'sendEventTelemetry')
25+
vi.spyOn(vscodeApi, 'postMessage')
26+
27+
const fnMock = vi.fn()
28+
const addKeyIntoTreeMock = vi.fn()
29+
const deleteKeyFromTreeMock = vi.fn();
30+
(vi.spyOn(useKeys, 'useKeysApi') as Mock).mockImplementation(() => ({
31+
fetchPatternKeysAction: fnMock,
32+
setDatabaseId: fnMock,
33+
setDatabaseIndex: fnMock,
34+
addKeyIntoTree: addKeyIntoTreeMock,
35+
deleteKeyFromTree: deleteKeyFromTreeMock,
36+
}))
37+
const setKeysTreeSortMock = vi.fn()
38+
const resetKeysTreeMock = vi.fn();
39+
(vi.spyOn(useContext, 'useContextApi') as Mock).mockImplementation(() => ({
40+
setKeysTreeSort: setKeysTreeSortMock,
41+
resetKeysTree: resetKeysTreeMock,
42+
}))
43+
44+
describe('LogicalDatabaseWrapper', () => {
45+
it('should render', () => {
46+
expect(render(<LogicalDatabaseWrapper {...mockedProps} />)).toBeTruthy()
47+
})
48+
49+
describe('selectedKeyAction', () => {
50+
const setSelectedKeyActionMock = vi.fn()
51+
const setSelectedKeyMock = vi.fn()
52+
const spySelectedKey = vi.spyOn(useSelectedKeyStore, 'useSelectedKeyStore') as Mock
53+
54+
const selectedKeyAction = {
55+
type: SelectedKeyActionType.Removed,
56+
database: {
57+
id: constants.DATABASE_ID,
58+
},
59+
keyInfo: {
60+
key: constants.KEY_NAME_1,
61+
keyType: KeyTypes.Hash,
62+
},
63+
}
64+
65+
spySelectedKey.mockImplementation(() => ({
66+
setSelectedKeyAction: setSelectedKeyActionMock,
67+
setSelectedKey: setSelectedKeyMock,
68+
selectedKeyAction,
69+
}))
70+
71+
afterEach(() => {
72+
vi.restoreAllMocks()
73+
})
74+
75+
it('should call deleteKeyFromTree and setSelectedKeyAction action after if selected key action is Removed', async () => {
76+
render(<LogicalDatabaseWrapper {...mockedProps} database={{ id: constants.DATABASE_ID } as Database} />)
77+
78+
await waitForStack()
79+
80+
expect(setSelectedKeyActionMock).toBeCalledWith(null)
81+
expect(deleteKeyFromTreeMock).toBeCalledWith(constants.KEY_NAME_1)
82+
})
83+
84+
it('should not call any mocks if database is not equal', async () => {
85+
render(<LogicalDatabaseWrapper {...mockedProps} database={{ id: '123123' } as Database} />)
86+
87+
await waitForStack()
88+
89+
expect(setSelectedKeyActionMock).not.toBeCalled()
90+
expect(deleteKeyFromTreeMock).not.toBeCalled()
91+
expect(addKeyIntoTreeMock).not.toBeCalled()
92+
})
93+
94+
it('should not call any mocks if type is not defined', async () => {
95+
render(<LogicalDatabaseWrapper {...mockedProps} database={{ id: constants.DATABASE_ID } as Database} />)
96+
97+
await waitForStack()
98+
99+
expect(setSelectedKeyActionMock).not.toBeCalled()
100+
expect(deleteKeyFromTreeMock).not.toBeCalled()
101+
expect(addKeyIntoTreeMock).not.toBeCalled()
102+
})
103+
104+
it('should call addKeyIntoTree action after if selected key action is Added', async () => {
105+
const spySelectedKey = vi.spyOn(useSelectedKeyStore, 'useSelectedKeyStore') as Mock
106+
107+
const setSelectedKeyActionMock = vi.fn()
108+
const setSelectedKeyMock = vi.fn()
109+
110+
spySelectedKey.mockImplementation(() => ({
111+
selectedKeyAction: {
112+
...selectedKeyAction,
113+
type: SelectedKeyActionType.Added,
114+
},
115+
setSelectedKeyAction: setSelectedKeyActionMock,
116+
setSelectedKey: setSelectedKeyMock,
117+
}))
118+
119+
render(<LogicalDatabaseWrapper {...mockedProps} database={{ id: constants.DATABASE_ID } as Database} />)
120+
121+
expect(setSelectedKeyMock).toBeCalledWith({ name: constants.KEY_NAME_1 })
122+
expect(setSelectedKeyActionMock).toBeCalledWith(null)
123+
expect(deleteKeyFromTreeMock).not.toBeCalled()
124+
expect(addKeyIntoTreeMock).not.toBeCalled()
125+
})
126+
})
127+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import React, { useEffect, useState } from 'react'
2+
import cx from 'classnames'
3+
import { useShallow } from 'zustand/react/shallow'
4+
5+
import { SelectedKeyActionType } from 'uiSrc/constants'
6+
import { Database, useSelectedKeyStore } from 'uiSrc/store'
7+
import { useKeysApi } from '../../hooks/useKeys'
8+
9+
export interface Props {
10+
database: Database
11+
dbIndex?: number
12+
children: React.ReactNode
13+
}
14+
15+
export const LogicalDatabaseWrapper = ({ children, database }: Props) => {
16+
const { id: dbId, db: dbIndex } = database
17+
const { selectedKeyAction, setSelectedKeyAction, setSelectedKey } = useSelectedKeyStore(useShallow((state) => ({
18+
selectedKeyAction: state.action,
19+
setSelectedKeyAction: state.setSelectedKeyAction,
20+
setSelectedKey: state.processSelectedKeySuccess,
21+
})))
22+
23+
const [renderChildren, setRenderChildren] = useState<boolean>(false)
24+
25+
const keysApi = useKeysApi()
26+
27+
useEffect(() => {
28+
keysApi.setDatabaseId(dbId)
29+
keysApi.setDatabaseIndex(dbIndex ?? 0)
30+
31+
setRenderChildren(true)
32+
}, [])
33+
34+
useEffect(() => {
35+
const { type, keyInfo, database: databaseAction } = selectedKeyAction || {}
36+
const { key, keyType, newKey } = keyInfo || {}
37+
const { id: databaseId, db: databaseIndex } = databaseAction || {}
38+
39+
if (!type || (databaseId! + databaseIndex !== dbId + dbIndex)) {
40+
return
41+
}
42+
43+
switch (type) {
44+
case SelectedKeyActionType.Added:
45+
keysApi.addKeyIntoTree(key!, keyType!)
46+
setSelectedKey({ name: key! })
47+
break
48+
case SelectedKeyActionType.Removed:
49+
keysApi.deleteKeyFromTree(key!)
50+
break
51+
case SelectedKeyActionType.Renamed:
52+
keysApi.editKeyName(key!, newKey!)
53+
break
54+
default:
55+
break
56+
}
57+
setSelectedKeyAction(null)
58+
}, [selectedKeyAction])
59+
60+
return (
61+
<div
62+
className={cx('flex w-full flex-col')}
63+
data-testid={`logical-database-${dbId}-${dbIndex}`}
64+
>
65+
{renderChildren && children}
66+
</div>
67+
)
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { LogicalDatabaseWrapper } from './LogicalDatabaseWrapper'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.databaseNameWrapper {
2+
@apply flex pl-3.5 cursor-pointer text-ellipsis whitespace-nowrap overflow-hidden items-center;
3+
}
4+
5+
.databaseName {
6+
@apply text-ellipsis whitespace-nowrap overflow-hidden flex items-center;
7+
}
8+
9+
.icon {
10+
@apply mr-2 w-[14px] h-[14px] min-w-[14px] ml-[3px];
11+
12+
&Nested {
13+
@apply m-0;
14+
}
15+
}

0 commit comments

Comments
 (0)