From 98737062a86f7b4b942b09dde06c6cdc81d5bcf2 Mon Sep 17 00:00:00 2001 From: John Dorlus Date: Fri, 30 Jul 2021 20:23:01 -0400 Subject: [PATCH 1/3] Added tests for dot notation processors. --- .../__jest__/processors/dot_expander.test.tsx | 121 ++++++++++++++++++ .../__jest__/processors/processor.helpers.tsx | 1 + .../processors/dot_expander.tsx | 7 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx new file mode 100644 index 0000000000000..0a99a817333a7 --- /dev/null +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx @@ -0,0 +1,121 @@ +/* + * 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 DOT_EXPANDER_TYPE = 'dot_expander'; + +describe('Processor: Dot Expander', () => { + 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(); + + // Open flyout to add new processor + testBed.actions.addProcessor(); + // Add type (the other fields are not visible until a type is selected) + await testBed.actions.addProcessorType(DOT_EXPANDER_TYPE); + }); + + test('prevents form submission if required fields are not provided', async () => { + const { + actions: { saveNewProcessor }, + form, + } = testBed; + + // Click submit button with only the type defined + await saveNewProcessor(); + + // Expect form error as "field" and "target_field" are required parameters + expect(form.getErrorsMessages()).toEqual(['A field value is required.']); + }); + + test('prevents form submission if field does not contain a . for the dot notation', async () => { + const { + actions: { saveNewProcessor }, + form, + component, + } = testBed; + + // Add "field" value (required) + form.setInputValue('fieldNameField.input', 'missing'); + component.update(); + + // Click submit button with only the type defined + await saveNewProcessor(); + + // Expect form error as "field" and "target_field" are required parameters + expect(form.getErrorsMessages()).toEqual([ + 'A field value requires at least one dot character.', + ]); + }); + test('saves with default parameter values', async () => { + const { + actions: { saveNewProcessor }, + form, + find, + component, + } = testBed; + + // Add "field" value (required) + form.setInputValue('fieldNameField.input', 'field.with.dot'); + + // Save the field + await saveNewProcessor(); + + const processors = getProcessorValue(onUpdate, DOT_EXPANDER_TYPE); + expect(processors[0][DOT_EXPANDER_TYPE]).toEqual({ + field: 'field.with.dot', + }); + }); + + test('allows optional parameters to be set', async () => { + const { + actions: { saveNewProcessor }, + form, + find, + component, + } = testBed; + + // Add "field" value (required) + form.setInputValue('fieldNameField.input', 'field.notation'); + + // Set optional parameters + form.setInputValue('pathField.input', 'somepath'); + + // Save the field with new changes + await saveNewProcessor(); + + const processors = getProcessorValue(onUpdate, DOT_EXPANDER_TYPE); + expect(processors[0][DOT_EXPANDER_TYPE]).toEqual({ + field: 'field.notation', + path: 'somepath', + }); + }); +}); 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 9101e64278dc6..65d9b8f306058 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 @@ -146,6 +146,7 @@ type TestSubject = | 'fieldNameField.input' | 'messageField.input' | 'mockCodeEditor' + | 'pathField.input' | 'tagField.input' | 'typeSelectorField' | 'dateRoundingField' diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/dot_expander.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/dot_expander.tsx index 4bbc242cf0ef8..c66633dfd23d5 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/dot_expander.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/dot_expander.tsx @@ -54,7 +54,12 @@ export const DotExpander: FunctionComponent = () => { ]} /> - + ); }; From c1ad0f4924d1de2d6898f0a592b834cef9d29b19 Mon Sep 17 00:00:00 2001 From: John Dorlus Date: Mon, 16 Aug 2021 22:39:42 -0400 Subject: [PATCH 2/3] Fixed nits in PR. --- .../__jest__/processors/dot_expander.test.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx index 0a99a817333a7..0bbe918af9eca 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx @@ -52,7 +52,7 @@ describe('Processor: Dot Expander', () => { // Click submit button with only the type defined await saveNewProcessor(); - // Expect form error as "field" and "target_field" are required parameters + // Expect form error as "field" is a required parameter expect(form.getErrorsMessages()).toEqual(['A field value is required.']); }); @@ -63,14 +63,19 @@ describe('Processor: Dot Expander', () => { component, } = testBed; - // Add "field" value (required) - form.setInputValue('fieldNameField.input', 'missing'); - component.update(); + // Add invalid "field" value (required) + form.setInputValue('fieldNameField.input', 'missingTheDot'); - // Click submit button with only the type defined + // Save the processor with invalid field await saveNewProcessor(); - // Expect form error as "field" and "target_field" are required parameters + // Move ahead the debounce time which will then execute any validations + await act(async () => { + jest.runAllTimers(); + }); + component.update(); + + // Expect form error as "field" does not contain '.' expect(form.getErrorsMessages()).toEqual([ 'A field value requires at least one dot character.', ]); From c9ddb068bba54b7a48ea86c0cac902fc7cbc2f9e Mon Sep 17 00:00:00 2001 From: John Dorlus Date: Tue, 17 Aug 2021 14:28:12 -0400 Subject: [PATCH 3/3] Fixed linting issues. --- .../pipeline_editor/__jest__/processors/dot_expander.test.tsx | 4 ---- 1 file changed, 4 deletions(-) diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx index 0bbe918af9eca..75468f31b1a54 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/__jest__/processors/dot_expander.test.tsx @@ -84,8 +84,6 @@ describe('Processor: Dot Expander', () => { const { actions: { saveNewProcessor }, form, - find, - component, } = testBed; // Add "field" value (required) @@ -104,8 +102,6 @@ describe('Processor: Dot Expander', () => { const { actions: { saveNewProcessor }, form, - find, - component, } = testBed; // Add "field" value (required)