From a276cfcd3cda0e9dd531d979cad015825ebd1ae6 Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 14 Jun 2021 13:43:23 -0600 Subject: [PATCH] Add snapshot. Export index pattern validator function for test --- .../entity_by_expression.test.tsx.snap | 171 ++++++++++++++++++ .../expressions/entity_by_expression.test.tsx | 94 ++++++++++ .../expressions/entity_by_expression.tsx | 2 +- 3 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/__snapshots__/entity_by_expression.test.tsx.snap create mode 100644 x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.test.tsx diff --git a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/__snapshots__/entity_by_expression.test.tsx.snap b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/__snapshots__/entity_by_expression.test.tsx.snap new file mode 100644 index 00000000000000..d9dd6ec4a0be53 --- /dev/null +++ b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/__snapshots__/entity_by_expression.test.tsx.snap @@ -0,0 +1,171 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`should render entity by expression with aggregatable field options for entity 1`] = ` + + + + + } + value="FlightNum" + > + + } + closePopover={[Function]} + display="block" + hasArrow={true} + id="popoverForExpression" + isOpen={false} + ownFocus={true} + panelPaddingSize="m" + zIndex={8000} + > +
+
+ + + +
+
+
+
+
+`; diff --git a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.test.tsx b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.test.tsx new file mode 100644 index 00000000000000..31b89873922c9c --- /dev/null +++ b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.test.tsx @@ -0,0 +1,94 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { mount } from 'enzyme'; +import { EntityByExpression, getValidIndexPatternFields } from './entity_by_expression'; + +const defaultProps = { + errors: { + index: [], + indexId: [], + geoField: [], + entity: [], + dateField: [], + boundaryType: [], + boundaryIndexTitle: [], + boundaryIndexId: [], + boundaryGeoField: [], + name: ['Name is required.'], + interval: [], + alertTypeId: [], + actionConnectors: [], + }, + entity: 'FlightNum', + setAlertParamsEntity: (arg: string) => {}, + indexFields: [ + { + count: 0, + name: 'DestLocation', + type: 'geo_point', + esTypes: ['geo_point'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + count: 0, + name: 'FlightNum', + type: 'string', + esTypes: ['keyword'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + count: 0, + name: 'OriginLocation', + type: 'geo_point', + esTypes: ['geo_point'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + { + count: 0, + name: 'timestamp', + type: 'date', + esTypes: ['date'], + scripted: false, + searchable: true, + aggregatable: true, + readFromDocValues: true, + }, + ], + isInvalid: false, +}; + +test('should render entity by expression with aggregatable field options for entity', async () => { + const component = mount(); + expect(component).toMatchSnapshot(); +}); +// + +test('should only use valid index fields', async () => { + // Only the string index field should match + const indexFields = getValidIndexPatternFields(defaultProps.indexFields); + expect(indexFields.length).toEqual(1); + + // Set all agg fields to false, invalidating them for use + const invalidIndexFields = defaultProps.indexFields.map((field) => ({ + ...field, + aggregatable: false, + })); + + const noIndexFields = getValidIndexPatternFields(invalidIndexFields); + expect(noIndexFields.length).toEqual(0); +}); diff --git a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.tsx b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.tsx index e10751785cea00..a194bd40d9931f 100644 --- a/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.tsx +++ b/x-pack/plugins/stack_alerts/public/alert_types/geo_containment/query_builder/expressions/entity_by_expression.tsx @@ -23,7 +23,7 @@ interface Props { } const ENTITY_TYPES = ['string', 'number', 'ip']; -function getValidIndexPatternFields(fields: IFieldType[]): IFieldType[] { +export function getValidIndexPatternFields(fields: IFieldType[]): IFieldType[] { return fields.filter((field) => { const isSpecifiedSupportedField = ENTITY_TYPES.includes(field.type); const hasLeadingUnderscore = field.name.startsWith('_');