1- import { cloneDeep } from 'lodash'
1+ import { merge } from 'lodash'
22import React from 'react'
33import { instance , mock } from 'ts-mockito'
44import { useSelector } from 'react-redux'
@@ -7,13 +7,16 @@ import {
77 cleanup ,
88 clearStoreActions ,
99 fireEvent ,
10+ initialStateDefault ,
1011 mockedStore ,
12+ mockStore ,
1113 render ,
1214 screen ,
1315 userEvent ,
1416} from 'uiSrc/utils/test-utils'
1517import {
1618 loadList ,
19+ loadListSuccess ,
1720 redisearchListSelector ,
1821 setSelectedIndex ,
1922} from 'uiSrc/slices/browser/redisearch'
@@ -25,11 +28,25 @@ import { connectedInstanceSelector } from 'uiSrc/slices/instances/instances'
2528import { changeSearchMode , fetchKeys } from 'uiSrc/slices/browser/keys'
2629import { BrowserStorageItem } from 'uiSrc/constants'
2730import RediSearchIndexesList , { Props } from './RediSearchIndexesList'
31+ import { INSTANCE_ID_MOCK } from 'uiSrc/mocks/handlers/instances/instancesHandlers'
32+ import { setStoreRef } from 'uiSrc/utils/test-store'
33+ import { REDISEARCH_LIST_DATA_MOCK } from 'uiSrc/mocks/handlers/browser/redisearchHandlers'
2834
2935let store : typeof mockedStore
3036beforeEach ( ( ) => {
3137 cleanup ( )
32- store = cloneDeep ( mockedStore )
38+ store = mockStore (
39+ merge ( { } , initialStateDefault , {
40+ connections : {
41+ instances : {
42+ connectedInstance : {
43+ id : INSTANCE_ID_MOCK ,
44+ } ,
45+ } ,
46+ } ,
47+ } ) ,
48+ )
49+ setStoreRef ( store )
3350 store . clearActions ( )
3451} )
3552
@@ -74,6 +91,10 @@ jest.mock('uiSrc/services', () => ({
7491 } ,
7592} ) )
7693
94+ const renderRediSearchIndexesList = ( props : Props ) => {
95+ return render ( < RediSearchIndexesList { ...props } /> , { store } )
96+ }
97+
7798describe ( 'RediSearchIndexesList' , ( ) => {
7899 beforeEach ( ( ) => {
79100 const state : any = store . getState ( )
@@ -109,9 +130,7 @@ describe('RediSearchIndexesList', () => {
109130 } )
110131
111132 it ( 'should render' , ( ) => {
112- expect (
113- render ( < RediSearchIndexesList { ...instance ( mockedProps ) } /> ) ,
114- ) . toBeTruthy ( )
133+ expect ( renderRediSearchIndexesList ( instance ( mockedProps ) ) ) . toBeTruthy ( )
115134 const searchInput = screen . getByTestId ( 'select-search-mode' )
116135 expect ( searchInput ) . toBeInTheDocument ( )
117136 } )
@@ -123,9 +142,7 @@ describe('RediSearchIndexesList', () => {
123142 modules : [ ] ,
124143 } ) )
125144
126- expect (
127- render ( < RediSearchIndexesList { ...instance ( mockedProps ) } /> ) ,
128- ) . toBeTruthy ( )
145+ expect ( renderRediSearchIndexesList ( instance ( mockedProps ) ) ) . toBeTruthy ( )
129146
130147 const expectedActions = [
131148 changeSearchMode ( SearchMode . Pattern ) ,
@@ -143,9 +160,7 @@ describe('RediSearchIndexesList', () => {
143160 } )
144161
145162 it ( '"loadList" should be called after render' , ( ) => {
146- const { rerender } = render (
147- < RediSearchIndexesList { ...instance ( mockedProps ) } /> ,
148- )
163+ const { rerender } = renderRediSearchIndexesList ( instance ( mockedProps ) )
149164
150165 ; ( connectedInstanceSelector as jest . Mock ) . mockImplementation ( ( ) => ( {
151166 host : '123.23.1.1' ,
@@ -162,12 +177,10 @@ describe('RediSearchIndexesList', () => {
162177
163178 it ( '"onCreateIndex" should be called after click Create Index' , async ( ) => {
164179 const onCreateIndexMock = jest . fn ( )
165- const { findByText } = render (
166- < RediSearchIndexesList
167- { ...instance ( mockedProps ) }
168- onCreateIndex = { onCreateIndexMock }
169- /> ,
170- )
180+ const { findByText } = renderRediSearchIndexesList ( {
181+ ...instance ( mockedProps ) ,
182+ onCreateIndex : onCreateIndexMock ,
183+ } )
171184
172185 await userEvent . click ( screen . getByTestId ( 'select-search-mode' ) )
173186 await userEvent . click ( ( await findByText ( 'Create Index' ) ) || document )
@@ -187,9 +200,7 @@ describe('RediSearchIndexesList', () => {
187200 selectedIndex : null ,
188201 } )
189202
190- const { queryByText } = render (
191- < RediSearchIndexesList { ...instance ( mockedProps ) } /> ,
192- )
203+ const { queryByText } = renderRediSearchIndexesList ( instance ( mockedProps ) )
193204
194205 ; ( connectedInstanceSelector as jest . Mock ) . mockImplementation ( ( ) => ( {
195206 host : '123.123.1.1' ,
@@ -199,7 +210,11 @@ describe('RediSearchIndexesList', () => {
199210 await userEvent . click ( screen . getByTestId ( 'select-search-mode' ) )
200211 await userEvent . click ( queryByText ( bufferToString ( index ) ) || document )
201212
202- const expectedActions = [ setSelectedIndex ( index ) , loadList ( ) ]
213+ const expectedActions = [
214+ setSelectedIndex ( index ) ,
215+ loadList ( ) ,
216+ loadListSuccess ( REDISEARCH_LIST_DATA_MOCK . indexes ) ,
217+ ]
203218
204219 expect ( clearStoreActions ( store . getActions ( ) ) ) . toEqual (
205220 clearStoreActions ( expectedActions ) ,
@@ -214,7 +229,7 @@ describe('RediSearchIndexesList', () => {
214229 modules : [ { name : RedisDefaultModules . Search } ] ,
215230 } ) )
216231
217- render ( < RediSearchIndexesList { ... instance ( mockedProps ) } /> )
232+ renderRediSearchIndexesList ( instance ( mockedProps ) )
218233
219234 const afterRenderActions = [ ...store . getActions ( ) ]
220235
0 commit comments