Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended AlertContextValue with metadata optional property #59391

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,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 @@ -23,6 +23,7 @@ export interface AlertsContextValue {
>;
charts?: ChartsPluginSetup;
dataFieldsFormats?: DataPublicPluginSetup['fieldFormats'];
metadata?: Record<string, any>;
phillipb marked this conversation as resolved.
Show resolved Hide resolved
}

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();
});
});