Skip to content

Commit 129548a

Browse files
committed
fix store reference
1 parent 8640a2b commit 129548a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
/* eslint-disable global-require */
21
import type { RootState, AppDispatch } from './store'
32

4-
// Importing store methods dynamically to avoid circular dependencies
3+
// Lazy reference to avoid circular dependencies
4+
// The store will be set by the store module itself after it's created
5+
let storeRef: { getState: () => RootState; dispatch: AppDispatch } | null = null
6+
7+
// This function will be called by the store modules to set the reference
8+
export const setStoreRef = (store: { getState: () => RootState; dispatch: AppDispatch }) => {
9+
storeRef = store
10+
}
511

612
const getState = (): RootState => {
7-
const { store } = require('uiSrc/slices/store')
8-
return store.getState() as RootState
13+
if (!storeRef) {
14+
throw new Error('Store not initialized. Make sure store-dynamic is imported after store creation.')
15+
}
16+
return storeRef.getState()
917
}
1018

1119
const dispatch: AppDispatch = (action: any) => {
12-
const { store } = require('uiSrc/slices/store')
13-
return store.dispatch(action)
20+
if (!storeRef) {
21+
throw new Error('Store not initialized. Make sure store-dynamic is imported after store creation.')
22+
}
23+
return storeRef.dispatch(action)
1424
}
1525

16-
const store = {
26+
export const store = {
1727
getState,
1828
dispatch,
1929
}
2030

21-
export { store }
22-
23-
export { RootState, AppDispatch }
31+
export type { RootState, AppDispatch }

redisinsight/ui/src/slices/store.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import rdiStatisticsReducer from './rdi/statistics'
5656
import aiAssistantReducer from './panels/aiAssistant'
5757
import appDbSettingsReducer from './app/db-settings'
5858
import tagsReducer from './instances/tags'
59+
import { setStoreRef } from './store-dynamic'
5960

6061
const riConfig = getConfig()
6162

@@ -148,6 +149,7 @@ const store = configureStore({
148149
getDefaultMiddleware({ serializableCheck: false }),
149150
devTools: riConfig.app.env !== 'production',
150151
})
152+
setStoreRef(store)
151153

152154
export { store }
153155

redisinsight/ui/src/utils/test-utils.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { ThemeProvider } from 'styled-components'
1616
import { themeLight } from '@redis-ui/styles'
1717
import userEvent from '@testing-library/user-event'
1818
import type { RootState, ReduxStore } from 'uiSrc/slices/store'
19+
import { setStoreRef } from 'uiSrc/slices/store-dynamic'
1920
import { initialState as initialStateInstances } from 'uiSrc/slices/instances/instances'
2021
import { initialState as initialStateTags } from 'uiSrc/slices/instances/tags'
2122
import { initialState as initialStateCaCerts } from 'uiSrc/slices/instances/caCerts'
@@ -168,6 +169,10 @@ export const mockStore = configureMockStore<RootState>([thunk])
168169
export const mockedStore = mockStore(initialStateDefault)
169170
export const mockedStoreFn = () => mockStore(initialStateDefault)
170171

172+
// Set the mock store reference for the dynamic store wrapper
173+
// This ensures that store-dynamic works correctly in tests
174+
setStoreRef(mockedStore as any)
175+
171176
// insert root state to the render Component
172177
const render = (
173178
ui: JSX.Element,
@@ -178,6 +183,11 @@ const render = (
178183
...renderOptions
179184
}: Options = initialStateDefault,
180185
) => {
186+
// Set the store reference for the dynamic store wrapper if a custom store is provided
187+
if (store !== mockedStore) {
188+
setStoreRef(store as any)
189+
}
190+
181191
const Wrapper = ({ children }: { children: JSX.Element }) => (
182192
<ThemeProvider theme={themeLight}>
183193
<Provider store={store}>{children}</Provider>
@@ -198,6 +208,11 @@ const renderHook = (
198208
...renderOptions
199209
}: Options = initialStateDefault,
200210
) => {
211+
// Set the store reference for the dynamic store wrapper if a custom store is provided
212+
if (store !== mockedStore) {
213+
setStoreRef(store as any)
214+
}
215+
201216
const Wrapper = ({ children }: { children: JSX.Element }) => (
202217
<Provider store={store}>{children}</Provider>
203218
)

0 commit comments

Comments
 (0)