Skip to content

Commit

Permalink
[Mappings editor] Add scaled_float and date_range comp integration te…
Browse files Browse the repository at this point in the history
…sts (#81287)
  • Loading branch information
sebelga authored Oct 21, 2020
1 parent 3df30e0 commit cd19bd3
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { act } from 'react-dom/test-utils';

import { componentHelpers, MappingsEditorTestBed } from '../helpers';

const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;

// Parameters automatically added to the date range datatype when saved (with the default values)
export const defaultDateRangeParameters = {
type: 'date_range',
coerce: true,
index: true,
store: false,
};

describe('Mappings editor: date range datatype', () => {
/**
* Variable to store the mappings data forwarded to the consumer component
*/
let data: any;
let onChangeHandler: jest.Mock = jest.fn();
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
let testBed: MappingsEditorTestBed;

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

beforeEach(() => {
onChangeHandler = jest.fn();
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
});

test('should require a scaling factor to be provided', async () => {
const defaultMappings = {
properties: {
myField: {
type: 'double_range',
},
},
};

const updatedMappings = { ...defaultMappings };

await act(async () => {
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
});
testBed.component.update();

const {
component,
find,
exists,
actions: { startEditField, updateFieldAndCloseFlyout, toggleFormRow },
} = testBed;

// Open the flyout to edit the field
await startEditField('myField');

expect(exists('formatParameter')).toBe(false);

// Change the type to "date_range"
await act(async () => {
find('mappingsEditorFieldEdit.fieldSubType').simulate('change', [
{
label: 'Date range',
value: 'date_range',
},
]);
});
component.update();

expect(exists('formatParameter')).toBe(true);
expect(exists('formatParameter.formatInput')).toBe(false);
toggleFormRow('formatParameter');
expect(exists('formatParameter.formatInput')).toBe(true);

await act(async () => {
find('formatParameter.formatInput').simulate('change', [{ label: 'customDateFormat' }]);
});
component.update();

await updateFieldAndCloseFlyout();

// It should have the default parameters values added, plus the scaling factor
updatedMappings.properties.myField = {
...defaultDateRangeParameters,
format: 'customDateFormat',
} as any;

({ data } = await getMappingsEditorData(component));
expect(data).toEqual(updatedMappings);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { act } from 'react-dom/test-utils';

import { componentHelpers, MappingsEditorTestBed } from '../helpers';

const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;

// Parameters automatically added to the scaled float datatype when saved (with the default values)
export const defaultScaledFloatParameters = {
type: 'scaled_float',
coerce: true,
doc_values: true,
ignore_malformed: false,
index: true,
store: false,
};

describe('Mappings editor: scaled float datatype', () => {
/**
* Variable to store the mappings data forwarded to the consumer component
*/
let data: any;
let onChangeHandler: jest.Mock = jest.fn();
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
let testBed: MappingsEditorTestBed;

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

beforeEach(() => {
onChangeHandler = jest.fn();
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
});

test('should require a scaling factor to be provided', async () => {
const defaultMappings = {
properties: {
myField: {
type: 'byte',
},
},
};

const updatedMappings = { ...defaultMappings };

await act(async () => {
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
});
testBed.component.update();

const {
component,
find,
exists,
form,
actions: { startEditField, updateFieldAndCloseFlyout },
} = testBed;

// Open the flyout to edit the field
await startEditField('myField');

// Change the type to "scaled_float"
await act(async () => {
find('mappingsEditorFieldEdit.fieldSubType').simulate('change', [
{
label: 'Scaled float',
value: 'scaled_float',
},
]);
});
component.update();

// It should **not** allow to save as the "scaling factor" parameter has not been set
await updateFieldAndCloseFlyout();
expect(exists('mappingsEditorFieldEdit')).toBe(true);
expect(form.getErrorsMessages()).toEqual(['A scaling factor is required.']);

await act(async () => {
form.setInputValue('scalingFactor.input', '123');
});
await updateFieldAndCloseFlyout();
expect(exists('mappingsEditorFieldEdit')).toBe(false);

// It should have the default parameters values added, plus the scaling factor
updatedMappings.properties.myField = {
...defaultScaledFloatParameters,
scaling_factor: 123,
} as any;

({ data } = await getMappingsEditorData(component));
expect(data).toEqual(updatedMappings);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,11 @@ export type TestSubjects =
| 'toggleExpandButton'
| 'createFieldForm'
| 'createFieldForm.fieldType'
| 'createFieldForm.fieldSubType'
| 'createFieldForm.addButton'
| 'mappingsEditorFieldEdit'
| 'mappingsEditorFieldEdit.fieldType'
| 'mappingsEditorFieldEdit.fieldSubType'
| 'mappingsEditorFieldEdit.editFieldUpdateButton'
| 'mappingsEditorFieldEdit.flyoutTitle'
| 'mappingsEditorFieldEdit.documentationLink'
Expand All @@ -383,4 +385,7 @@ export type TestSubjects =
| 'searchQuoteAnalyzer-custom.input'
| 'useSameAnalyzerForSearchCheckBox.input'
| 'metaParameterEditor'
| 'scalingFactor.input'
| 'formatParameter'
| 'formatParameter.formatInput'
| string;
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const FormatParameter = ({ defaultValue, defaultToggleValue }: Props) =>
href: documentationService.getFormatLink(),
}}
defaultToggleValue={defaultToggleValue}
data-test-subj="formatParameter"
>
<UseField path="format" config={getFieldConfig('format')}>
{(formatField) => {
Expand All @@ -81,6 +82,7 @@ export const FormatParameter = ({ defaultValue, defaultToggleValue }: Props) =>
setComboBoxOptions([...comboBoxOptions, newOption]);
}}
fullWidth
data-test-subj="formatInput"
/>
</EuiFormRow>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const SubTypeParameter = ({
selectedOptions={subTypeField.value as ComboBoxOption[]}
onChange={subTypeField.setValue}
isClearable={false}
data-test-subj="fieldSubType"
/>
</EuiFormRow>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const NumericType = ({ field }: Props) => {
path="scaling_factor"
config={getFieldConfig('scaling_factor')}
component={Field}
data-test-subj="scalingFactor"
/>
</EditFieldFormRow>
) : null;
Expand Down

0 comments on commit cd19bd3

Please sign in to comment.