Skip to content

Commit cd19bd3

Browse files
authored
[Mappings editor] Add scaled_float and date_range comp integration tests (#81287)
1 parent 3df30e0 commit cd19bd3

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { act } from 'react-dom/test-utils';
7+
8+
import { componentHelpers, MappingsEditorTestBed } from '../helpers';
9+
10+
const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;
11+
12+
// Parameters automatically added to the date range datatype when saved (with the default values)
13+
export const defaultDateRangeParameters = {
14+
type: 'date_range',
15+
coerce: true,
16+
index: true,
17+
store: false,
18+
};
19+
20+
describe('Mappings editor: date range datatype', () => {
21+
/**
22+
* Variable to store the mappings data forwarded to the consumer component
23+
*/
24+
let data: any;
25+
let onChangeHandler: jest.Mock = jest.fn();
26+
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
27+
let testBed: MappingsEditorTestBed;
28+
29+
beforeAll(() => {
30+
jest.useFakeTimers();
31+
});
32+
33+
afterAll(() => {
34+
jest.useRealTimers();
35+
});
36+
37+
beforeEach(() => {
38+
onChangeHandler = jest.fn();
39+
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
40+
});
41+
42+
test('should require a scaling factor to be provided', async () => {
43+
const defaultMappings = {
44+
properties: {
45+
myField: {
46+
type: 'double_range',
47+
},
48+
},
49+
};
50+
51+
const updatedMappings = { ...defaultMappings };
52+
53+
await act(async () => {
54+
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
55+
});
56+
testBed.component.update();
57+
58+
const {
59+
component,
60+
find,
61+
exists,
62+
actions: { startEditField, updateFieldAndCloseFlyout, toggleFormRow },
63+
} = testBed;
64+
65+
// Open the flyout to edit the field
66+
await startEditField('myField');
67+
68+
expect(exists('formatParameter')).toBe(false);
69+
70+
// Change the type to "date_range"
71+
await act(async () => {
72+
find('mappingsEditorFieldEdit.fieldSubType').simulate('change', [
73+
{
74+
label: 'Date range',
75+
value: 'date_range',
76+
},
77+
]);
78+
});
79+
component.update();
80+
81+
expect(exists('formatParameter')).toBe(true);
82+
expect(exists('formatParameter.formatInput')).toBe(false);
83+
toggleFormRow('formatParameter');
84+
expect(exists('formatParameter.formatInput')).toBe(true);
85+
86+
await act(async () => {
87+
find('formatParameter.formatInput').simulate('change', [{ label: 'customDateFormat' }]);
88+
});
89+
component.update();
90+
91+
await updateFieldAndCloseFlyout();
92+
93+
// It should have the default parameters values added, plus the scaling factor
94+
updatedMappings.properties.myField = {
95+
...defaultDateRangeParameters,
96+
format: 'customDateFormat',
97+
} as any;
98+
99+
({ data } = await getMappingsEditorData(component));
100+
expect(data).toEqual(updatedMappings);
101+
});
102+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { act } from 'react-dom/test-utils';
7+
8+
import { componentHelpers, MappingsEditorTestBed } from '../helpers';
9+
10+
const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;
11+
12+
// Parameters automatically added to the scaled float datatype when saved (with the default values)
13+
export const defaultScaledFloatParameters = {
14+
type: 'scaled_float',
15+
coerce: true,
16+
doc_values: true,
17+
ignore_malformed: false,
18+
index: true,
19+
store: false,
20+
};
21+
22+
describe('Mappings editor: scaled float datatype', () => {
23+
/**
24+
* Variable to store the mappings data forwarded to the consumer component
25+
*/
26+
let data: any;
27+
let onChangeHandler: jest.Mock = jest.fn();
28+
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
29+
let testBed: MappingsEditorTestBed;
30+
31+
beforeAll(() => {
32+
jest.useFakeTimers();
33+
});
34+
35+
afterAll(() => {
36+
jest.useRealTimers();
37+
});
38+
39+
beforeEach(() => {
40+
onChangeHandler = jest.fn();
41+
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
42+
});
43+
44+
test('should require a scaling factor to be provided', async () => {
45+
const defaultMappings = {
46+
properties: {
47+
myField: {
48+
type: 'byte',
49+
},
50+
},
51+
};
52+
53+
const updatedMappings = { ...defaultMappings };
54+
55+
await act(async () => {
56+
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
57+
});
58+
testBed.component.update();
59+
60+
const {
61+
component,
62+
find,
63+
exists,
64+
form,
65+
actions: { startEditField, updateFieldAndCloseFlyout },
66+
} = testBed;
67+
68+
// Open the flyout to edit the field
69+
await startEditField('myField');
70+
71+
// Change the type to "scaled_float"
72+
await act(async () => {
73+
find('mappingsEditorFieldEdit.fieldSubType').simulate('change', [
74+
{
75+
label: 'Scaled float',
76+
value: 'scaled_float',
77+
},
78+
]);
79+
});
80+
component.update();
81+
82+
// It should **not** allow to save as the "scaling factor" parameter has not been set
83+
await updateFieldAndCloseFlyout();
84+
expect(exists('mappingsEditorFieldEdit')).toBe(true);
85+
expect(form.getErrorsMessages()).toEqual(['A scaling factor is required.']);
86+
87+
await act(async () => {
88+
form.setInputValue('scalingFactor.input', '123');
89+
});
90+
await updateFieldAndCloseFlyout();
91+
expect(exists('mappingsEditorFieldEdit')).toBe(false);
92+
93+
// It should have the default parameters values added, plus the scaling factor
94+
updatedMappings.properties.myField = {
95+
...defaultScaledFloatParameters,
96+
scaling_factor: 123,
97+
} as any;
98+
99+
({ data } = await getMappingsEditorData(component));
100+
expect(data).toEqual(updatedMappings);
101+
});
102+
});

x-pack/plugins/index_management/public/application/components/mappings_editor/__jest__/client_integration/helpers/mappings_editor.helpers.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,11 @@ export type TestSubjects =
358358
| 'toggleExpandButton'
359359
| 'createFieldForm'
360360
| 'createFieldForm.fieldType'
361+
| 'createFieldForm.fieldSubType'
361362
| 'createFieldForm.addButton'
362363
| 'mappingsEditorFieldEdit'
363364
| 'mappingsEditorFieldEdit.fieldType'
365+
| 'mappingsEditorFieldEdit.fieldSubType'
364366
| 'mappingsEditorFieldEdit.editFieldUpdateButton'
365367
| 'mappingsEditorFieldEdit.flyoutTitle'
366368
| 'mappingsEditorFieldEdit.documentationLink'
@@ -383,4 +385,7 @@ export type TestSubjects =
383385
| 'searchQuoteAnalyzer-custom.input'
384386
| 'useSameAnalyzerForSearchCheckBox.input'
385387
| 'metaParameterEditor'
388+
| 'scalingFactor.input'
389+
| 'formatParameter'
390+
| 'formatParameter.formatInput'
386391
| string;

x-pack/plugins/index_management/public/application/components/mappings_editor/components/document_fields/field_parameters/format_parameter.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export const FormatParameter = ({ defaultValue, defaultToggleValue }: Props) =>
5555
href: documentationService.getFormatLink(),
5656
}}
5757
defaultToggleValue={defaultToggleValue}
58+
data-test-subj="formatParameter"
5859
>
5960
<UseField path="format" config={getFieldConfig('format')}>
6061
{(formatField) => {
@@ -81,6 +82,7 @@ export const FormatParameter = ({ defaultValue, defaultToggleValue }: Props) =>
8182
setComboBoxOptions([...comboBoxOptions, newOption]);
8283
}}
8384
fullWidth
85+
data-test-subj="formatInput"
8486
/>
8587
</EuiFormRow>
8688
);

x-pack/plugins/index_management/public/application/components/mappings_editor/components/document_fields/field_parameters/subtype_parameter.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export const SubTypeParameter = ({
8585
selectedOptions={subTypeField.value as ComboBoxOption[]}
8686
onChange={subTypeField.setValue}
8787
isClearable={false}
88+
data-test-subj="fieldSubType"
8889
/>
8990
</EuiFormRow>
9091
);

x-pack/plugins/index_management/public/application/components/mappings_editor/components/document_fields/fields/field_types/numeric_type.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const NumericType = ({ field }: Props) => {
6060
path="scaling_factor"
6161
config={getFieldConfig('scaling_factor')}
6262
component={Field}
63+
data-test-subj="scalingFactor"
6364
/>
6465
</EditFieldFormRow>
6566
) : null;

0 commit comments

Comments
 (0)