|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { mountWithIntl, nextTick } from 'test_utils/enzyme_helpers'; |
| 8 | +import { actionTypeRegistryMock } from '../../../../../triggers_actions_ui/public/application/action_type_registry.mock'; |
| 9 | +import { alertTypeRegistryMock } from '../../../../../triggers_actions_ui/public/application/alert_type_registry.mock'; |
| 10 | +import { coreMock } from '../../../../../../../src/core/public/mocks'; |
| 11 | +import { AlertsContextValue } from '../../../../../triggers_actions_ui/public/application/context/alerts_context'; |
| 12 | +// eslint-disable-next-line @kbn/eslint/no-restricted-paths |
| 13 | +import { InventoryMetricConditions } from '../../../../server/lib/alerting/inventory_metric_threshold/types'; |
| 14 | +import React from 'react'; |
| 15 | +import { Expressions, AlertContextMeta, ExpressionRow } from './expression'; |
| 16 | +import { act } from 'react-dom/test-utils'; |
| 17 | +// eslint-disable-next-line @kbn/eslint/no-restricted-paths |
| 18 | +import { Comparator } from '../../../../server/lib/alerting/metric_threshold/types'; |
| 19 | +import { SnapshotCustomMetricInput } from '../../../../common/http_api/snapshot_api'; |
| 20 | + |
| 21 | +jest.mock('../../../containers/source/use_source_via_http', () => ({ |
| 22 | + useSourceViaHttp: () => ({ |
| 23 | + source: { id: 'default' }, |
| 24 | + createDerivedIndexPattern: () => ({ fields: [], title: 'metricbeat-*' }), |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +const exampleCustomMetric = { |
| 29 | + id: 'this-is-an-id', |
| 30 | + field: 'some.system.field', |
| 31 | + aggregation: 'rate', |
| 32 | + type: 'custom', |
| 33 | +} as SnapshotCustomMetricInput; |
| 34 | + |
| 35 | +describe('Expression', () => { |
| 36 | + async function setup(currentOptions: AlertContextMeta) { |
| 37 | + const alertParams = { |
| 38 | + criteria: [], |
| 39 | + nodeType: undefined, |
| 40 | + filterQueryText: '', |
| 41 | + }; |
| 42 | + |
| 43 | + const mocks = coreMock.createSetup(); |
| 44 | + const startMocks = coreMock.createStart(); |
| 45 | + const [ |
| 46 | + { |
| 47 | + application: { capabilities }, |
| 48 | + }, |
| 49 | + ] = await mocks.getStartServices(); |
| 50 | + |
| 51 | + const context: AlertsContextValue<AlertContextMeta> = { |
| 52 | + http: mocks.http, |
| 53 | + toastNotifications: mocks.notifications.toasts, |
| 54 | + actionTypeRegistry: actionTypeRegistryMock.create() as any, |
| 55 | + alertTypeRegistry: alertTypeRegistryMock.create() as any, |
| 56 | + docLinks: startMocks.docLinks, |
| 57 | + capabilities: { |
| 58 | + ...capabilities, |
| 59 | + actions: { |
| 60 | + delete: true, |
| 61 | + save: true, |
| 62 | + show: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + metadata: currentOptions, |
| 66 | + }; |
| 67 | + |
| 68 | + const wrapper = mountWithIntl( |
| 69 | + <Expressions |
| 70 | + alertsContext={context} |
| 71 | + alertInterval="1m" |
| 72 | + alertParams={alertParams as any} |
| 73 | + errors={[]} |
| 74 | + setAlertParams={(key, value) => Reflect.set(alertParams, key, value)} |
| 75 | + setAlertProperty={() => {}} |
| 76 | + /> |
| 77 | + ); |
| 78 | + |
| 79 | + const update = async () => |
| 80 | + await act(async () => { |
| 81 | + await nextTick(); |
| 82 | + wrapper.update(); |
| 83 | + }); |
| 84 | + |
| 85 | + await update(); |
| 86 | + |
| 87 | + return { wrapper, update, alertParams }; |
| 88 | + } |
| 89 | + |
| 90 | + it('should prefill the alert using the context metadata', async () => { |
| 91 | + const currentOptions = { |
| 92 | + filter: 'foo', |
| 93 | + nodeType: 'pod', |
| 94 | + customMetrics: [], |
| 95 | + options: { metric: { type: 'memory' } }, |
| 96 | + }; |
| 97 | + const { alertParams } = await setup(currentOptions as AlertContextMeta); |
| 98 | + expect(alertParams.nodeType).toBe('pod'); |
| 99 | + expect(alertParams.filterQueryText).toBe('foo'); |
| 100 | + expect(alertParams.criteria).toEqual([ |
| 101 | + { |
| 102 | + metric: 'memory', |
| 103 | + comparator: Comparator.GT, |
| 104 | + threshold: [], |
| 105 | + timeSize: 1, |
| 106 | + timeUnit: 'm', |
| 107 | + }, |
| 108 | + ]); |
| 109 | + }); |
| 110 | + describe('using custom metrics', () => { |
| 111 | + it('should prefill the alert using the context metadata', async () => { |
| 112 | + const currentOptions = { |
| 113 | + filter: '', |
| 114 | + nodeType: 'tx', |
| 115 | + customMetrics: [exampleCustomMetric], |
| 116 | + options: { metric: exampleCustomMetric }, |
| 117 | + }; |
| 118 | + const { alertParams, update } = await setup(currentOptions as AlertContextMeta); |
| 119 | + await update(); |
| 120 | + expect(alertParams.nodeType).toBe('tx'); |
| 121 | + expect(alertParams.filterQueryText).toBe(''); |
| 122 | + expect(alertParams.criteria).toEqual([ |
| 123 | + { |
| 124 | + metric: 'custom', |
| 125 | + comparator: Comparator.GT, |
| 126 | + threshold: [], |
| 127 | + timeSize: 1, |
| 128 | + timeUnit: 'm', |
| 129 | + customMetric: exampleCustomMetric, |
| 130 | + }, |
| 131 | + ]); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
| 135 | + |
| 136 | +describe('ExpressionRow', () => { |
| 137 | + async function setup(expression: InventoryMetricConditions) { |
| 138 | + const wrapper = mountWithIntl( |
| 139 | + <ExpressionRow |
| 140 | + nodeType="host" |
| 141 | + canDelete={false} |
| 142 | + remove={() => {}} |
| 143 | + addExpression={() => {}} |
| 144 | + key={1} |
| 145 | + expressionId={1} |
| 146 | + setAlertParams={() => {}} |
| 147 | + errors={{ |
| 148 | + aggField: [], |
| 149 | + timeSizeUnit: [], |
| 150 | + timeWindowSize: [], |
| 151 | + metric: [], |
| 152 | + }} |
| 153 | + expression={expression} |
| 154 | + alertsContextMetadata={{ |
| 155 | + customMetrics: [], |
| 156 | + }} |
| 157 | + /> |
| 158 | + ); |
| 159 | + |
| 160 | + const update = async () => |
| 161 | + await act(async () => { |
| 162 | + await nextTick(); |
| 163 | + wrapper.update(); |
| 164 | + }); |
| 165 | + |
| 166 | + await update(); |
| 167 | + |
| 168 | + return { wrapper, update }; |
| 169 | + } |
| 170 | + const expression = { |
| 171 | + metric: 'custom', |
| 172 | + comparator: Comparator.GT, |
| 173 | + threshold: [], |
| 174 | + timeSize: 1, |
| 175 | + timeUnit: 'm', |
| 176 | + customMetric: exampleCustomMetric, |
| 177 | + }; |
| 178 | + |
| 179 | + it('loads custom metrics passed in through the expression, even with an empty context', async () => { |
| 180 | + const { wrapper } = await setup(expression as InventoryMetricConditions); |
| 181 | + const [valueMatch] = |
| 182 | + wrapper.html().match('<span class="euiExpression__value">Rate of some.system.field</span>') ?? |
| 183 | + []; |
| 184 | + expect(valueMatch).toBeTruthy(); |
| 185 | + }); |
| 186 | +}); |
0 commit comments