Skip to content

Commit

Permalink
Unskip mappings editor core tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Sep 10, 2020
1 parent 825688a commit 7593e61
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ describe('<TemplateClone />', () => {
name: `${templateToClone.name}-copy`,
indexPatterns: DEFAULT_INDEX_PATTERNS,
};
// @ts-expect-error
delete expected.template; // As no settings, mappings or aliases have been defined, no "template" param is sent

expect(JSON.parse(JSON.parse(latestRequest.requestBody).body)).toEqual(expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,22 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
return { field: find(testSubject as TestSubjects), testSubject };
};

const addField = (name: string, type: string) => {
form.setInputValue('nameParameterInput', name);
find('createFieldForm.fieldType').simulate('change', [
{
label: type,
value: type,
},
]);
find('createFieldForm.addButton').simulate('click');
const addField = async (name: string, type: string) => {
await act(async () => {
form.setInputValue('nameParameterInput', name);
find('createFieldForm.fieldType').simulate('change', [
{
label: type,
value: type,
},
]);
});

await act(async () => {
find('createFieldForm.addButton').simulate('click');
});

component.update();
};

const startEditField = (path: string) => {
Expand Down Expand Up @@ -194,14 +201,18 @@ const createActions = (testBed: TestBed<TestSubjects>) => {
component.update();
};

const selectTab = (tab: 'fields' | 'templates' | 'advanced') => {
const selectTab = async (tab: 'fields' | 'templates' | 'advanced') => {
const index = ['fields', 'templates', 'advanced'].indexOf(tab);

const tabElement = find('formTab').at(index);
if (tabElement.length === 0) {
throw new Error(`Tab not found: "${tab}"`);
}
tabElement.simulate('click');

await act(async () => {
tabElement.simulate('click');
});
component.update();
};

const updateJsonEditor = (testSubject: TestSubjects, value: object) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import { act } from 'react-dom/test-utils';
import { componentHelpers, MappingsEditorTestBed } from './helpers';

const { setup, getMappingsEditorDataFactory } = componentHelpers.mappingsEditor;
const onChangeHandler = jest.fn();
const getMappingsEditorData = getMappingsEditorDataFactory(onChangeHandler);

// FLAKY: https://github.com/elastic/kibana/issues/66457
describe.skip('Mappings editor: core', () => {
describe('Mappings editor: core', () => {
/**
* 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();
Expand All @@ -26,8 +26,9 @@ describe.skip('Mappings editor: core', () => {
jest.useRealTimers();
});

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

test('default behaviour', async () => {
Expand All @@ -42,11 +43,14 @@ describe.skip('Mappings editor: core', () => {
},
};

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

const { component } = testBed;
component.update();

const expectedMappings = {
_meta: {}, // Was not defined so an empty object is returned
_source: {}, // Was not defined so an empty object is returned
...defaultMappings,
properties: {
user: {
Expand Down Expand Up @@ -78,8 +82,13 @@ describe.skip('Mappings editor: core', () => {
},
},
};
const testBed = setup({ onChange: onChangeHandler, value });
const { exists } = testBed;

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

const { component, exists } = testBed;
component.update();

expect(exists('mappingsEditor')).toBe(true);
expect(exists('mappingTypesDetectedCallout')).toBe(true);
Expand All @@ -94,8 +103,12 @@ describe.skip('Mappings editor: core', () => {
},
},
};
const testBed = setup({ onChange: onChangeHandler, value });
const { exists } = testBed;
await act(async () => {
testBed = setup({ onChange: onChangeHandler, value });
});

const { component, exists } = testBed;
component.update();

expect(exists('mappingsEditor')).toBe(true);
expect(exists('mappingTypesDetectedCallout')).toBe(false);
Expand All @@ -108,10 +121,12 @@ describe.skip('Mappings editor: core', () => {
properties: {},
dynamic_templates: [{ before: 'foo' }],
};
let testBed: MappingsEditorTestBed;

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

test('should keep the changes when switching tabs', async () => {
Expand All @@ -129,10 +144,7 @@ describe.skip('Mappings editor: core', () => {
expect(find('fieldsListItem').length).toEqual(0); // Check that we start with an empty list

const newField = { name: 'John', type: 'text' };
await act(async () => {
addField(newField.name, newField.type);
});
component.update();
await addField(newField.name, newField.type);

expect(find('fieldsListItem').length).toEqual(1);

Expand All @@ -142,10 +154,7 @@ describe.skip('Mappings editor: core', () => {
// -------------------------------------
// Navigate to dynamic templates tab
// -------------------------------------
await act(async () => {
selectTab('templates');
});
component.update();
await selectTab('templates');

let templatesValue = getJsonEditorValue('dynamicTemplatesEditor');
expect(templatesValue).toEqual(defaultMappings.dynamic_templates);
Expand All @@ -163,10 +172,7 @@ describe.skip('Mappings editor: core', () => {
// ------------------------------------------------------
// Switch to advanced settings tab and make some changes
// ------------------------------------------------------
await act(async () => {
selectTab('advanced');
});
component.update();
await selectTab('advanced');

let isDynamicMappingsEnabled = getToggleValue(
'advancedConfiguration.dynamicMappingsToggle.input'
Expand Down Expand Up @@ -194,29 +200,21 @@ describe.skip('Mappings editor: core', () => {
// ----------------------------------------------------------------------------
// Go back to dynamic templates tab and make sure our changes are still there
// ----------------------------------------------------------------------------
await act(async () => {
selectTab('templates');
});
component.update();
await selectTab('templates');

templatesValue = getJsonEditorValue('dynamicTemplatesEditor');
expect(templatesValue).toEqual(updatedValueTemplates);

// -----------------------------------------------------------
// Go back to fields and make sure our created field is there
// -----------------------------------------------------------
await act(async () => {
selectTab('fields');
});
component.update();
await selectTab('fields');

field = find('fieldsListItem').at(0);
expect(find('fieldName', field).text()).toEqual(newField.name);

// Go back to advanced settings tab make sure dynamic mappings is disabled
await act(async () => {
selectTab('advanced');
});
component.update();
await selectTab('advanced');

isDynamicMappingsEnabled = getToggleValue(
'advancedConfiguration.dynamicMappingsToggle.input'
Expand All @@ -231,46 +229,47 @@ describe.skip('Mappings editor: core', () => {
/**
* Note: the "indexSettings" prop will be tested along with the "analyzer" parameter on a text datatype field,
* as it is the only place where it is consumed by the mappings editor.
*
* The test that covers it is text_datatype.test.tsx: "analyzer parameter: custom analyzer (from index settings)"
* The test that covers it is in the "text_datatype.test.tsx": "analyzer parameter: custom analyzer (from index settings)"
*/
const defaultMappings: any = {
dynamic: true,
numeric_detection: false,
date_detection: true,
properties: {
title: { type: 'text' },
address: {
type: 'object',
properties: {
street: { type: 'text' },
city: { type: 'text' },
let defaultMappings: any;

beforeEach(async () => {
defaultMappings = {
dynamic: true,
numeric_detection: false,
date_detection: true,
properties: {
title: { type: 'text' },
address: {
type: 'object',
properties: {
street: { type: 'text' },
city: { type: 'text' },
},
},
},
},
dynamic_templates: [{ initial: 'value' }],
_source: {
enabled: true,
includes: ['field1', 'field2'],
excludes: ['field3'],
},
_meta: {
some: 'metaData',
},
_routing: {
required: false,
},
};

let testBed: MappingsEditorTestBed;
dynamic_templates: [{ initial: 'value' }],
_source: {
enabled: true,
includes: ['field1', 'field2'],
excludes: ['field3'],
},
_meta: {
some: 'metaData',
},
_routing: {
required: false,
},
};

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

test('props.value => should prepopulate the editor data', async () => {
const {
component,
actions: { selectTab, getJsonEditorValue, getComboBoxValue, getToggleValue },
find,
} = testBed;
Expand All @@ -285,10 +284,7 @@ describe.skip('Mappings editor: core', () => {
/**
* Dynamic templates
*/
await act(async () => {
selectTab('templates');
});
component.update();
await selectTab('templates');

// Test that dynamic templates JSON is rendered in the templates editor
const templatesValue = getJsonEditorValue('dynamicTemplatesEditor');
Expand All @@ -297,10 +293,7 @@ describe.skip('Mappings editor: core', () => {
/**
* Advanced settings
*/
await act(async () => {
selectTab('advanced');
});
component.update();
await selectTab('advanced');

const isDynamicMappingsEnabled = getToggleValue(
'advancedConfiguration.dynamicMappingsToggle.input'
Expand Down Expand Up @@ -339,7 +332,14 @@ describe.skip('Mappings editor: core', () => {
/**
* Mapped fields
*/
await act(async () => {
find('addFieldButton').simulate('click');
});
component.update();

const newField = { name: 'someNewField', type: 'text' };
await addField(newField.name, newField.type);

updatedMappings = {
...updatedMappings,
properties: {
Expand All @@ -348,26 +348,14 @@ describe.skip('Mappings editor: core', () => {
},
};

await act(async () => {
find('addFieldButton').simulate('click');
});
component.update();

await act(async () => {
addField(newField.name, newField.type);
});
component.update();

({ data } = await getMappingsEditorData(component));

expect(data).toEqual(updatedMappings);

/**
* Dynamic templates
*/
await act(async () => {
await selectTab('templates');
});
component.update();
await selectTab('templates');

const updatedTemplatesValue = [{ someTemplateProp: 'updated' }];
updatedMappings = {
Expand All @@ -385,10 +373,7 @@ describe.skip('Mappings editor: core', () => {
/**
* Advanced settings
*/
await act(async () => {
selectTab('advanced');
});
component.update();
await selectTab('advanced');

// Disbable dynamic mappings
await act(async () => {
Expand Down
Loading

0 comments on commit 7593e61

Please sign in to comment.