Skip to content

Commit 1bed6ab

Browse files
committed
Add MDS UTs and bug fixes (#734)
* working on ut Signed-off-by: Jackie Han <jkhanjob@gmail.com> * update ut Signed-off-by: Jackie Han <jkhanjob@gmail.com> * update UTs and add bug fixes Signed-off-by: Jackie Han <jkhanjob@gmail.com> --------- Signed-off-by: Jackie Han <jkhanjob@gmail.com>
1 parent 348a29c commit 1bed6ab

File tree

39 files changed

+536
-185
lines changed

39 files changed

+536
-185
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@
5656
"browserify-sign": "^4.2.2",
5757
"axios": "^1.6.1"
5858
}
59-
}
59+
}

public/pages/AnomalyCharts/containers/__tests__/AnomaliesChart.test.tsx

+18-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import {
2222
FAKE_ANOMALIES_RESULT,
2323
FAKE_DATE_RANGE,
2424
} from '../../../../pages/utils/__tests__/constants';
25+
import { Router } from 'react-router-dom';
26+
import { createMemoryHistory } from 'history';
2527

2628
const DEFAULT_PROPS = {
2729
onDateRangeChange: jest.fn(),
@@ -47,17 +49,32 @@ const DEFAULT_PROPS = {
4749
entityAnomalySummaries: [],
4850
} as AnomaliesChartProps;
4951

52+
const history = createMemoryHistory();
53+
5054
const renderDataFilter = (chartProps: AnomaliesChartProps) => ({
5155
...render(
5256
<Provider store={mockedStore()}>
5357
<CoreServicesContext.Provider value={coreServicesMock}>
54-
<AnomaliesChart {...chartProps} />
58+
<Router history={history}>
59+
<AnomaliesChart {...chartProps} />
60+
</Router>
5561
</CoreServicesContext.Provider>
5662
</Provider>
5763
),
5864
});
5965

6066
describe('<AnomaliesChart /> spec', () => {
67+
beforeAll(() => {
68+
Object.defineProperty(window, 'location', {
69+
value: {
70+
href: 'http://test.com',
71+
pathname: '/',
72+
search: '',
73+
hash: '',
74+
},
75+
writable: true
76+
});
77+
});
6178
test('renders the component for sample / preview', () => {
6279
console.error = jest.fn();
6380
const { getByText, getAllByText } = renderDataFilter({

public/pages/AnomalyCharts/containers/__tests__/AnomalyDetailsChart.test.tsx

+41-15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ import {
2222
} from '../../../../pages/utils/__tests__/constants';
2323
import { INITIAL_ANOMALY_SUMMARY } from '../../utils/constants';
2424
import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils';
25+
import { createMemoryHistory } from 'history';
26+
import { Router } from 'react-router-dom';
27+
28+
jest.mock('../../../../services', () => ({
29+
...jest.requireActual('../../../../services'),
30+
31+
getDataSourceEnabled: () => ({
32+
enabled: false
33+
})
34+
}));
35+
36+
const history = createMemoryHistory();
2537

2638
const renderAnomalyOccurenceChart = (
2739
isNotSample: boolean,
@@ -30,27 +42,41 @@ const renderAnomalyOccurenceChart = (
3042
...render(
3143
<Provider store={mockedStore()}>
3244
<CoreServicesContext.Provider value={coreServicesMock}>
33-
<AnomalyDetailsChart
34-
onDateRangeChange={jest.fn()}
35-
onZoomRangeChange={jest.fn()}
36-
bucketizedAnomalies={false}
37-
anomalySummary={INITIAL_ANOMALY_SUMMARY}
38-
anomalies={FAKE_ANOMALY_DATA}
39-
detector={getRandomDetector()}
40-
dateRange={FAKE_DATE_RANGE}
41-
isLoading={false}
42-
showAlerts={isNotSample}
43-
isNotSample={isNotSample}
44-
isHCDetector={isHCDetector}
45-
anomalyGradeSeriesName={'testAnomalyGradeSeriesName'}
46-
confidenceSeriesName={'testConfidenceSeriesName'}
47-
/>
45+
<Router history={history}>
46+
<AnomalyDetailsChart
47+
onDateRangeChange={jest.fn()}
48+
onZoomRangeChange={jest.fn()}
49+
bucketizedAnomalies={false}
50+
anomalySummary={INITIAL_ANOMALY_SUMMARY}
51+
anomalies={FAKE_ANOMALY_DATA}
52+
detector={getRandomDetector()}
53+
dateRange={FAKE_DATE_RANGE}
54+
isLoading={false}
55+
showAlerts={isNotSample}
56+
isNotSample={isNotSample}
57+
isHCDetector={isHCDetector}
58+
anomalyGradeSeriesName={'testAnomalyGradeSeriesName'}
59+
confidenceSeriesName={'testConfidenceSeriesName'}
60+
/>
61+
</Router>
4862
</CoreServicesContext.Provider>
4963
</Provider>
5064
),
5165
});
5266

5367
describe('<AnomalyDetailsChart /> spec', () => {
68+
beforeAll(() => {
69+
Object.defineProperty(window, 'location', {
70+
value: {
71+
href: 'http://test.com',
72+
pathname: '/',
73+
search: '',
74+
hash: '',
75+
},
76+
writable: true
77+
});
78+
});
79+
5480
test('renders the component in case of Sample Anomaly', () => {
5581
console.error = jest.fn();
5682
const { getByText } = renderAnomalyOccurenceChart(false, false);

public/pages/AnomalyCharts/containers/__tests__/AnomalyOccurrenceChart.test.tsx

+41-16
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,67 @@ import { mockedStore } from '../../../../redux/utils/testUtils';
1616
import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices';
1717
import { coreServicesMock } from '../../../../../test/mocks';
1818
import { AnomalyOccurrenceChart } from '../AnomalyOccurrenceChart';
19+
import { createMemoryHistory } from 'history';
20+
import { Router } from 'react-router-dom';
1921
import {
2022
FAKE_ANOMALY_DATA,
2123
FAKE_DATE_RANGE,
2224
} from '../../../../pages/utils/__tests__/constants';
2325
import { INITIAL_ANOMALY_SUMMARY } from '../../utils/constants';
2426
import { getRandomDetector } from '../../../../redux/reducers/__tests__/utils';
2527

28+
jest.mock('../../../../services', () => ({
29+
...jest.requireActual('../../../../services'),
30+
31+
getDataSourceEnabled: () => ({
32+
enabled: false
33+
})
34+
}));
35+
36+
const history = createMemoryHistory();
37+
2638
const renderAnomalyOccurenceChart = (
2739
isNotSample: boolean,
2840
isHCDetector: boolean
2941
) => ({
3042
...render(
3143
<Provider store={mockedStore()}>
3244
<CoreServicesContext.Provider value={coreServicesMock}>
33-
<AnomalyOccurrenceChart
34-
onDateRangeChange={jest.fn()}
35-
onZoomRangeChange={jest.fn()}
36-
title="test"
37-
bucketizedAnomalies={false}
38-
anomalySummary={INITIAL_ANOMALY_SUMMARY}
39-
anomalies={FAKE_ANOMALY_DATA}
40-
detector={getRandomDetector()}
41-
dateRange={FAKE_DATE_RANGE}
42-
isLoading={false}
43-
showAlerts={isNotSample}
44-
isNotSample={isNotSample}
45-
isHCDetector={isHCDetector}
46-
anomalyGradeSeriesName={'testAnomalyGradeSeriesName'}
47-
confidenceSeriesName={'testConfidenceSeriesName'}
48-
/>
45+
<Router history={history}>
46+
<AnomalyOccurrenceChart
47+
onDateRangeChange={jest.fn()}
48+
onZoomRangeChange={jest.fn()}
49+
title="test"
50+
bucketizedAnomalies={false}
51+
anomalySummary={INITIAL_ANOMALY_SUMMARY}
52+
anomalies={FAKE_ANOMALY_DATA}
53+
detector={getRandomDetector()}
54+
dateRange={FAKE_DATE_RANGE}
55+
isLoading={false}
56+
showAlerts={isNotSample}
57+
isNotSample={isNotSample}
58+
isHCDetector={isHCDetector}
59+
anomalyGradeSeriesName={'testAnomalyGradeSeriesName'}
60+
confidenceSeriesName={'testConfidenceSeriesName'}
61+
/>
62+
</Router>
4963
</CoreServicesContext.Provider>
5064
</Provider>
5165
),
5266
});
5367

5468
describe('<AnomalyOccurrenceChart /> spec', () => {
69+
beforeAll(() => {
70+
Object.defineProperty(window, 'location', {
71+
value: {
72+
href: 'http://test.com',
73+
pathname: '/',
74+
search: '',
75+
hash: '',
76+
},
77+
writable: true
78+
});
79+
});
5580
test('renders the component in case of Sample Anomaly', () => {
5681
console.error = jest.fn();
5782
const { getByText } = renderAnomalyOccurenceChart(false, false);

public/pages/ConfigureModel/containers/ConfigureModel.tsx

+46-21
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { RouteComponentProps, useLocation } from 'react-router-dom';
3232
import { AppState } from '../../../redux/reducers';
3333
import { getMappings } from '../../../redux/reducers/opensearch';
3434
import { useFetchDetectorInfo } from '../../CreateDetectorSteps/hooks/useFetchDetectorInfo';
35-
import { BREADCRUMBS, BASE_DOCS_LINK } from '../../../utils/constants';
35+
import { BREADCRUMBS, BASE_DOCS_LINK, MDS_BREADCRUMBS } from '../../../utils/constants';
3636
import { useHideSideNavBar } from '../../main/hooks/useHideSideNavBar';
3737
import { updateDetector } from '../../../redux/reducers/ad';
3838
import {
@@ -64,7 +64,7 @@ import {
6464
} from '../../../pages/utils/helpers';
6565
import {
6666
getDataSourceManagementPlugin,
67-
getDataSourcePlugin,
67+
getDataSourceEnabled,
6868
getNotifications,
6969
getSavedObjectsClient,
7070
} from '../../../services';
@@ -89,7 +89,7 @@ export function ConfigureModel(props: ConfigureModelProps) {
8989
const dispatch = useDispatch();
9090
const location = useLocation();
9191
const MDSQueryParams = getDataSourceFromURL(location);
92-
const dataSourceEnabled = getDataSourcePlugin()?.dataSourceEnabled || false;
92+
const dataSourceEnabled = getDataSourceEnabled().enabled;
9393
const dataSourceId = MDSQueryParams.dataSourceId;
9494

9595
useHideSideNavBar(true, false);
@@ -122,30 +122,55 @@ export function ConfigureModel(props: ConfigureModelProps) {
122122
}, [detector]);
123123

124124
useEffect(() => {
125-
if (props.isEdit) {
126-
core.chrome.setBreadcrumbs([
127-
BREADCRUMBS.ANOMALY_DETECTOR,
128-
BREADCRUMBS.DETECTORS,
129-
{
130-
text: detector && detector.name ? detector.name : '',
131-
href: constructHrefWithDataSourceId(`#/detectors/${detectorId}`, dataSourceId, false)
132-
},
133-
BREADCRUMBS.EDIT_MODEL_CONFIGURATION,
134-
]);
125+
if (dataSourceEnabled) {
126+
if (props.isEdit) {
127+
core.chrome.setBreadcrumbs([
128+
MDS_BREADCRUMBS.ANOMALY_DETECTOR(dataSourceId),
129+
MDS_BREADCRUMBS.DETECTORS(dataSourceId),
130+
{
131+
text: detector && detector.name ? detector.name : '',
132+
href: constructHrefWithDataSourceId(`#/detectors/${detectorId}`, dataSourceId, false)
133+
},
134+
MDS_BREADCRUMBS.EDIT_MODEL_CONFIGURATION,
135+
]);
136+
} else {
137+
core.chrome.setBreadcrumbs([
138+
MDS_BREADCRUMBS.ANOMALY_DETECTOR(dataSourceId),
139+
MDS_BREADCRUMBS.DETECTORS(dataSourceId),
140+
MDS_BREADCRUMBS.CREATE_DETECTOR,
141+
]);
142+
}
135143
} else {
136-
core.chrome.setBreadcrumbs([
137-
BREADCRUMBS.ANOMALY_DETECTOR,
138-
BREADCRUMBS.DETECTORS,
139-
BREADCRUMBS.CREATE_DETECTOR,
140-
]);
144+
if (props.isEdit) {
145+
core.chrome.setBreadcrumbs([
146+
BREADCRUMBS.ANOMALY_DETECTOR,
147+
BREADCRUMBS.DETECTORS,
148+
{
149+
text: detector && detector.name ? detector.name : '',
150+
href: `#/detectors/${detectorId}`,
151+
},
152+
BREADCRUMBS.EDIT_MODEL_CONFIGURATION,
153+
]);
154+
} else {
155+
core.chrome.setBreadcrumbs([
156+
BREADCRUMBS.ANOMALY_DETECTOR,
157+
BREADCRUMBS.DETECTORS,
158+
BREADCRUMBS.CREATE_DETECTOR,
159+
]);
160+
}
141161
}
142162
}, [detector]);
143163

144164
useEffect(() => {
145165
if (hasError) {
146-
props.history.push(
147-
constructHrefWithDataSourceId('/detectors', dataSourceId, false)
148-
);
166+
if(dataSourceEnabled) {
167+
props.history.push(
168+
constructHrefWithDataSourceId('/detectors', dataSourceId, false)
169+
);
170+
}
171+
else {
172+
props.history.push('/detectors');
173+
}
149174
}
150175
}, [hasError]);
151176

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

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import { CoreServicesContext } from '../../../../components/CoreServices/CoreSer
2525
import { INITIAL_DETECTOR_DEFINITION_VALUES } from '../../../DefineDetector/utils/constants';
2626
import { INITIAL_MODEL_CONFIGURATION_VALUES } from '../../utils/constants';
2727

28+
jest.mock('../../../../services', () => ({
29+
...jest.requireActual('../../../../services'),
30+
31+
getDataSourceEnabled: () => ({
32+
enabled: false
33+
})
34+
}));
35+
2836
const renderWithRouter = (isEdit: boolean = false) => ({
2937
...render(
3038
<Provider store={configureStore(httpClientMock)}>
@@ -34,6 +42,7 @@ const renderWithRouter = (isEdit: boolean = false) => ({
3442
render={(props: RouteComponentProps) => (
3543
<CoreServicesContext.Provider value={coreServicesMock}>
3644
<ConfigureModel
45+
setActionMenu={jest.fn()}
3746
isEdit={isEdit}
3847
detectorDefinitionValues={INITIAL_DETECTOR_DEFINITION_VALUES}
3948
initialValues={INITIAL_MODEL_CONFIGURATION_VALUES}

public/pages/Dashboard/Components/EmptyDashboard/__tests__/EmptyDashboard.test.tsx

+16-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@
1212
import React from 'react';
1313
import { render } from '@testing-library/react';
1414
import { EmptyDashboard } from '../EmptyDashboard';
15+
import { createMemoryHistory } from 'history';
16+
import { Router } from 'react-router-dom';
17+
18+
jest.mock('../../../../../services', () => ({
19+
...jest.requireActual('../../../../../services'),
20+
21+
getDataSourceEnabled: () => ({
22+
enabled: false
23+
})
24+
}));
25+
26+
const history = createMemoryHistory();
1527

1628
describe('<EmptyDetector /> spec', () => {
1729
describe('Empty results', () => {
1830
test('renders component with empty message', async () => {
19-
const { container } = render(<EmptyDashboard />);
31+
const { container } = render(
32+
<Router history={history}>
33+
<EmptyDashboard />
34+
</Router>);
2035
expect(container.firstChild).toMatchSnapshot();
2136
});
2237
});

public/pages/Dashboard/Components/EmptyDashboard/__tests__/__snapshots__/EmptyDashboard.test.tsx.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ exports[`<EmptyDetector /> spec Empty results renders component with empty messa
7272
<a
7373
class="euiButton euiButton--primary"
7474
data-test-subj="sampleDetectorButton"
75-
href="anomaly-detection-dashboards#/overview"
75+
href="anomaly-detection-dashboards#/overview?"
7676
rel="noreferrer"
7777
style="width: 200px;"
7878
>
@@ -93,7 +93,7 @@ exports[`<EmptyDetector /> spec Empty results renders component with empty messa
9393
<a
9494
class="euiButton euiButton--primary euiButton--fill"
9595
data-test-subj="createDetectorButton"
96-
href="anomaly-detection-dashboards#/create-ad/"
96+
href="anomaly-detection-dashboards#/create-ad/?"
9797
rel="noreferrer"
9898
style="width: 200px;"
9999
>

0 commit comments

Comments
 (0)