Skip to content

Commit a3487bc

Browse files
authored
Merge pull request #4052 from RedisInsight/fe/bugfix/RI-6292-edit-rdi-name-fix
RI-6292 edit connected rdi instance fix
2 parents af973d3 + 15800e9 commit a3487bc

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

redisinsight/ui/src/slices/rdi/instances.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ const instancesSlice = createSlice({
128128
// reset connected instance
129129
resetConnectedInstance: (state) => {
130130
state.connectedInstance = initialState.connectedInstance
131+
},
132+
133+
updateConnectedInstance: (state, { payload }: { payload: RdiInstance }) => {
134+
state.connectedInstance = { ...state.connectedInstance, ...payload }
131135
}
132136
}
133137
})
@@ -151,7 +155,8 @@ export const {
151155
setConnectedInstance,
152156
setConnectedInstanceSuccess,
153157
setConnectedInstanceFailure,
154-
resetConnectedInstance
158+
resetConnectedInstance,
159+
updateConnectedInstance,
155160
} = instancesSlice.actions
156161

157162
// selectors
@@ -213,7 +218,7 @@ export function editInstanceAction(
213218
payload: Partial<RdiInstance>,
214219
onSuccess?: (data: RdiInstanceResponse) => void
215220
) {
216-
return async (dispatch: AppDispatch) => {
221+
return async (dispatch: AppDispatch, stateInit: () => RootState) => {
217222
dispatch(defaultInstanceChanging())
218223

219224
try {
@@ -226,6 +231,11 @@ export function editInstanceAction(
226231
dispatch(defaultInstanceChangingSuccess())
227232
dispatch(fetchInstancesAction())
228233

234+
const state = stateInit()
235+
if (state.rdi.instances.connectedInstance?.id === data.id) {
236+
dispatch(updateConnectedInstance(data))
237+
}
238+
229239
onSuccess?.(data)
230240
}
231241
} catch (_err) {

redisinsight/ui/src/slices/tests/rdi/instances.spec.ts

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import reducer, {
1313
instancesSelector,
1414
fetchConnectedInstanceAction,
1515
checkConnectToRdiInstanceAction,
16-
} from 'uiSrc/slices/rdi/instances'
16+
editInstanceAction,
17+
defaultInstanceChanging,
18+
defaultInstanceChangingSuccess,
19+
defaultInstanceChangingFailure,
20+
updateConnectedInstance } from 'uiSrc/slices/rdi/instances'
1721
import { apiService } from 'uiSrc/services'
1822
import { addErrorNotification, IAddInstanceErrorPayload } from 'uiSrc/slices/app/notifications'
1923
import { RdiInstance } from 'uiSrc/slices/interfaces'
24+
import { Rdi } from 'apiSrc/modules/rdi/models'
2025

2126
let store: typeof mockedStore
2227

@@ -160,6 +165,31 @@ describe('rdi instances slice', () => {
160165
})
161166
})
162167

168+
describe('updateConnectedInstance', () => {
169+
it('should properly update connected instance', () => {
170+
// Arrange
171+
const state = {
172+
...initialState,
173+
connectedInstance: {
174+
...initialState.connectedInstance,
175+
...mockRdiInstance,
176+
}
177+
}
178+
179+
// Act
180+
const nextState = reducer(initialState, updateConnectedInstance(mockRdiInstance as Rdi))
181+
182+
// Assert
183+
const rootState = Object.assign(initialStateDefault, {
184+
rdi: {
185+
instances: nextState
186+
},
187+
})
188+
189+
expect(instancesSelector(rootState)).toEqual(state)
190+
})
191+
})
192+
163193
// thunks
164194

165195
describe('thunks', () => {
@@ -210,6 +240,55 @@ describe('rdi instances slice', () => {
210240
})
211241
})
212242

243+
describe('editInstanceAction', () => {
244+
it('succeed to edit data and calls a success callback', async () => {
245+
const onSuccess = jest.fn()
246+
const responsePayload = { data: mockRdiInstance, status: 200 }
247+
248+
apiService.patch = jest.fn().mockResolvedValue(responsePayload)
249+
250+
// Act
251+
await store.dispatch<any>(
252+
editInstanceAction('123', mockRdiInstance, onSuccess)
253+
)
254+
255+
// Assert
256+
const expectedActions = [
257+
defaultInstanceChanging(),
258+
defaultInstanceChangingSuccess(),
259+
]
260+
261+
expect(store.getActions()).toEqual(expect.arrayContaining(expectedActions))
262+
expect(onSuccess).toBeCalledWith(mockRdiInstance)
263+
})
264+
265+
it('failed to edit data', async () => {
266+
const errorMessage = 'Something was wrong!'
267+
const responsePayload = {
268+
response: {
269+
status: 500,
270+
data: { message: errorMessage },
271+
},
272+
}
273+
274+
apiService.patch = jest.fn().mockRejectedValue(responsePayload)
275+
276+
// Act
277+
await store.dispatch<any>(
278+
editInstanceAction('123', mockRdiInstance)
279+
)
280+
281+
// Assert
282+
const expectedActions = [
283+
defaultInstanceChanging(),
284+
defaultInstanceChangingFailure(errorMessage),
285+
addErrorNotification(responsePayload as AxiosError),
286+
]
287+
288+
expect(store.getActions()).toEqual(expectedActions)
289+
})
290+
})
291+
213292
describe('checkConnectToRdiInstanceAction', () => {
214293
it('succeed to fetch data', async () => {
215294
const responsePayload = { status: 200 }

0 commit comments

Comments
 (0)