Skip to content

Commit

Permalink
added UT for validation API related components (opensearch-project#252)
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Galitzky <[email protected]>
  • Loading branch information
amitgalitz authored May 26, 2022
1 parent 96a0ab2 commit f95f52b
Show file tree
Hide file tree
Showing 40 changed files with 6,365 additions and 134 deletions.
8 changes: 5 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A clear and concise description of the bug.

**How can one reproduce the bug?**
Steps to reproduce the behavior:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
Expand All @@ -20,9 +21,10 @@ Steps to reproduce the behavior:
A clear and concise description of what you expected to happen.

**What is your host/environment?**
- OS: [e.g. iOS]
- Version [e.g. 22]
- Plugins

- OS: [e.g. iOS]
- Version [e.g. 22]
- Plugins

**Do you have any screenshots?**
If applicable, add screenshots to help explain your problem.
Expand Down
3 changes: 2 additions & 1 deletion .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ title: '[FEATURE]'
labels: 'enhancement, untriaged'
assignees: ''
---

**Is your feature request related to a problem?**
A clear and concise description of what the problem is, e.g. _I'm always frustrated when [...]_

Expand All @@ -15,4 +16,4 @@ A clear and concise description of what you want to happen.
A clear and concise description of any alternative solutions or features you've considered.

**Do you have any additional context?**
Add any other context or screenshots about the feature request here.
Add any other context or screenshots about the feature request here.
3 changes: 3 additions & 0 deletions global-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
process.env.TZ = 'UTC';
}
2 changes: 1 addition & 1 deletion public/components/CodeModal/__tests__/CodeModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import React from 'react';
import { render } from '@testing-library/react';
import { CodeModal } from '../CodeModal';

