Skip to content

Commit 5dd47fc

Browse files
add tests for point field type
1 parent 569eb8a commit 5dd47fc

File tree

3 files changed

+168
-1
lines changed

3 files changed

+168
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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 point datatype when saved (with the default values)
13+
export const defaultPointParameters = {
14+
type: 'point',
15+
ignore_malformed: false,
16+
ignore_z_value: true,
17+
};
18+
19+
describe('Mappings editor: point datatype', () => {
20+
/**
21+
* Variable to store the mappings data forwarded to the consumer component
22+
*/
23+
let data: any;
24+
let onChangeHandler: jest.Mock = jest.fn();
25+
let getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
26+
let testBed: MappingsEditorTestBed;
27+
28+
beforeAll(() => {
29+
jest.useFakeTimers();
30+
});
31+
32+
afterAll(() => {
33+
jest.useRealTimers();
34+
});
35+
36+
beforeEach(() => {
37+
onChangeHandler = jest.fn();
38+
getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);
39+
});
40+
41+
test('initial view and default parameters values', async () => {
42+
const defaultMappings = {
43+
properties: {
44+
myField: {
45+
type: 'point',
46+
},
47+
},
48+
};
49+
50+
const updatedMappings = { ...defaultMappings };
51+
52+
await act(async () => {
53+
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
54+
});
55+
testBed.component.update();
56+
57+
const {
58+
component,
59+
actions: { startEditField, updateFieldAndCloseFlyout },
60+
} = testBed;
61+
62+
// Open the flyout to edit the field
63+
await startEditField('myField');
64+
65+
// Save the field and close the flyout
66+
await updateFieldAndCloseFlyout();
67+
68+
// It should have the default parameters values added
69+
updatedMappings.properties.myField = defaultPointParameters;
70+
71+
({ data } = await getMappingsEditorData(component));
72+
expect(data).toEqual(updatedMappings);
73+
});
74+
75+
describe('meta parameter', () => {
76+
const defaultMappings = {
77+
properties: {
78+
myField: {
79+
type: 'point',
80+
},
81+
},
82+
};
83+
84+
const updatedMappings = { ...defaultMappings };
85+
86+
const metaParameter = {
87+
meta: {
88+
my_metadata: 'foobar',
89+
},
90+
};
91+
92+
beforeEach(async () => {
93+
await act(async () => {
94+
testBed = setup({ value: defaultMappings, onChange: onChangeHandler });
95+
});
96+
testBed.component.update();
97+
});
98+
99+
test('valid meta object', async () => {
100+
const {
101+
component,
102+
actions: {
103+
startEditField,
104+
updateFieldAndCloseFlyout,
105+
showAdvancedSettings,
106+
toggleFormRow,
107+
updateJsonEditor,
108+
},
109+
} = testBed;
110+
111+
// Open the flyout to edit the field
112+
await startEditField('myField');
113+
await showAdvancedSettings();
114+
115+
// Enable the meta parameter and add value
116+
toggleFormRow('metaParameter');
117+
await act(async () => {
118+
updateJsonEditor('metaParameterEditor', metaParameter.meta);
119+
});
120+
component.update();
121+
122+
// Save the field and close the flyout
123+
await updateFieldAndCloseFlyout();
124+
125+
// It should have the default parameters values added, plus metadata
126+
updatedMappings.properties.myField = {
127+
...defaultPointParameters,
128+
...metaParameter,
129+
};
130+
131+
({ data } = await getMappingsEditorData(component));
132+
expect(data).toEqual(updatedMappings);
133+
});
134+
135+
test('strip empty string', async () => {
136+
const {
137+
component,
138+
actions: { startEditField, updateFieldAndCloseFlyout, showAdvancedSettings, toggleFormRow },
139+
} = testBed;
140+
141+
// Open the flyout to edit the field
142+
await startEditField('myField');
143+
await showAdvancedSettings();
144+
145+
// Enable the meta parameter
146+
toggleFormRow('metaParameter');
147+
148+
// Save the field and close the flyout without adding any values to meta parameter
149+
await updateFieldAndCloseFlyout();
150+
151+
// It should have the default parameters values added
152+
updatedMappings.properties.myField = defaultPointParameters;
153+
154+
({ data } = await getMappingsEditorData(component));
155+
expect(data).toEqual(updatedMappings);
156+
});
157+
});
158+
});

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
239239
const getCheckboxValue = (testSubject: TestSubjects): boolean =>
240240
find(testSubject).props().checked;
241241

242+
const toggleFormRow = (formRowName: string) => {
243+
form.toggleEuiSwitch(`${formRowName}.formRowToggle`);
244+
};
245+
242246
return {
243247
selectTab,
244248
getFieldAt,
@@ -252,6 +256,7 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
252256
getComboBoxValue,
253257
getToggleValue,
254258
getCheckboxValue,
259+
toggleFormRow,
255260
};
256261
};
257262

@@ -365,4 +370,6 @@ export type TestSubjects =
365370
| 'searchQuoteAnalyzer-custom'
366371
| 'searchQuoteAnalyzer-toggleCustomButton'
367372
| 'searchQuoteAnalyzer-custom.input'
368-
| 'useSameAnalyzerForSearchCheckBox.input';
373+
| 'useSameAnalyzerForSearchCheckBox.input'
374+
| 'metaParameterEditor'
375+
| string;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ export const MetaParameter: FunctionComponent<Props> = ({ defaultToggleValue })
3232
}),
3333
href: documentationService.getMetaLink(),
3434
}}
35+
data-test-subj="metaParameter"
3536
>
3637
<UseField
3738
path="meta"
3839
config={getFieldConfig('meta')}
3940
component={JsonEditorField}
4041
componentProps={{
4142
euiCodeEditorProps: {
43+
['data-test-subj']: 'metaParameterEditor',
4244
height: '300px',
4345
'aria-label': i18n.translate('xpack.idxMgmt.mappingsEditor.metaParameterAriaLabel', {
4446
defaultMessage: 'metadata field data editor',

0 commit comments

Comments
 (0)