Skip to content

Commit 6a07d17

Browse files
authored
Display selected clusters correctly on edit page (#1011)
* clusters show up on edit page Signed-off-by: Amit Galitzky <[email protected]> * update release notes Signed-off-by: Amit Galitzky <[email protected]> --------- Signed-off-by: Amit Galitzky <[email protected]>
1 parent 47a7457 commit 6a07d17

File tree

8 files changed

+1384
-26
lines changed

8 files changed

+1384
-26
lines changed

public/pages/DefineDetector/components/Datasource/DataSource.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ import { INITIAL_MODEL_CONFIGURATION_VALUES } from '../../../ConfigureModel/util
4646
import { FILTER_TYPES } from '../../../../models/interfaces';
4747
import { useLocation } from 'react-router-dom';
4848
import _ from 'lodash';
49-
import { cleanString } from '../../../../../../../src/plugins/vis_type_vega/public/expressions/helpers';
50-
import { L } from '../../../../../../../src/plugins/maps_legacy/public/lazy_load_bundle/lazy';
49+
import { getClusterOptionLabel } from '../../utils/helpers';
5150

5251
interface DataSourceProps {
5352
formikProps: FormikProps<DetectorDefinitionFormikValues>;
@@ -59,7 +58,7 @@ interface DataSourceProps {
5958
oldFilterQuery: any;
6059
}
6160

62-
interface ClusterOption {
61+
export interface ClusterOption {
6362
label: string;
6463
cluster: string;
6564
localcluster: string;
@@ -81,7 +80,7 @@ export function DataSource(props: DataSourceProps) {
8180
useEffect(() => {
8281
const getInitialClusters = async () => {
8382
await dispatch(getClustersInfo(dataSourceId));
84-
setFieldValue('clusters', props.formikProps.values.clusters);
83+
handleClusterUpdate(opensearchState.clusters);
8584
};
8685
getInitialClusters();
8786
}, [dataSourceId]);
@@ -141,7 +140,7 @@ export function DataSource(props: DataSourceProps) {
141140
return clustersString;
142141
};
143142

144-
useEffect(() => {
143+
const handleClusterUpdate = (clusters: ClusterInfo[]) => {
145144
if (opensearchState.clusters && opensearchState.clusters.length > 0) {
146145
const localCluster: ClusterInfo[] = getLocalCluster(
147146
opensearchState.clusters
@@ -156,10 +155,11 @@ export function DataSource(props: DataSourceProps) {
156155
setFieldValue('clusters', getVisibleClusterOptions(localCluster));
157156
}
158157
}
159-
}, [opensearchState.clusters]);
158+
};
160159

161-
const getClusterOptionLabel = (clusterInfo: ClusterInfo) =>
162-
`${clusterInfo.name} ${clusterInfo.localCluster ? '(Local)' : '(Remote)'}`;
160+
useEffect(() => {
161+
handleClusterUpdate(opensearchState.clusters);
162+
}, [opensearchState.clusters]);
163163

164164
useEffect(() => {
165165
setIndexNames(props.formikProps.values.index);

public/pages/DefineDetector/containers/DefineDetector.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import React, {
1717
useState,
1818
} from 'react';
1919
import { RouteComponentProps, useLocation } from 'react-router';
20-
import { useDispatch } from 'react-redux';
20+
import { useDispatch, useSelector } from 'react-redux';
2121
import { Dispatch } from 'redux';
2222
import { FormikProps, Formik } from 'formik';
2323
import { get, isEmpty } from 'lodash';
@@ -69,6 +69,7 @@ import {
6969
isDataSourceCompatible,
7070
} from '../../../pages/utils/helpers';
7171
import queryString from 'querystring';
72+
import { AppState } from '../../../redux/reducers';
7273

7374
interface DefineDetectorRouterProps {
7475
detectorId?: string;
@@ -89,7 +90,7 @@ export const DefineDetector = (props: DefineDetectorProps) => {
8990
const MDSQueryParams = getDataSourceFromURL(location);
9091
const dataSourceId = MDSQueryParams.dataSourceId;
9192
const dataSourceEnabled = getDataSourceEnabled().enabled;
92-
93+
const opensearchState = useSelector((state: AppState) => state.opensearch);
9394
const core = React.useContext(CoreServicesContext) as CoreStart;
9495
const dispatch = useDispatch<Dispatch<APIAction>>();
9596
useHideSideNavBar(true, false);
@@ -345,7 +346,7 @@ export const DefineDetector = (props: DefineDetectorProps) => {
345346
initialValues={
346347
props.initialValues
347348
? props.initialValues
348-
: detectorDefinitionToFormik(detector)
349+
: detectorDefinitionToFormik(detector, opensearchState.clusters)
349350
}
350351
enableReinitialize={true}
351352
onSubmit={() => {}}

public/pages/DefineDetector/containers/__tests__/DefineDetector.test.tsx

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,40 @@ import {
2626
INITIAL_DETECTOR_DEFINITION_VALUES,
2727
testDetectorDefinitionValues,
2828
} from '../../utils/constants';
29+
import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils';
30+
import { useFetchDetectorInfo } from '../../../CreateDetectorSteps/hooks/useFetchDetectorInfo';
2931

3032
jest.mock('../../../../services', () => {
3133
const originalModule = jest.requireActual('../../../../services');
3234

3335
return {
3436
...originalModule,
3537
getDataSourceEnabled: () => ({
36-
enabled: false
38+
enabled: false,
3739
}),
3840
getUISettings: () => ({
3941
get: (flag) => {
4042
if (flag === 'home:useNewHomePage') {
41-
return false;
43+
return false;
4244
}
4345
return originalModule.getUISettings().get(flag);
44-
}
46+
},
4547
}),
4648
getNavigationUI: () => ({
47-
HeaderControl: null
49+
HeaderControl: null,
4850
}),
4951
getApplication: () => ({
50-
setAppRightControls: null,
51-
})
52+
setAppRightControls: null,
53+
}),
5254
};
5355
});
5456

57+
jest.mock('../../../CreateDetectorSteps/hooks/useFetchDetectorInfo', () => ({
58+
// Within each test, the mock implementation for useFetchDetectorInfo is set using jest.Mock.
59+
// This ensures that the hook returns the desired values for each test case.
60+
useFetchDetectorInfo: jest.fn(),
61+
}));
62+
5563
const renderWithRouterEmpty = (isEdit: boolean = false) => ({
5664
...render(
5765
<Provider store={configureStore(httpClientMock)}>
@@ -98,6 +106,28 @@ const renderWithRouterFull = (isEdit: boolean = false) => ({
98106
),
99107
});
100108

109+
const renderWithRouterFullAndEdit = (isEdit: boolean = true) => ({
110+
...render(
111+
<Provider store={configureStore(httpClientMock)}>
112+
<Router>
113+
<Switch>
114+
<Route
115+
render={(props: RouteComponentProps) => (
116+
<CoreServicesContext.Provider value={coreServicesMock}>
117+
<DefineDetector
118+
setActionMenu={jest.fn()}
119+
isEdit={isEdit}
120+
{...props}
121+
/>
122+
</CoreServicesContext.Provider>
123+
)}
124+
/>
125+
</Switch>
126+
</Router>
127+
</Provider>
128+
),
129+
});
130+
101131
describe('<DefineDetector /> Full', () => {
102132
beforeEach(() => {
103133
jest.clearAllMocks();
@@ -106,12 +136,32 @@ describe('<DefineDetector /> Full', () => {
106136
});
107137
describe('creating detector definition', () => {
108138
test('renders the component', () => {
139+
const detectorInfo = {
140+
detector: getRandomDetector(true),
141+
hasError: false,
142+
isLoadingDetector: true,
143+
errorMessage: undefined,
144+
};
145+
146+
(useFetchDetectorInfo as jest.Mock).mockImplementation(
147+
() => detectorInfo
148+
);
109149
const { container, getByText } = renderWithRouterFull(false);
110150
getByText('Define detector');
111151
expect(container.firstChild).toMatchSnapshot();
112152
});
113153

114154
test('duplicate name', async () => {
155+
const detectorInfo = {
156+
detector: getRandomDetector(true),
157+
hasError: false,
158+
isLoadingDetector: true,
159+
errorMessage: undefined,
160+
};
161+
162+
(useFetchDetectorInfo as jest.Mock).mockImplementation(
163+
() => detectorInfo
164+
);
115165
httpClientMock.get = jest.fn().mockResolvedValue({
116166
ok: true,
117167
response: {
@@ -130,6 +180,31 @@ describe('<DefineDetector /> Full', () => {
130180
});
131181
});
132182

183+
describe('<DefineDetector /> FullEdit', () => {
184+
beforeEach(() => {
185+
console.error = jest.fn();
186+
console.warn = jest.fn();
187+
});
188+
describe('editing detector page', () => {
189+
test('renders the component', () => {
190+
const detectorInfo = {
191+
detector: getRandomDetector(true, '', ['opensearch:index-1']),
192+
hasError: false,
193+
isLoadingDetector: true,
194+
errorMessage: undefined,
195+
};
196+
197+
(useFetchDetectorInfo as jest.Mock).mockImplementation(
198+
() => detectorInfo
199+
);
200+
const { container, getByText } = renderWithRouterFullAndEdit(true);
201+
getByText('Edit detector settings');
202+
getByText('opensearch (Remote)');
203+
expect(container.firstChild).toMatchSnapshot();
204+
});
205+
});
206+
});
207+
133208
describe('<DefineDetector /> empty', () => {
134209
beforeEach(() => {
135210
jest.clearAllMocks();

0 commit comments

Comments
 (0)