|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | + |
| 12 | +import React from 'react'; |
| 13 | +import { render, screen } from '@testing-library/react'; |
| 14 | +import { DetectorDetail, DetectorRouterProps } from '../DetectorDetail'; |
| 15 | +import { Provider } from 'react-redux'; |
| 16 | +import { |
| 17 | + HashRouter as Router, |
| 18 | + RouteComponentProps, |
| 19 | + Route, |
| 20 | + Switch, |
| 21 | + Redirect, |
| 22 | +} from 'react-router-dom'; |
| 23 | +import configureMockStore from 'redux-mock-store'; |
| 24 | +import thunk from 'redux-thunk'; |
| 25 | +import { httpClientMock, coreServicesMock } from '../../../../../test/mocks'; |
| 26 | +import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices'; |
| 27 | +import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils'; |
| 28 | +import { useFetchDetectorInfo } from '../../../CreateDetectorSteps/hooks/useFetchDetectorInfo'; |
| 29 | + |
| 30 | +jest.mock('../../hooks/useFetchMonitorInfo'); |
| 31 | + |
| 32 | +//jest.mock('../../../CreateDetectorSteps/hooks/useFetchDetectorInfo'); |
| 33 | +jest.mock('../../../CreateDetectorSteps/hooks/useFetchDetectorInfo', () => ({ |
| 34 | + // The jest.mock function is used at the top level of the test file to mock the entire module. |
| 35 | + // Within each test, the mock implementation for useFetchDetectorInfo is set using jest.Mock. |
| 36 | + // This ensures that the hook returns the desired values for each test case. |
| 37 | + useFetchDetectorInfo: jest.fn(), |
| 38 | +})); |
| 39 | + |
| 40 | +jest.mock('../../../../services', () => ({ |
| 41 | + ...jest.requireActual('../../../../services'), |
| 42 | + |
| 43 | + getDataSourceEnabled: () => ({ |
| 44 | + enabled: false, |
| 45 | + }), |
| 46 | +})); |
| 47 | + |
| 48 | +const detectorId = '4QY4YHEB5W9C7vlb3Mou'; |
| 49 | + |
| 50 | +// Configure the mock store |
| 51 | +const middlewares = [thunk]; |
| 52 | +const mockStore = configureMockStore(middlewares); |
| 53 | + |
| 54 | +const renderWithRouter = (detectorId: string, initialState: any) => ({ |
| 55 | + ...render( |
| 56 | + <Provider store={mockStore(initialState)}> |
| 57 | + <Router> |
| 58 | + <Switch> |
| 59 | + <Route |
| 60 | + path={`/detectors/${detectorId}/results`} |
| 61 | + render={(props: RouteComponentProps<DetectorRouterProps>) => { |
| 62 | + const testProps = { |
| 63 | + ...props, |
| 64 | + match: { |
| 65 | + params: { detectorId: detectorId }, |
| 66 | + isExact: false, |
| 67 | + path: '', |
| 68 | + url: '', |
| 69 | + }, |
| 70 | + }; |
| 71 | + return ( |
| 72 | + <CoreServicesContext.Provider value={coreServicesMock}> |
| 73 | + <DetectorDetail {...testProps} /> |
| 74 | + </CoreServicesContext.Provider> |
| 75 | + ); |
| 76 | + }} |
| 77 | + /> |
| 78 | + <Redirect from="/" to={`/detectors/${detectorId}/results`} /> |
| 79 | + </Switch> |
| 80 | + </Router> |
| 81 | + </Provider> |
| 82 | + ), |
| 83 | +}); |
| 84 | + |
| 85 | +const resultIndex = 'opensearch-ad-plugin-result-test-query2'; |
| 86 | + |
| 87 | +describe('detector detail', () => { |
| 88 | + beforeEach(() => { |
| 89 | + jest.clearAllMocks(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('detector info still loading', () => { |
| 93 | + const detectorInfo = { |
| 94 | + detector: getRandomDetector(true, resultIndex), |
| 95 | + hasError: false, |
| 96 | + isLoadingDetector: true, |
| 97 | + errorMessage: undefined, |
| 98 | + }; |
| 99 | + |
| 100 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); |
| 101 | + |
| 102 | + const initialState = { |
| 103 | + opensearch: { |
| 104 | + indices: [{ health: 'green', index: resultIndex }], |
| 105 | + requesting: false, |
| 106 | + }, |
| 107 | + ad: { |
| 108 | + detectors: {}, |
| 109 | + }, |
| 110 | + alerting: { |
| 111 | + monitors: {}, |
| 112 | + }, |
| 113 | + }; |
| 114 | + |
| 115 | + renderWithRouter(detectorId, initialState); |
| 116 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 117 | + |
| 118 | + // Assert that the element is not in the document |
| 119 | + expect(element).toBeNull(); |
| 120 | + }); |
| 121 | + |
| 122 | + test('detector has no result index', () => { |
| 123 | + const detectorInfo = { |
| 124 | + detector: getRandomDetector(true, undefined), |
| 125 | + hasError: false, |
| 126 | + isLoadingDetector: true, |
| 127 | + errorMessage: undefined, |
| 128 | + }; |
| 129 | + |
| 130 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); |
| 131 | + |
| 132 | + const initialState = { |
| 133 | + opensearch: { |
| 134 | + indices: [{ health: 'green', index: resultIndex }], |
| 135 | + requesting: false, |
| 136 | + }, |
| 137 | + ad: { |
| 138 | + detectors: {}, |
| 139 | + }, |
| 140 | + alerting: { |
| 141 | + monitors: {}, |
| 142 | + }, |
| 143 | + }; |
| 144 | + |
| 145 | + renderWithRouter(detectorId, initialState); |
| 146 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 147 | + |
| 148 | + // Assert that the element is not in the document |
| 149 | + expect(element).toBeNull(); |
| 150 | + }); |
| 151 | + |
| 152 | + test('cat indices are being requested', () => { |
| 153 | + const detectorInfo = { |
| 154 | + detector: getRandomDetector(true, resultIndex), |
| 155 | + hasError: false, |
| 156 | + isLoadingDetector: false, |
| 157 | + errorMessage: undefined, |
| 158 | + }; |
| 159 | + |
| 160 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); |
| 161 | + |
| 162 | + const initialState = { |
| 163 | + opensearch: { |
| 164 | + indices: [], |
| 165 | + requesting: true, |
| 166 | + }, |
| 167 | + ad: { |
| 168 | + detectors: {}, |
| 169 | + }, |
| 170 | + alerting: { |
| 171 | + monitors: {}, |
| 172 | + }, |
| 173 | + }; |
| 174 | + |
| 175 | + renderWithRouter(detectorId, initialState); |
| 176 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 177 | + |
| 178 | + // Assert that the element is not in the document |
| 179 | + expect(element).toBeNull(); |
| 180 | + }); |
| 181 | + |
| 182 | + test('visible indices are empty', () => { |
| 183 | + const detectorInfo = { |
| 184 | + detector: getRandomDetector(true, resultIndex), |
| 185 | + hasError: false, |
| 186 | + isLoadingDetector: false, |
| 187 | + errorMessage: undefined, |
| 188 | + }; |
| 189 | + |
| 190 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); |
| 191 | + |
| 192 | + const initialState = { |
| 193 | + opensearch: { |
| 194 | + indices: [], |
| 195 | + requesting: false, |
| 196 | + }, |
| 197 | + ad: { |
| 198 | + detectors: {}, |
| 199 | + }, |
| 200 | + alerting: { |
| 201 | + monitors: {}, |
| 202 | + }, |
| 203 | + }; |
| 204 | + |
| 205 | + renderWithRouter(detectorId, initialState); |
| 206 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 207 | + |
| 208 | + // Assert that the element is not in the document |
| 209 | + expect(element).toBeNull(); |
| 210 | + }); |
| 211 | + |
| 212 | + test('the result index is not found in the visible indices', () => { |
| 213 | + const detectorInfo = { |
| 214 | + detector: getRandomDetector(true, resultIndex), |
| 215 | + hasError: false, |
| 216 | + isLoadingDetector: false, |
| 217 | + errorMessage: undefined, |
| 218 | + }; |
| 219 | + |
| 220 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => detectorInfo); |
| 221 | + |
| 222 | + const initialState = { |
| 223 | + opensearch: { |
| 224 | + indices: [{ health: 'green', index: '.kibana_-962704462_v992471_1' }], |
| 225 | + requesting: false, |
| 226 | + }, |
| 227 | + ad: { |
| 228 | + detectors: {}, |
| 229 | + }, |
| 230 | + alerting: { |
| 231 | + monitors: {}, |
| 232 | + }, |
| 233 | + }; |
| 234 | + |
| 235 | + renderWithRouter(detectorId, initialState); |
| 236 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 237 | + |
| 238 | + // Assert that the element is in the document |
| 239 | + expect(element).not.toBeNull(); |
| 240 | + }); |
| 241 | + |
| 242 | + test('the result index is found in the visible indices', () => { |
| 243 | + const detector = getRandomDetector(true, resultIndex); |
| 244 | + |
| 245 | + // Set up the mock implementation for useFetchDetectorInfo |
| 246 | + (useFetchDetectorInfo as jest.Mock).mockImplementation(() => ({ |
| 247 | + detector: detector, |
| 248 | + hasError: false, |
| 249 | + isLoadingDetector: false, |
| 250 | + errorMessage: undefined, |
| 251 | + })); |
| 252 | + |
| 253 | + const initialState = { |
| 254 | + opensearch: { |
| 255 | + indices: [ |
| 256 | + { health: 'green', index: '.kibana_-962704462_v992471_1' }, |
| 257 | + { health: 'green', index: resultIndex }, |
| 258 | + ], |
| 259 | + requesting: false, |
| 260 | + }, |
| 261 | + ad: { |
| 262 | + detectors: {}, |
| 263 | + }, |
| 264 | + alerting: { |
| 265 | + monitors: {}, |
| 266 | + }, |
| 267 | + }; |
| 268 | + |
| 269 | + renderWithRouter(detectorId, initialState); |
| 270 | + const element = screen.queryByTestId('missingResultIndexCallOut'); |
| 271 | + |
| 272 | + // Assert that the element is not in the document |
| 273 | + expect(element).toBeNull(); |
| 274 | + }); |
| 275 | +}); |
0 commit comments