Skip to content

Commit 9d8a087

Browse files
Fix tests (#5119)
* Properly msw server to return success response for unhandled requests temporary Fix store issue in RediSearchIndexesList.spec.tsx * fix the rest tests related to store initialization issue
1 parent 2d7b6a4 commit 9d8a087

File tree

11 files changed

+162
-147
lines changed

11 files changed

+162
-147
lines changed

redisinsight/ui/src/components/global-url-handler/GlobalUrlHandler.spec.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React from 'react'
2-
import { cloneDeep } from 'lodash'
32
import reactRouterDom from 'react-router-dom'
4-
import { act, cleanup, mockedStore, render } from 'uiSrc/utils/test-utils'
3+
import {
4+
act,
5+
cleanup,
6+
createMockedStore,
7+
mockedStore,
8+
render,
9+
} from 'uiSrc/utils/test-utils'
510

611
import {
712
appRedirectionSelector,
@@ -45,13 +50,15 @@ jest.mock('uiSrc/utils/routing', () => ({
4550
let store: typeof mockedStore
4651
beforeEach(() => {
4752
cleanup()
48-
store = cloneDeep(mockedStore)
53+
store = createMockedStore()
4954
store.clearActions()
5055
})
5156

5257
const fromUrl =
5358
'redisinsight://databases/connect?redisUrl=redis://default:password@localhost:6379&databaseAlias=My Name&redirect=workbench?guidePath=/quick-guides/document/introduction.md&cloudBdbId=1232&subscriptionType=fixed&planMemoryLimit=30&memoryLimitMeasurementUnit=mb&free=true&target=_blank'
5459

60+
const renderGlobalUrlHandler = () => render(<GlobalUrlHandler />, { store })
61+
5562
describe('GlobalUrlHandler', () => {
5663
beforeEach(() => {
5764
reactRouterDom.useLocation = jest
@@ -60,11 +67,11 @@ describe('GlobalUrlHandler', () => {
6067
})
6168

6269
it('should render', () => {
63-
expect(render(<GlobalUrlHandler />)).toBeTruthy()
70+
expect(renderGlobalUrlHandler()).toBeTruthy()
6471
})
6572

6673
it('should not call any actions by default', () => {
67-
render(<GlobalUrlHandler />)
74+
renderGlobalUrlHandler()
6875
expect(store.getActions()).toEqual([])
6976
})
7077

@@ -77,7 +84,7 @@ describe('GlobalUrlHandler', () => {
7784
.mockReturnValueOnce({ replace: replaceMock })
7885
reactRouterDom.useLocation = jest.fn().mockReturnValueOnce({ search })
7986

80-
render(<GlobalUrlHandler />)
87+
renderGlobalUrlHandler()
8188
expect(store.getActions()).toEqual([
8289
setFromUrl(decodeURIComponent(fromUrl)),
8390
])
@@ -99,7 +106,7 @@ describe('GlobalUrlHandler', () => {
99106
;(appRedirectionSelector as jest.Mock).mockReturnValueOnce({ fromUrl })
100107

101108
await act(async () => {
102-
render(<GlobalUrlHandler />)
109+
renderGlobalUrlHandler()
103110
})
104111

105112
const actionUrl = new URL(fromUrl)
@@ -130,7 +137,7 @@ describe('GlobalUrlHandler', () => {
130137
;(appRedirectionSelector as jest.Mock).mockReturnValueOnce({ fromUrl })
131138

132139
await act(async () => {
133-
render(<GlobalUrlHandler />)
140+
renderGlobalUrlHandler()
134141
})
135142

136143
const actionUrl = new URL(fromUrl)
@@ -158,9 +165,7 @@ describe('GlobalUrlHandler', () => {
158165
fromUrl,
159166
})
160167

161-
await act(() => {
162-
render(<GlobalUrlHandler />)
163-
})
168+
renderGlobalUrlHandler()
164169

165170
const actionUrl = new URL(fromUrl)
166171
const fromParams = new URLSearchParams(actionUrl.search)
@@ -194,7 +199,7 @@ describe('GlobalUrlHandler', () => {
194199
})
195200

196201
await act(() => {
197-
render(<GlobalUrlHandler />)
202+
renderGlobalUrlHandler()
198203
})
199204

200205
const actionUrl = new URL(url)
@@ -246,7 +251,7 @@ describe('GlobalUrlHandler', () => {
246251
})
247252

248253
await act(() => {
249-
render(<GlobalUrlHandler />)
254+
renderGlobalUrlHandler()
250255
})
251256

252257
const actionUrl = new URL(url)
@@ -296,7 +301,7 @@ describe('GlobalUrlHandler', () => {
296301
})
297302

298303
await act(() => {
299-
render(<GlobalUrlHandler />)
304+
renderGlobalUrlHandler()
300305
})
301306

302307
const actionUrl = new URL(url)
@@ -347,7 +352,7 @@ describe('GlobalUrlHandler', () => {
347352
})
348353

349354
await act(() => {
350-
render(<GlobalUrlHandler />)
355+
renderGlobalUrlHandler()
351356
})
352357

353358
const actionUrl = new URL(url)
@@ -398,7 +403,7 @@ describe('GlobalUrlHandler', () => {
398403
})
399404

400405
await act(() => {
401-
render(<GlobalUrlHandler />)
406+
renderGlobalUrlHandler()
402407
})
403408

404409
const actionUrl = new URL(url)

redisinsight/ui/src/components/side-panels/panels/live-time-recommendations/components/recommendation/Recommendation.spec.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from 'uiSrc/utils/test-utils'
1616
import { sendEventTelemetry, TelemetryEvent } from 'uiSrc/telemetry'
1717

18-
import { updateRecommendation } from 'uiSrc/slices/recommendations/recommendations'
18+
import {updateRecommendation, updateRecommendationSuccess} from 'uiSrc/slices/recommendations/recommendations'
1919
import { INSTANCE_ID_MOCK } from 'uiSrc/mocks/handlers/instances/instancesHandlers'
2020
import { MOCK_RECOMMENDATIONS } from 'uiSrc/constants/mocks/mock-recommendations'
2121
import { findTutorialPath } from 'uiSrc/utils'
@@ -214,7 +214,7 @@ describe('Recommendation', () => {
214214
)
215215
})
216216

217-
const expectedActions = [updateRecommendation()]
217+
const expectedActions = [updateRecommendation(), updateRecommendationSuccess({})]
218218

219219
expect(store.getActions()).toEqual(expectedActions)
220220
expect(screen.getByTestId('toggle-hide-searchJSON-btn')).toBeInTheDocument()

redisinsight/ui/src/electron/components/ConfigOAuth/ConfigOAuth.spec.tsx

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import React from 'react'
2-
import { cloneDeep } from 'lodash'
3-
import { cleanup, mockedStore, render } from 'uiSrc/utils/test-utils'
2+
import {
3+
cleanup,
4+
createMockedStore,
5+
mockedStore,
6+
render,
7+
} from 'uiSrc/utils/test-utils'
48

59
import {
610
CloudAuthStatus,
@@ -48,13 +52,17 @@ jest.mock('uiSrc/slices/instances/cloud', () => ({
4852
let store: typeof mockedStore
4953
beforeEach(() => {
5054
cleanup()
51-
store = cloneDeep(mockedStore)
55+
store = createMockedStore()
5256
store.clearActions()
5357
window.app = {
5458
cloudOauthCallback: jest.fn(),
5559
} as any
5660
})
5761

62+
const renderConfigOAuth = () => {
63+
return render(<ConfigOAuth />, { store })
64+
}
65+
5866
describe('ConfigOAuth', () => {
5967
it('should render', () => {
6068
expect(render(<ConfigOAuth />)).toBeTruthy()
@@ -68,7 +76,7 @@ describe('ConfigOAuth', () => {
6876
window.app?.cloudOauthCallback.mockImplementation((cb: any) =>
6977
cb(undefined, { status: CloudAuthStatus.Succeed }),
7078
)
71-
render(<ConfigOAuth />)
79+
renderConfigOAuth()
7280

7381
const expectedActions = [
7482
setJob({
@@ -95,7 +103,7 @@ describe('ConfigOAuth', () => {
95103
error: 'error',
96104
}),
97105
)
98-
render(<ConfigOAuth />)
106+
renderConfigOAuth()
99107

100108
const expectedActions = [
101109
setOAuthCloudSource(null),
@@ -127,7 +135,7 @@ describe('ConfigOAuth', () => {
127135
window.app?.cloudOauthCallback.mockImplementation((cb: any) =>
128136
cb(undefined, { status: CloudAuthStatus.Succeed }),
129137
)
130-
render(<ConfigOAuth />)
138+
renderConfigOAuth()
131139

132140
const afterCallbackActions = [
133141
setJob({
@@ -165,7 +173,7 @@ describe('ConfigOAuth', () => {
165173
window.app?.cloudOauthCallback.mockImplementation((cb: any) =>
166174
cb(undefined, { status: CloudAuthStatus.Succeed }),
167175
)
168-
render(<ConfigOAuth />)
176+
renderConfigOAuth()
169177

170178
const afterCallbackActions = [
171179
setJob({
@@ -204,7 +212,7 @@ describe('ConfigOAuth', () => {
204212
window.app?.cloudOauthCallback.mockImplementation((cb: any) =>
205213
cb(undefined, { status: CloudAuthStatus.Succeed }),
206214
)
207-
render(<ConfigOAuth />)
215+
renderConfigOAuth()
208216

209217
const afterCallbackActions = [
210218
setJob({
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { setupServer } from 'msw/node'
2+
import { http, HttpResponse } from 'msw'
23
import { handlers } from './handlers'
34

45
// Setup requests interception using the given handlers.
5-
export const mswServer = setupServer(...handlers)
6+
export const mswServer = setupServer(
7+
...handlers,
8+
http.all(
9+
'*',
10+
jest
11+
.fn()
12+
.mockImplementation(async ({ request }) => {
13+
console.warn(`[MSW] Unhandled request: ${request.method} ${request.url}`)
14+
return HttpResponse.json({}, { status: 200 })
15+
}),
16+
),
17+
)

redisinsight/ui/src/pages/browser/components/redisearch-key-list/RediSearchIndexesList.spec.tsx

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cloneDeep } from 'lodash'
1+
import { merge } from 'lodash'
22
import React from 'react'
33
import { instance, mock } from 'ts-mockito'
44
import { 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'
1517
import {
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'
2528
import { changeSearchMode, fetchKeys } from 'uiSrc/slices/browser/keys'
2629
import { BrowserStorageItem } from 'uiSrc/constants'
2730
import 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

2935
let store: typeof mockedStore
3036
beforeEach(() => {
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+
7798
describe('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

Comments
 (0)