[Workplace Search] Port bugfix to handle duplicate schema (#91055)

Ports https://github.com/elastic/ent-search/pull/3040

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Scotty Bollinger 2021-02-11 12:33:22 -06:00 committed by GitHub
parent f85be6b36b
commit b4368bde14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 12 deletions

View file

@ -307,16 +307,25 @@ describe('SchemaLogic', () => {
});
});
it('addNewField', () => {
const setServerFieldSpy = jest.spyOn(SchemaLogic.actions, 'setServerField');
SchemaLogic.actions.onInitializeSchema(serverResponse);
const newSchema = {
...schema,
bar: 'number',
};
SchemaLogic.actions.addNewField('bar', 'number');
describe('addNewField', () => {
it('handles happy path', () => {
const setServerFieldSpy = jest.spyOn(SchemaLogic.actions, 'setServerField');
SchemaLogic.actions.onInitializeSchema(serverResponse);
const newSchema = {
...schema,
bar: 'number',
};
SchemaLogic.actions.addNewField('bar', 'number');
expect(setServerFieldSpy).toHaveBeenCalledWith(newSchema, ADD);
expect(setServerFieldSpy).toHaveBeenCalledWith(newSchema, ADD);
});
it('handles duplicate', () => {
SchemaLogic.actions.onInitializeSchema(serverResponse);
SchemaLogic.actions.addNewField('foo', 'number');
expect(setErrorMessage).toHaveBeenCalledWith('New field already exists: foo.');
});
});
it('updateExistingFieldType', () => {

View file

@ -8,6 +8,8 @@
import { kea, MakeLogicType } from 'kea';
import { cloneDeep, isEqual } from 'lodash';
import { i18n } from '@kbn/i18n';
import { TEXT } from '../../../../../shared/constants/field_types';
import { ADD, UPDATE } from '../../../../../shared/constants/operations';
import {
@ -300,9 +302,22 @@ export const SchemaLogic = kea<MakeLogicType<SchemaValues, SchemaActions>>({
}
},
addNewField: ({ fieldName, newFieldType }) => {
const schema = cloneDeep(values.activeSchema);
schema[fieldName] = newFieldType;
actions.setServerField(schema, ADD);
if (fieldName in values.activeSchema) {
window.scrollTo(0, 0);
setErrorMessage(
i18n.translate(
'xpack.enterpriseSearch.workplaceSearch.contentSource.schema.newFieldExists.message',
{
defaultMessage: 'New field already exists: {fieldName}.',
values: { fieldName },
}
)
);
} else {
const schema = cloneDeep(values.activeSchema);
schema[fieldName] = newFieldType;
actions.setServerField(schema, ADD);
}
},
updateExistingFieldType: ({ fieldName, newFieldType }) => {
const schema = cloneDeep(values.activeSchema);