Skip to content

Commit

Permalink
[7.x] Extended AlertContextValue with metadata optional property (#59391
Browse files Browse the repository at this point in the history
) (#59474)

* no message

* Fixed type check

* changed ci groups
  • Loading branch information
YulNaumenko authored Mar 6, 2020
1 parent f003149 commit c8c3ebb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export const IndexThresholdAlertTypeExpression: React.FunctionComponent<IndexThr
groupBy: groupBy ?? DEFAULT_VALUES.GROUP_BY,
threshold: threshold ?? DEFAULT_VALUES.THRESHOLD,
});
if (index.length > 0) {

if (index && index.length > 0) {
const currentEsFields = await getFields(index);
const timeFields = getTimeFieldOptions(currentEsFields as any);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FieldFormatsRegistry } from 'src/plugins/data/common/field_formats';
import { TypeRegistry } from '../type_registry';
import { AlertTypeModel, ActionTypeModel } from '../../types';

export interface AlertsContextValue {
export interface AlertsContextValue<MetaData = Record<string, any>> {
reloadAlerts?: () => Promise<void>;
http: HttpSetup;
alertTypeRegistry: TypeRegistry<AlertTypeModel>;
Expand All @@ -23,6 +23,7 @@ export interface AlertsContextValue {
>;
charts?: ChartsPluginSetup;
dataFieldsFormats?: Pick<FieldFormatsRegistry, 'register'>;
metadata?: MetaData;
}

const AlertsContext = createContext<AlertsContextValue>(null as any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,35 @@
import * as React from 'react';
import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers';
import { act } from 'react-dom/test-utils';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFormLabel } from '@elastic/eui';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { AlertAdd } from './alert_add';
import { actionTypeRegistryMock } from '../../action_type_registry.mock';
import { ValidationResult } from '../../../types';
import { AlertsContextProvider } from '../../context/alerts_context';
import { AlertsContextProvider, useAlertsContext } from '../../context/alerts_context';
import { alertTypeRegistryMock } from '../../alert_type_registry.mock';
import { chartPluginMock } from '../../../../../../../src/plugins/charts/public/mocks';
import { dataPluginMock } from '../../../../../../../src/plugins/data/public/mocks';
import { ReactWrapper } from 'enzyme';
const actionTypeRegistry = actionTypeRegistryMock.create();
const alertTypeRegistry = alertTypeRegistryMock.create();

export const TestExpression: React.FunctionComponent<any> = () => {
const alertsContext = useAlertsContext();
const { metadata } = alertsContext;

return (
<EuiFormLabel>
<FormattedMessage
defaultMessage="Metadata: {val}. Fields: {fields}."
id="xpack.triggersActionsUI.sections.alertAdd.metadataTest"
values={{ val: metadata!.test, fields: metadata!.fields.join(' ') }}
/>
</EuiFormLabel>
);
};

describe('alert_add', () => {
let deps: any;
let wrapper: ReactWrapper<any>;
Expand All @@ -41,7 +58,7 @@ describe('alert_add', () => {
validate: (): ValidationResult => {
return { errors: {} };
},
alertParamsExpression: () => <React.Fragment />,
alertParamsExpression: TestExpression,
};

const actionTypeModel = {
Expand Down Expand Up @@ -77,13 +94,10 @@ describe('alert_add', () => {
alertTypeRegistry: deps.alertTypeRegistry,
toastNotifications: deps.toastNotifications,
uiSettings: deps.uiSettings,
metadata: { test: 'some value', fields: ['test'] },
}}
>
<AlertAdd
consumer={'alerting'}
addFlyoutVisible={true}
setAddFlyoutVisibility={state => {}}
/>
<AlertAdd consumer={'alerting'} addFlyoutVisible={true} setAddFlyoutVisibility={() => {}} />
</AlertsContextProvider>
);
// Wait for active space to resolve before requesting the component to update
Expand All @@ -97,5 +111,10 @@ describe('alert_add', () => {
await setup();
expect(wrapper.find('[data-test-subj="addAlertFlyoutTitle"]').exists()).toBeTruthy();
expect(wrapper.find('[data-test-subj="saveAlertButton"]').exists()).toBeTruthy();
wrapper
.find('[data-test-subj="my-alert-type-SelectOption"]')
.first()
.simulate('click');
expect(wrapper.contains('Metadata: some value. Fields: test.')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default function alertingApiIntegrationTests({
const esArchiver = getService('esArchiver');

describe('alerting api integration security and spaces enabled', function() {
this.tags('ciGroup3');
this.tags('ciGroup5');

before(async () => {
for (const space of Spaces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function alertingApiIntegrationTests({
const esArchiver = getService('esArchiver');

describe('alerting api integration spaces only', function() {
this.tags('ciGroup3');
this.tags('ciGroup9');

before(async () => {
for (const space of Object.values(Spaces)) {
Expand Down

0 comments on commit c8c3ebb

Please sign in to comment.