describe('CodeMOdal spec', () => {
describe('CodeModal spec', () => {
const onVisibilityChange = jest.fn(() => true);
const onCloseModal = jest.fn();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`CodeMOdal spec renders the component 1`] = `
exports[`CodeModal spec renders the component 1`] = `
<div
aria-hidden="true"
data-aria-hidden="true"
Expand Down
26 changes: 26 additions & 0 deletions public/components/ContentPanel/ContentPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@ describe('<ContentPanel /> spec', () => {
getByText('Hello');
getByText('World');
});

test('renders array title', () => {
const { getByText } = render(
<ContentPanel
title={['testing-1', 'testing-2']}
actions={[<div>Hello</div>, <div>World</div>]}
>
<div>Testing ContentPanel</div>
</ContentPanel>
);
getByText('testing-1');
getByText('testing-2');
});
test('renders array subtitle', () => {
const { getByText } = render(
<ContentPanel
title="Testing"
subTitle={['testing-1', 'testing-2']}
actions={[<div>Hello</div>, <div>World</div>]}
>
<div>Testing ContentPanel</div>
</ContentPanel>
);
getByText('testing-1');
getByText('testing-2');
});
});
6 changes: 2 additions & 4 deletions public/components/ContentPanel/ContentPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const ContentPanel = (props: ContentPanelProps) => {
);
return (
<EuiPanel
style={{ padding: '20px'}}
style={{ padding: '20px' }}
className={props.contentPanelClassName}
>
<EuiFlexGroup
Expand Down Expand Up @@ -133,9 +133,7 @@ const ContentPanel = (props: ContentPanelProps) => {
margin="s"
className={props.horizontalRuleClassName}
/>
<div style={{ padding: '10px 0px' }}>
{props.children}
</div>
<div style={{ padding: '10px 0px' }}>{props.children}</div>
</div>
) : null}
</EuiPanel>
Expand Down
6 changes: 3 additions & 3 deletions public/models/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ export type MonitorAlert = {
triggerName: string;
severity: number;
state: string;
error: string;
error: string | null;
startTime: number;
endTime: number;
acknowledgedTime: number;
endTime: number | null;
acknowledgedTime: number | null;
};

export type AnomalySummary = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,14 @@ exports[`<AnomalyHeatmapChart /> spec AnomalyHeatmapChart with anomaly summaries
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7.5 11.508L7.468 8H6.25V7h2.401l.03 3.508H9.8v1H7.5zm-.25-6.202a.83.83 0 01.207-.577c.137-.153.334-.229.59-.229.256 0 .454.076.594.23.14.152.209.345.209.576 0 .228-.07.417-.21.568-.14.15-.337.226-.593.226-.256 0-.453-.075-.59-.226a.81.81 0 01-.207-.568zM8 13A5 5 0 108 3a5 5 0 000 10zm0 1A6 6 0 118 2a6 6 0 010 12z"
d="M13.6 12.186l-1.357-1.358c-.025-.025-.058-.034-.084-.056.53-.794.84-1.746.84-2.773a4.977 4.977 0 00-.84-2.772c.026-.02.059-.03.084-.056L13.6 3.813a6.96 6.96 0 010 8.373zM8 15A6.956 6.956 0 013.814 13.6l1.358-1.358c.025-.025.034-.057.055-.084C6.02 12.688 6.974 13 8 13a4.978 4.978 0 002.773-.84c.02.026.03.058.056.083l1.357 1.358A6.956 6.956 0 018 15zm-5.601-2.813a6.963 6.963 0 010-8.373l1.359 1.358c.024.025.057.035.084.056A4.97 4.97 0 003 8c0 1.027.31 1.98.842 2.773-.027.022-.06.031-.084.056l-1.36 1.358zm5.6-.187A4 4 0 118 4a4 4 0 010 8zM8 1c1.573 0 3.019.525 4.187 1.4l-1.357 1.358c-.025.025-.035.057-.056.084A4.979 4.979 0 008 3a4.979 4.979 0 00-2.773.842c-.021-.027-.03-.059-.055-.084L3.814 2.4A6.957 6.957 0 018 1zm0-1a8.001 8.001 0 10.003 16.002A8.001 8.001 0 008 0z"
fill-rule="evenodd"
/>
</svg>
<span
class="euiCallOutHeader__title"
>
No anomalies found in the specified date range.
Choose a filled rectangle in the heat map for a more detailed view of anomalies within that entity.
</span>
</div>
</div>
Expand Down Expand Up @@ -931,14 +931,14 @@ exports[`<AnomalyHeatmapChart /> spec AnomalyHeatmapChart with multiple category
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7.5 11.508L7.468 8H6.25V7h2.401l.03 3.508H9.8v1H7.5zm-.25-6.202a.83.83 0 01.207-.577c.137-.153.334-.229.59-.229.256 0 .454.076.594.23.14.152.209.345.209.576 0 .228-.07.417-.21.568-.14.15-.337.226-.593.226-.256 0-.453-.075-.59-.226a.81.81 0 01-.207-.568zM8 13A5 5 0 108 3a5 5 0 000 10zm0 1A6 6 0 118 2a6 6 0 010 12z"
d="M13.6 12.186l-1.357-1.358c-.025-.025-.058-.034-.084-.056.53-.794.84-1.746.84-2.773a4.977 4.977 0 00-.84-2.772c.026-.02.059-.03.084-.056L13.6 3.813a6.96 6.96 0 010 8.373zM8 15A6.956 6.956 0 013.814 13.6l1.358-1.358c.025-.025.034-.057.055-.084C6.02 12.688 6.974 13 8 13a4.978 4.978 0 002.773-.84c.02.026.03.058.056.083l1.357 1.358A6.956 6.956 0 018 15zm-5.601-2.813a6.963 6.963 0 010-8.373l1.359 1.358c.024.025.057.035.084.056A4.97 4.97 0 003 8c0 1.027.31 1.98.842 2.773-.027.022-.06.031-.084.056l-1.36 1.358zm5.6-.187A4 4 0 118 4a4 4 0 010 8zM8 1c1.573 0 3.019.525 4.187 1.4l-1.357 1.358c-.025.025-.035.057-.056.084A4.979 4.979 0 008 3a4.979 4.979 0 00-2.773.842c-.021-.027-.03-.059-.055-.084L3.814 2.4A6.957 6.957 0 018 1zm0-1a8.001 8.001 0 10.003 16.002A8.001 8.001 0 008 0z"
fill-rule="evenodd"
/>
</svg>
<span
class="euiCallOutHeader__title"
>
No anomalies found in the specified date range.
Choose a filled rectangle in the heat map for a more detailed view of anomalies within that entity.
</span>
</div>
</div>
Expand Down Expand Up @@ -1516,14 +1516,14 @@ exports[`<AnomalyHeatmapChart /> spec AnomalyHeatmapChart with one category fiel
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7.5 11.508L7.468 8H6.25V7h2.401l.03 3.508H9.8v1H7.5zm-.25-6.202a.83.83 0 01.207-.577c.137-.153.334-.229.59-.229.256 0 .454.076.594.23.14.152.209.345.209.576 0 .228-.07.417-.21.568-.14.15-.337.226-.593.226-.256 0-.453-.075-.59-.226a.81.81 0 01-.207-.568zM8 13A5 5 0 108 3a5 5 0 000 10zm0 1A6 6 0 118 2a6 6 0 010 12z"
d="M13.6 12.186l-1.357-1.358c-.025-.025-.058-.034-.084-.056.53-.794.84-1.746.84-2.773a4.977 4.977 0 00-.84-2.772c.026-.02.059-.03.084-.056L13.6 3.813a6.96 6.96 0 010 8.373zM8 15A6.956 6.956 0 013.814 13.6l1.358-1.358c.025-.025.034-.057.055-.084C6.02 12.688 6.974 13 8 13a4.978 4.978 0 002.773-.84c.02.026.03.058.056.083l1.357 1.358A6.956 6.956 0 018 15zm-5.601-2.813a6.963 6.963 0 010-8.373l1.359 1.358c.024.025.057.035.084.056A4.97 4.97 0 003 8c0 1.027.31 1.98.842 2.773-.027.022-.06.031-.084.056l-1.36 1.358zm5.6-.187A4 4 0 118 4a4 4 0 010 8zM8 1c1.573 0 3.019.525 4.187 1.4l-1.357 1.358c-.025.025-.035.057-.056.084A4.979 4.979 0 008 3a4.979 4.979 0 00-2.773.842c-.021-.027-.03-.059-.055-.084L3.814 2.4A6.957 6.957 0 018 1zm0-1a8.001 8.001 0 10.003 16.002A8.001 8.001 0 008 0z"
fill-rule="evenodd"
/>
</svg>
<span
class="euiCallOutHeader__title"
>
No anomalies found in the specified date range.
Choose a filled rectangle in the heat map for a more detailed view of anomalies within that entity.
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@

//@ts-ignore
import moment from 'moment';
import { getAnomalySummary } from '../anomalyChartUtils';
import {
getAnomalySummary,
convertAlerts,
generateAlertAnnotations,
} from '../anomalyChartUtils';
import { httpClientMock, coreServicesMock } from '../../../../../test/mocks';
import { MonitorAlert } from '../../../../models/interfaces';

describe('anomalyChartUtils', () => {
describe('getAnomalySummary', () => {
Expand Down Expand Up @@ -75,3 +81,80 @@ describe('anomalyChartUtils', () => {
});
});
});

describe('anomalyChartUtils function tests', () => {
const alertResponse = {
response: {
alerts: [
{
id: 'eQURa3gBKo1jAh6qUo49',
version: 300,
monitor_id: 'awUMa3gBKo1jAh6qu47E',
schema_version: 2,
monitor_version: 2,
monitor_name: 'Example_monitor_name',
monitor_user: {
name: 'admin',
backend_roles: ['admin'],
roles: ['all_access', 'own_index'],
custom_attribute_names: [],
user_requested_tenant: null,
},
trigger_id: 'bQUQa3gBKo1jAh6qnY6G',
trigger_name: 'Example_trigger_name',
state: 'ACTIVE',
error_message: null,
alert_history: [
{
timestamp: 1617314504873,
message: 'Example error emssage',
},
{
timestamp: 1617312543925,
message: 'Example error message',
},
],
severity: 1,
action_execution_results: [
{
action_id: 'bgUQa3gBKo1jAh6qnY6G',
last_execution_time: 1617317979908,
throttled_count: 0,
},
],
start_time: 1616704000492,
last_notification_time: 1617317979908,
end_time: null,
acknowledged_time: null,
},
],
totalAlerts: 1,
},
};
const alertConverted = [
{
monitorName: 'Example_monitor_name',
triggerName: 'Example_trigger_name',
severity: 1,
state: 'ACTIVE',
error: null,
startTime: 1616704000492,
endTime: null,
acknowledgedTime: null,
},
] as MonitorAlert[];
test('convertAlerts', () => {
expect(convertAlerts(alertResponse)).toStrictEqual(alertConverted);
});
test('generateAlertAnnotations', () => {
const alertsConverted = generateAlertAnnotations(alertConverted);
expect(alertsConverted).toStrictEqual([
{
dataValue: 1616704000492,
details:
'There is a severity 1 alert with state ACTIVE from 03/25/21 08:26:40 PM.',
header: '03/25/21 08:26:40 PM',
},
]);
});
});
1 change: 0 additions & 1 deletion public/pages/AnomalyCharts/utils/anomalyChartUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ export const getEntityAnomaliesHeatmapData = (
singleAnomalySummary.startTime >= timeWindow.startDate &&
singleAnomalySummary.startTime < timeWindow.endDate
);

if (isEmpty(anomalySummaryInTimeRange)) {
maxAnomalyGradesForEntity.push(0);
numAnomalyGradesForEntity.push(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('<ConfigureModel /> spec', () => {
test('validate all required fields', async () => {
const { getByText } = renderWithRouter();
fireEvent.click(getByText('Next'));
await waitFor(()=>{});
await waitFor(() => {});
getByText('You must enter a feature name');
getByText('You must select a field');
});
Expand Down
2 changes: 1 addition & 1 deletion public/pages/Dashboard/Components/AnomaliesLiveChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ export const AnomaliesLiveChart = (props: AnomaliesLiveChartProps) => {
ALL_CUSTOM_AD_RESULT_INDICES,
false
);

setLiveAnomalyData(latestLiveAnomalyResult);

setLatestLiveAnomalousDetectorsCount(
Expand All @@ -144,6 +143,7 @@ export const AnomaliesLiveChart = (props: AnomaliesLiveChartProps) => {
} else {
setLastAnomalyResult(undefined);
}

setLiveTimeRange({
startDateTime: moment().subtract(31, 'minutes'),
endDateTime: moment(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render, waitFor } from '@testing-library/react';
import React from 'react';
import { AnomaliesLiveChart } from '../AnomaliesLiveChart';
import { selectedDetectors } from '../../../../pages/utils/__tests__/constants';
import { Provider } from 'react-redux';
import { coreServicesMock } from '../../../../../test/mocks';
import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices';
import { mockedStore } from '../../../../redux/utils/testUtils';
const anomalyResponse = [
{
ok: true,
response: {
anomaly_grade: 0.10949221682655441,
data_start_time: 1651817250642,
data_end_time: 1651817310642,
detector_id: 'gtU2l4ABuV34PY9ITTdm',
name: 'test2',
},
},
];
const anomalyResultQuery = [
{
anomaly_grade: 0.10949221682655441,
data_start_time: 1651817250642,
data_end_time: 1651817310642,
detector_id: 'gtU2l4ABuV34PY9ITTdm',
},
];

jest.mock('../../utils/utils', () => ({
getLatestAnomalyResultsForDetectorsByTimeRange: jest.fn(
() => anomalyResponse
),
getFloorPlotTime: jest.fn(() => 1651817250642),
getLatestAnomalyResultsByTimeRange: jest.fn(() => anomalyResultQuery),
visualizeAnomalyResultForXYChart: jest.fn(),
}));
describe('<AnomaliesLiveChart /> spec', () => {
test('AnomaliesLiveChart with Sample anomaly data', async () => {
const { container, getByTestId, getAllByText, getByText } = render(
<Provider store={mockedStore()}>
<CoreServicesContext.Provider value={coreServicesMock}>
<AnomaliesLiveChart {...selectedDetectors} />
</CoreServicesContext.Provider>
</Provider>
);
//mock current last update to a specific date so doesn't produce new snapshot each minute
Date.now = jest.fn().mockReturnValue(new Date('2021-06-06T12:33:37.000Z'));
await waitFor(() => {
expect(
getByTestId('dashboardFullScreenButton').innerHTML.includes(
'euiIcon-isssLoaded'
)
);
});
await waitFor(() => {});
expect(container).toMatchSnapshot();
getAllByText('Detector with the most recent anomaly');
});
});
Loading

0 comments on commit f95f52b

Please sign in to comment.