From 8f5c4bd855a05233043ee1c64637fa504965770e Mon Sep 17 00:00:00 2001 From: Alison Goryachev Date: Wed, 13 Apr 2022 09:41:35 -0400 Subject: [PATCH] add tests for grok processor --- .../__jest__/processors/grok.test.ts | 107 ++++++++++++++++++ .../__jest__/processors/processor.helpers.tsx | 7 +- .../drag_and_drop_text_list.tsx | 15 ++- .../processor_form/processors/grok.test.tsx | 57 ---------- 4 files changed, 125 insertions(+), 61 deletions(-) create mode 100644 x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/grok.test.ts delete mode 100644 x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/grok.test.tsx diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/grok.test.ts b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/grok.test.ts new file mode 100644 index 0000000000000..b488e69ed84da --- /dev/null +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/grok.test.ts @@ -0,0 +1,107 @@ +/* + * 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 { act } from 'react-dom/test-utils'; +import { setup, SetupResult, getProcessorValue, setupEnvironment } from './processor.helpers'; + +const GROK_TYPE = 'grok'; + +describe('Processor: Grok', () => { + let onUpdate: jest.Mock; + let testBed: SetupResult; + let clickAddPattern: () => Promise; + const { httpSetup } = setupEnvironment(); + + beforeAll(() => { + jest.useFakeTimers(); + // disable all react-beautiful-dnd development warnings + (window as any)['__react-beautiful-dnd-disable-dev-warnings'] = true; + }); + + afterAll(() => { + jest.useRealTimers(); + // enable all react-beautiful-dnd development warnings + (window as any)['__react-beautiful-dnd-disable-dev-warnings'] = false; + }); + + beforeEach(async () => { + onUpdate = jest.fn(); + + await act(async () => { + testBed = await setup(httpSetup, { + value: { + processors: [], + }, + onFlyoutOpen: jest.fn(), + onUpdate, + }); + }); + + const { find, component, actions } = testBed; + + clickAddPattern = async () => { + await act(async () => { + find('droppableList.addButton').simulate('click'); + }); + component.update(); + }; + + component.update(); + + // Open flyout to add new processor + actions.addProcessor(); + // Add type (the other fields are not visible until a type is selected) + await actions.addProcessorType(GROK_TYPE); + }); + + test('prevents form submission if required fields are not provided', async () => { + const { + actions: { saveNewProcessor }, + form, + exists, + } = testBed; + + // Click submit button with only the type defined + await saveNewProcessor(); + + // Expect form error as "field" is a required parameter + expect(form.getErrorsMessages()).toEqual(['A field value is required.']); + // Patterns field is also required; it uses EuiDraggable and only shows an error icon when invalid + expect(exists('droppableList.errorIcon')).toBe(true); + }); + + test('saves with default parameter values', async () => { + const { + actions: { saveNewProcessor }, + form, + } = testBed; + + // Add "field" value + form.setInputValue('fieldNameField.input', 'test_grok_processor'); + + // Add pattern 1 + form.setInputValue('droppableList.input-0', 'pattern1'); + + // Add pattern 2 + await clickAddPattern(); + form.setInputValue('droppableList.input-1', 'pattern2'); + + // Add pattern 3 + await clickAddPattern(); + form.setInputValue('droppableList.input-2', 'pattern3'); + + // Save the field + await saveNewProcessor(); + + const processors = getProcessorValue(onUpdate, GROK_TYPE); + + expect(processors[0][GROK_TYPE]).toEqual({ + field: 'test_grok_processor', + patterns: ['pattern1', 'pattern2', 'pattern3'], + }); + }); +}); diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/processor.helpers.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/processor.helpers.tsx index 0e7431964c84b..89a7be5ca5cae 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/processor.helpers.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/processor.helpers.tsx @@ -180,4 +180,9 @@ type TestSubject = | 'transportField.input' | 'seedField.input' | 'copyFromInput' - | 'trimSwitch.input'; + | 'trimSwitch.input' + | 'droppableList.addButton' + | 'droppableList.errorIcon' + | 'droppableList.input-0' + | 'droppableList.input-1' + | 'droppableList.input-2'; diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/field_components/drag_and_drop_text_list.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/field_components/drag_and_drop_text_list.tsx index 7a0ea533b0d68..d574479b03e7b 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/field_components/drag_and_drop_text_list.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/field_components/drag_and_drop_text_list.tsx @@ -87,7 +87,12 @@ function DragAndDropTextListComponent({ [onMove] ); return ( - + <> {/* Label and help text. Also wire up the htmlFor so the label points to the first text field. */} {typeof errorMessage === 'string' && ( -
+
- + {addLabel}
diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/grok.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/grok.test.tsx deleted file mode 100644 index 7d556cc41d109..0000000000000 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/grok.test.tsx +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 { - uiSettingsServiceMock, - i18nServiceMock, -} from '../../../../../../../../../../src/core/public/mocks'; - -import { Form, useForm, KibanaContextProvider } from '../../../../../../shared_imports'; -import { Grok } from './grok'; - -// @ts-ignore -window.Worker = function () { - this.postMessage = () => {}; - (this as any).terminate = () => {}; -}; - -describe('', () => { - const setup = (props?: { defaultValue: Record }) => { - function MyComponent() { - const { form } = useForm({ defaultValue: props?.defaultValue }); - const i18n = i18nServiceMock.createStartContract(); - return ( - - -
- - -
-
- ); - } - return mount(); - }; - - beforeAll(() => { - // disable all react-beautiful-dnd development warnings - (window as any)['__react-beautiful-dnd-disable-dev-warnings'] = true; - }); - - afterAll(() => { - // enable all react-beautiful-dnd development warnings - (window as any)['__react-beautiful-dnd-disable-dev-warnings'] = false; - }); - test('smoke', () => { - setup({ defaultValue: { type: 'grok', fields: { patterns: ['test'] } } }); - }); -});