Skip to content

Commit e51d437

Browse files
Properly msw server to return success response for unhandled requests temporary
Fix store issue in RediSearchIndexesList.spec.tsx
1 parent 6fba2c6 commit e51d437

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
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 () => HttpResponse.json({}, { status: 200 })),
13+
),
14+
)

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

redisinsight/ui/src/setup-tests.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,8 @@ Object.defineProperty(window, 'File', {
4141
})
4242

4343
beforeAll(() => {
44-
// mswServer.listen()
4544
mswServer.listen({
46-
onUnhandledRequest: ((req: any, res: any, ctx: any) => {
47-
const url = req.url.href
48-
const method = req.method
49-
const testName = expect.getState()?.currentTestName ?? 'unknown test'
50-
51-
// Log it nicely
52-
console.warn(`[MSW][${testName}] Unhandled request: ${method} ${url}`)
53-
54-
// throw to fail the test???
55-
// tmp: just return empty object to fix libuv error.
56-
// todo: need to find all unhandled requests and probably throw right from here and close socket
57-
return res(
58-
ctx.status(200),
59-
ctx.json({})
60-
)
61-
}) as any,
45+
onUnhandledRequest: 'bypass'
6246
})
6347
})
6448

0 commit comments

Comments
 (0)