|
| 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 | +}) |
0 commit comments