Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[App Search] Schema: Set up server routes, grand foray into shared/connected Kea logic #99548

Merged
merged 11 commits into from
May 11, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { EngineDetails } from '../../../engine/types';

export const getConflictingEnginesFromConflictingField = (
conflictingField: SchemaConflictFieldTypes
): string[] => Object.values(conflictingField).flat();
) => Object.values(conflictingField).flat() as string[];

export const getConflictingEnginesFromSchemaConflicts = (
schemaConflicts: SchemaConflicts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,18 @@ import { i18n } from '@kbn/i18n';
export const SCHEMA_TITLE = i18n.translate('xpack.enterpriseSearch.appSearch.engine.schema.title', {
defaultMessage: 'Schema',
});

export const ADD_SCHEMA_ERROR = (fieldName: string) =>
i18n.translate('xpack.enterpriseSearch.appSearch.engine.schema.addSchemaErrorMessage', {
defaultMessage: 'Field name already exists: {fieldName}',
values: { fieldName },
});
export const ADD_SCHEMA_SUCCESS = (fieldName: string) =>
i18n.translate('xpack.enterpriseSearch.appSearch.engine.schema.addSchemaSuccessMessage', {
defaultMessage: 'New field added: {fieldName}',
values: { fieldName },
});
export const UPDATE_SCHEMA_SUCCESS = i18n.translate(
'xpack.enterpriseSearch.appSearch.engine.schema.updateSchemaSuccessMessage',
{ defaultMessage: 'Schema updated' }
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { LogicMounter, mockFlashMessageHelpers, mockHttpValues } from '../../../__mocks__';
import '../../__mocks__/engine_logic.mock';

import { nextTick } from '@kbn/test/jest';

import { SchemaType } from '../../../shared/schema/types';

import { SchemaBaseLogic } from './schema_base_logic';

describe('SchemaBaseLogic', () => {
const { mount } = new LogicMounter(SchemaBaseLogic);
const { http } = mockHttpValues;
const { flashAPIErrors } = mockFlashMessageHelpers;

const MOCK_SCHEMA = {
some_text_field: SchemaType.Text,
some_number_field: SchemaType.Number,
};

const DEFAULT_VALUES = {
dataLoading: true,
schema: {},
};

beforeEach(() => {
jest.clearAllMocks();
});

it('has expected default values', () => {
mount();
expect(SchemaBaseLogic.values).toEqual(DEFAULT_VALUES);
});

describe('actions', () => {
describe('setSchema', () => {
it('stores schema state and sets dataLoading to false', () => {
mount({ schema: {}, dataLoading: true });

SchemaBaseLogic.actions.setSchema(MOCK_SCHEMA);

expect(SchemaBaseLogic.values).toEqual({
...DEFAULT_VALUES,
schema: MOCK_SCHEMA,
dataLoading: false,
});
});
});
});

describe('listeners', () => {
const MOCK_RESPONSE = {
schema: MOCK_SCHEMA,
};
const mockCallback = jest.fn();

describe('loadSchema', () => {
it('should make an API call and then set schema state', async () => {
http.get.mockReturnValueOnce(Promise.resolve(MOCK_RESPONSE));
mount();
jest.spyOn(SchemaBaseLogic.actions, 'setSchema');

SchemaBaseLogic.actions.loadSchema(mockCallback);
await nextTick();

expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/schema');
expect(SchemaBaseLogic.actions.setSchema).toHaveBeenCalledWith(MOCK_SCHEMA);
});

it('should send the entire API response to the passed callback', async () => {
http.get.mockReturnValueOnce(Promise.resolve(MOCK_RESPONSE));
mount();

SchemaBaseLogic.actions.loadSchema(mockCallback);
await nextTick();

expect(mockCallback).toHaveBeenCalledWith(MOCK_RESPONSE);
});

it('handles errors', async () => {
http.get.mockReturnValueOnce(Promise.reject('error'));
mount();

SchemaBaseLogic.actions.loadSchema(mockCallback);
await nextTick();

expect(flashAPIErrors).toHaveBeenCalledWith('error');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { kea, MakeLogicType } from 'kea';

import { flashAPIErrors } from '../../../shared/flash_messages';
import { HttpLogic } from '../../../shared/http';
import { Schema } from '../../../shared/schema/types';
import { EngineLogic } from '../engine';

export interface SchemaBaseValues {
dataLoading: boolean;
schema: Schema;
}

export interface SchemaBaseActions {
loadSchema(callback: Function): Function;
setSchema(schema: Schema): { schema: Schema };
}

export const SchemaBaseLogic = kea<MakeLogicType<SchemaBaseValues, SchemaBaseActions>>({
path: ['enterprise_search', 'app_search', 'schema_base_logic'],
actions: {
loadSchema: (callback) => callback,
setSchema: (schema) => ({ schema }),
},
reducers: {
dataLoading: [
true,
{
loadSchema: () => true,
setSchema: () => false,
},
],
schema: [
{},
{
setSchema: (_, { schema }) => schema,
},
],
},
listeners: ({ actions }) => ({
loadSchema: async (callback) => {
const { http } = HttpLogic.values;
const { engineName } = EngineLogic.values;

try {
const response = await http.get(`/api/app_search/engines/${engineName}/schema`);
actions.setSchema(response.schema);
callback(response);
} catch (e) {
flashAPIErrors(e);
}
},
}),
});
Loading