|
1 | 1 | import React from 'react'
|
2 |
| -import { cloneDeep } from 'lodash' |
3 | 2 | import { Provider } from 'react-redux'
|
| 3 | +import { rest } from 'msw' |
| 4 | +import { configureStore, combineReducers } from '@reduxjs/toolkit' |
| 5 | +import { mswServer } from 'uiSrc/mocks/server' |
4 | 6 | import {
|
5 | 7 | cleanup,
|
6 | 8 | render,
|
7 | 9 | screen,
|
8 |
| - mockStore, |
9 | 10 | initialStateDefault,
|
10 | 11 | userEvent,
|
| 12 | + getMswURL, |
11 | 13 | } from 'uiSrc/utils/test-utils'
|
12 | 14 | import { INSTANCE_ID_MOCK } from 'uiSrc/mocks/handlers/instances/instancesHandlers'
|
13 | 15 | import { MOCK_REDISEARCH_INDEX_INFO } from 'uiSrc/mocks/data/redisearch'
|
| 16 | +import Notifications from 'uiSrc/components/notifications' |
| 17 | +import { TelemetryEvent, sendEventTelemetry } from 'uiSrc/telemetry' |
| 18 | +import { ApiEndpoints } from 'uiSrc/constants' |
| 19 | +import { getUrl } from 'uiSrc/utils' |
| 20 | +import { RootState } from 'uiSrc/slices/store' |
| 21 | +import notificationsReducer from 'uiSrc/slices/app/notifications' |
| 22 | +import appInfoReducer from 'uiSrc/slices/app/info' |
| 23 | +import redisearchReducer from 'uiSrc/slices/browser/redisearch' |
| 24 | +import instancesReducer from 'uiSrc/slices/instances/instances' |
14 | 25 | import { IndexSection, IndexSectionProps } from './IndexSection'
|
15 | 26 |
|
| 27 | +jest.mock('uiSrc/telemetry', () => ({ |
| 28 | + ...jest.requireActual('uiSrc/telemetry'), |
| 29 | + sendEventTelemetry: jest.fn(), |
| 30 | +})) |
| 31 | + |
| 32 | +const createTestStore = () => { |
| 33 | + // TODO: Use rootReducer instead, once you realize how to solve the issue with the instancesReducer |
| 34 | + // console.error No reducer provided for key "instances" |
| 35 | + // > 81 | connections: combineReducers({ |
| 36 | + const testReducer = combineReducers({ |
| 37 | + app: combineReducers({ |
| 38 | + notifications: notificationsReducer, |
| 39 | + info: appInfoReducer, |
| 40 | + }), |
| 41 | + browser: combineReducers({ |
| 42 | + redisearch: redisearchReducer, |
| 43 | + }), |
| 44 | + connections: combineReducers({ |
| 45 | + instances: instancesReducer, |
| 46 | + }), |
| 47 | + }) |
| 48 | + |
| 49 | + const testState: RootState = { |
| 50 | + ...initialStateDefault, |
| 51 | + |
| 52 | + connections: { |
| 53 | + ...initialStateDefault.connections, |
| 54 | + instances: { |
| 55 | + ...initialStateDefault.connections.instances, |
| 56 | + connectedInstance: { |
| 57 | + ...initialStateDefault.connections.instances.connectedInstance, |
| 58 | + id: INSTANCE_ID_MOCK, |
| 59 | + name: 'test-instance', |
| 60 | + host: 'localhost', |
| 61 | + port: 6379, |
| 62 | + modules: [], |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + return configureStore({ |
| 69 | + reducer: testReducer, |
| 70 | + preloadedState: testState, |
| 71 | + middleware: (getDefaultMiddleware) => |
| 72 | + getDefaultMiddleware({ serializableCheck: false }), |
| 73 | + }) |
| 74 | +} |
| 75 | + |
16 | 76 | const renderComponent = (props?: Partial<IndexSectionProps>) => {
|
17 | 77 | const defaultProps: IndexSectionProps = {
|
18 | 78 | index: 'test-index',
|
19 | 79 | }
|
20 | 80 |
|
21 |
| - // Create a mock store with proper connected instance state |
22 |
| - const stateWithConnectedInstance = cloneDeep(initialStateDefault) |
23 |
| - stateWithConnectedInstance.connections.instances.connectedInstance = { |
24 |
| - ...stateWithConnectedInstance.connections.instances.connectedInstance, |
25 |
| - id: INSTANCE_ID_MOCK, |
26 |
| - name: 'test-instance', |
27 |
| - host: 'localhost', |
28 |
| - port: 6379, |
29 |
| - modules: [], |
30 |
| - } |
31 |
| - |
32 |
| - const store = mockStore(stateWithConnectedInstance) |
| 81 | + const store = createTestStore() |
33 | 82 |
|
34 | 83 | return render(
|
35 | 84 | <Provider store={store}>
|
36 | 85 | <IndexSection {...defaultProps} {...props} />
|
| 86 | + <Notifications /> |
37 | 87 | </Provider>,
|
38 | 88 | )
|
39 | 89 | }
|
@@ -147,4 +197,115 @@ describe('IndexSection', () => {
|
147 | 197 | expect(weightValue[0]).toBeInTheDocument()
|
148 | 198 | expect(separatorValue[0]).toBeInTheDocument()
|
149 | 199 | })
|
| 200 | + |
| 201 | + describe('delete index', () => { |
| 202 | + let confirmSpy: jest.SpyInstance |
| 203 | + let telemetryMock: jest.Mock |
| 204 | + |
| 205 | + beforeEach(() => { |
| 206 | + // Mock window.confirm to return true (user confirms deletion) |
| 207 | + confirmSpy = jest.spyOn(window, 'confirm').mockReturnValue(true) |
| 208 | + |
| 209 | + // Mock the telemetry function |
| 210 | + telemetryMock = sendEventTelemetry as jest.Mock |
| 211 | + telemetryMock.mockClear() |
| 212 | + }) |
| 213 | + |
| 214 | + afterEach(() => { |
| 215 | + confirmSpy.mockRestore() |
| 216 | + mswServer.resetHandlers() |
| 217 | + }) |
| 218 | + |
| 219 | + it('should delete an index when the delete button is clicked and confirmed', async () => { |
| 220 | + renderComponent() |
| 221 | + |
| 222 | + const deleteButton = screen.getByText('Delete') |
| 223 | + expect(deleteButton).toBeInTheDocument() |
| 224 | + |
| 225 | + // Click the delete button |
| 226 | + await userEvent.click(deleteButton) |
| 227 | + |
| 228 | + // Verify that confirm was called with the correct message |
| 229 | + expect(confirmSpy).toHaveBeenCalledWith( |
| 230 | + 'Are you sure you want to delete this index?', |
| 231 | + ) |
| 232 | + |
| 233 | + // Wait for the success notification to appear |
| 234 | + const successNotification = await screen.findByText( |
| 235 | + 'Index has been deleted', |
| 236 | + ) |
| 237 | + expect(successNotification).toBeInTheDocument() |
| 238 | + |
| 239 | + // Verify that telemetry event was sent with correct data |
| 240 | + expect(telemetryMock).toHaveBeenCalledTimes(1) |
| 241 | + const telemetryCall = telemetryMock.mock.calls[0][0] |
| 242 | + expect(telemetryCall.event).toBe(TelemetryEvent.SEARCH_INDEX_DELETED) |
| 243 | + expect(telemetryCall.eventData.databaseId).toBe(INSTANCE_ID_MOCK) |
| 244 | + expect(telemetryCall.eventData.indexName).toBeDefined() |
| 245 | + }) |
| 246 | + |
| 247 | + it('should not delete an index when the deletion is cancelled', async () => { |
| 248 | + // Mock window.confirm to return false (user cancels deletion) |
| 249 | + confirmSpy.mockReturnValueOnce(false) |
| 250 | + |
| 251 | + renderComponent() |
| 252 | + |
| 253 | + const deleteButton = screen.getByText('Delete') |
| 254 | + expect(deleteButton).toBeInTheDocument() |
| 255 | + |
| 256 | + // Click the delete button |
| 257 | + await userEvent.click(deleteButton) |
| 258 | + |
| 259 | + // Verify that confirm was called with the correct message |
| 260 | + expect(confirmSpy).toHaveBeenCalledWith( |
| 261 | + 'Are you sure you want to delete this index?', |
| 262 | + ) |
| 263 | + |
| 264 | + // Verify that no API call was made and no notification appears |
| 265 | + expect(telemetryMock).not.toHaveBeenCalled() |
| 266 | + |
| 267 | + const successNotification = screen.queryByText('Index has been deleted') |
| 268 | + expect(successNotification).not.toBeInTheDocument() |
| 269 | + }) |
| 270 | + |
| 271 | + it('should handle deletion failure gracefully', async () => { |
| 272 | + // Override the MSW handler to return an error for this test |
| 273 | + mswServer.use( |
| 274 | + rest.delete( |
| 275 | + getMswURL(getUrl(INSTANCE_ID_MOCK, ApiEndpoints.REDISEARCH)), |
| 276 | + async (_req, res, ctx) => |
| 277 | + res( |
| 278 | + ctx.status(500), |
| 279 | + ctx.json({ |
| 280 | + error: 'Internal Server Error', |
| 281 | + statusCode: 500, |
| 282 | + message: 'Failed to delete index', |
| 283 | + }), |
| 284 | + ), |
| 285 | + ), |
| 286 | + ) |
| 287 | + |
| 288 | + renderComponent() |
| 289 | + |
| 290 | + const deleteButton = screen.getByText('Delete') |
| 291 | + expect(deleteButton).toBeInTheDocument() |
| 292 | + |
| 293 | + // Click the delete button |
| 294 | + await userEvent.click(deleteButton) |
| 295 | + |
| 296 | + // Verify that confirm was called with the correct message |
| 297 | + expect(confirmSpy).toHaveBeenCalledWith( |
| 298 | + 'Are you sure you want to delete this index?', |
| 299 | + ) |
| 300 | + |
| 301 | + // Wait for the error notification to appear |
| 302 | + const errorNotification = await screen.findByText( |
| 303 | + 'Failed to delete index', |
| 304 | + ) |
| 305 | + expect(errorNotification).toBeInTheDocument() |
| 306 | + |
| 307 | + // Verify that telemetry event was not sent on error |
| 308 | + expect(telemetryMock).not.toHaveBeenCalled() |
| 309 | + }) |
| 310 | + }) |
150 | 311 | })
|
0 commit comments