Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CIT test for the fail processor. #95261

Merged
merged 10 commits into from
May 11, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 } from './processor.helpers';

// Default parameter values automatically added to the URI parts processor when saved
const defaultFailParameters = {
ignore_failure: undefined,
if: undefined,
tag: undefined,
description: undefined,
};

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();
});

test('prevents form submission if required fields are not provided', async () => {
const {
actions: { addProcessor, saveNewProcessor, addProcessorType },
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.']);
cuff-links marked this conversation as resolved.
Show resolved Hide resolved

// Add type (the other fields are not visible until a type is selected)
await addProcessorType({ type: 'fail', label: 'Fail' });

// 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 default parameter values', async () => {
cuff-links marked this conversation as resolved.
Show resolved Hide resolved
const {
actions: { addProcessor, saveNewProcessor, addProcessorType },
form,
} = testBed;

// Open flyout to add new processor
addProcessor();
// Add type (the other fields are not visible until a type is selected)
await addProcessorType({ type: 'fail', label: 'Fail' });
// Add "message" value (required)
form.setInputValue('messageField.input', 'Test Error Message');
// Save the field
await saveNewProcessor();

const [onUpdateResult] = onUpdate.mock.calls[onUpdate.mock.calls.length - 1];
const { processors } = onUpdateResult.getData();
expect(processors[0].fail).toEqual({
message: 'Test Error Message',
...defaultFailParameters,
});
});

test('allows optional parameters to be set', async () => {
const {
actions: { addProcessor, addProcessorType, saveNewProcessor },
form,
} = testBed;

// Open flyout to add new processor
addProcessor();
// Add type (the other fields are not visible until a type is selected)
await addProcessorType({ type: 'fail', label: 'Fail' });
// Add "message" value (required)
form.setInputValue('messageField.input', 'Test Error Message');

// Save the field with new changes
await saveNewProcessor();

const [onUpdateResult] = onUpdate.mock.calls[onUpdate.mock.calls.length - 1];
const { processors } = onUpdateResult.getData();
expect(processors[0].fail).toEqual({
message: 'Test Error Message',
ignore_failure: undefined,
if: undefined,
tag: undefined,
description: undefined,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type TestSubject =
| 'addProcessorForm.submitButton'
| 'processorTypeSelector.input'
| 'fieldNameField.input'
| 'messageField.input'
| 'targetField.input'
| 'keepOriginalField.input'
| 'removeIfSuccessfulField.input';
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,12 @@ const fieldsConfig: FieldsConfig = {
};

export const Fail: FunctionComponent = () => {
return <UseField component={Field} config={fieldsConfig.message} path="fields.message" />;
return (
<UseField
data-test-subj="messageField"
component={Field}
config={fieldsConfig.message}
path="fields.message"
/>
);
};