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 test for the date processor. #103415

Merged
merged 5 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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 DATE_TYPE = 'date';

describe('Processor: Date', () => {
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(DATE_TYPE);
});

test('prevents form submission when field and format 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 "value" are required parameters
expect(form.getErrorsMessages()).toEqual([
'A field value is required.',
'A value for formats is required.',
]);
});

test('saves with required field and formats parameter values', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;

// Add "field" value (required)
form.setInputValue('fieldNameField.input', 'field_1');

await act(async () => {
find('formatsValueField.input').simulate('change', [{ label: 'ISO8601' }]);
});
component.update();

// Save the field
await saveNewProcessor();

const processors = getProcessorValue(onUpdate, DATE_TYPE);
expect(processors[0].date).toEqual({
field: 'field_1',
formats: ['ISO8601'],
});
});

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

form.setInputValue('fieldNameField.input', 'field_1');

// Set optional parameteres
cuff-links marked this conversation as resolved.
Show resolved Hide resolved
await act(async () => {
find('formatsValueField.input').simulate('change', [{ label: 'ISO8601' }]);
});
component.update();

// Set target field
form.setInputValue('targetField.input', 'target_field');

// Set locale field
form.setInputValue('localeField.input', 'SPANISH');

// Set timezone field.
form.setInputValue('timezoneField.input', 'EST');

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

const processors = getProcessorValue(onUpdate, DATE_TYPE);
expect(processors[0].date).toEqual({
field: 'field_1',
formats: ['ISO8601'],
target_field: 'target_field',
locale: 'SPANISH',
timezone: 'EST',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ type TestSubject =
| 'addProcessorButton'
| 'addProcessorForm.submitButton'
| 'appendValueField.input'
| 'formatsValueField.input'
| 'timezoneField.input'
| 'localeField.input'
| 'processorTypeSelector.input'
| 'fieldNameField.input'
| 'messageField.input'
Expand All @@ -148,9 +151,9 @@ type TestSubject =
| 'ignoreFailureSwitch.input'
| 'ifField.textarea'
| 'targetField.input'
| 'targetFieldsField.input'
| 'keepOriginalField.input'
| 'removeIfSuccessfulField.input'
| 'targetFieldsField.input'
| 'shapeSelectorField'
| 'errorDistanceField.input'
| 'separatorValueField.input'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export const DateProcessor: FunctionComponent = () => {
)}
/>

<UseField config={fieldsConfig.formats} component={ComboBoxField} path="fields.formats" />
<UseField
data-test-subj="formatsValueField"
config={fieldsConfig.formats}
component={ComboBoxField}
path="fields.formats"
/>

<TargetField
helpText={
Expand All @@ -108,9 +113,19 @@ export const DateProcessor: FunctionComponent = () => {
}
/>

<UseField config={fieldsConfig.timezone} component={Field} path="fields.timezone" />
<UseField
data-test-subj="timezoneField"
config={fieldsConfig.timezone}
component={Field}
path="fields.timezone"
/>

<UseField config={fieldsConfig.locale} component={Field} path="fields.locale" />
<UseField
data-test-subj="localeField"
config={fieldsConfig.locale}
component={Field}
path="fields.locale"
/>
</>
);
};