From dc8329ed0de4bf82aeb81988e86056390f6fd53d Mon Sep 17 00:00:00 2001 From: John Dorlus Date: Tue, 11 May 2021 16:41:56 -0400 Subject: [PATCH] Added CIT test for the fail processor. (#95261) * Added CIT test for the fail processor. * Fixed linter issues. * Used new functions and helpers. * Added common test to ensure that a processor type was selected before saving. Fixed some titles and nits from the PR feedback. * Removed unused var. * Merged in master and updated tests to remove repeat code. --- .../common_processor_fields.test.tsx | 15 ++++ .../__jest__/processors/fail.test.tsx | 77 +++++++++++++++++++ .../__jest__/processors/processor.helpers.tsx | 1 + .../processor_form/processors/fail.tsx | 9 ++- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/fail.test.tsx diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/common_processor_fields.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/common_processor_fields.test.tsx index af4c187df75e6..af5c8c50abf7b 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/common_processor_fields.test.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/common_processor_fields.test.tsx @@ -37,6 +37,21 @@ describe('Processor: Common Fields For All Processors', () => { testBed.component.update(); }); + test('prevents form submission if required type field is not provided', async () => { + const { + actions: { addProcessor, saveNewProcessor }, + form, + } = testBed; + + // Open flyout to add new processor + addProcessor(); + // Click submit button without entering any fields + await saveNewProcessor(); + + // Expect form error as a processor type is required + expect(form.getErrorsMessages()).toEqual(['A type is required.']); + }); + test('saves with common fields set', async () => { const { actions: { addProcessor, saveNewProcessor, addProcessorType }, diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/fail.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/fail.test.tsx new file mode 100644 index 0000000000000..db5840379536a --- /dev/null +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/fail.test.tsx @@ -0,0 +1,77 @@ +/* + * 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 } from './processor.helpers'; + +const FAIL_TYPE = 'fail'; +describe('Processor: Fail', () => { + let onUpdate: jest.Mock; + let testBed: SetupResult; + + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + + beforeEach(async () => { + onUpdate = jest.fn(); + + await act(async () => { + testBed = await setup({ + value: { + processors: [], + }, + onFlyoutOpen: jest.fn(), + onUpdate, + }); + }); + testBed.component.update(); + + const { + actions: { addProcessor, addProcessorType }, + } = testBed; + // Open the processor flyout + addProcessor(); + + // Add type (the other fields are not visible until a type is selected) + await addProcessorType(FAIL_TYPE); + }); + + test('prevents form submission if required message field is not provided', async () => { + const { + actions: { saveNewProcessor }, + form, + } = testBed; + + // Click submit button with only the type defined + await saveNewProcessor(); + + // Expect form error as "field" is required parameter + expect(form.getErrorsMessages()).toEqual(['A message is required.']); + }); + + test('saves with required parameter value', async () => { + const { + actions: { saveNewProcessor }, + form, + } = testBed; + + // Add "message" value (required) + form.setInputValue('messageField.input', 'Test Error Message'); + // Save the field + await saveNewProcessor(); + + const processors = getProcessorValue(onUpdate, FAIL_TYPE); + expect(processors[0].fail).toEqual({ + message: 'Test Error Message', + }); + }); +}); 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 68f494a56af5d..797d26f2404ab 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 @@ -140,6 +140,7 @@ type TestSubject = | 'appendValueField.input' | 'processorTypeSelector.input' | 'fieldNameField.input' + | 'messageField.input' | 'mockCodeEditor' | 'tagField.input' | 'ignoreMissingSwitch.input' diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/fail.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/fail.tsx index f2fe6d6ef95f6..156aa9ae1545a 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/fail.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/fail.tsx @@ -37,5 +37,12 @@ const fieldsConfig: FieldsConfig = { }; export const Fail: FunctionComponent = () => { - return ; + return ( + + ); };