-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[App Search] Schema: Set up server routes, grand foray into shared/co…
…nnected Kea logic (#99548) (#99833) * Set up server API routes * Set up types * Set up shared/base Schema logic file - values & actions shared between both default source engine & meta engine pages * Add default/indexed engine SchemaLogic * Add MetaEnginesSchemaLogic - significantly different actions (no updating) & API response from source engines, hence the separate files + fix typing issue - without Partial<>, all 4 enum types are expected instead of 1-4 - for some reason this causes an error in a separate a util file, not sure why (Typescript issue?) * Update Schema & MetaEngineSchema views with loaders * PR feedback: comment nit * PR feedback: Remove unnecessary async/awaits * PR feedback: Simplify loadSchema to be shared by base logic Much clean, such simple Co-authored-by: Kibana Machine <[email protected]> Co-authored-by: Constance <[email protected]>
- Loading branch information
1 parent
768f6f7
commit 22dc0c5
Showing
17 changed files
with
1,043 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...erprise_search/public/applications/app_search/components/schema/schema_base_logic.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 MOCK_RESPONSE = { | ||
schema: MOCK_SCHEMA, | ||
} as any; | ||
|
||
const DEFAULT_VALUES = { | ||
dataLoading: true, | ||
schema: {}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('has expected default values', () => { | ||
mount(); | ||
expect(SchemaBaseLogic.values).toEqual(DEFAULT_VALUES); | ||
}); | ||
|
||
describe('actions', () => { | ||
describe('onSchemaLoad', () => { | ||
it('stores schema state and sets dataLoading to false', () => { | ||
mount({ schema: {}, dataLoading: true }); | ||
|
||
SchemaBaseLogic.actions.onSchemaLoad(MOCK_RESPONSE); | ||
|
||
expect(SchemaBaseLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
dataLoading: false, | ||
schema: MOCK_SCHEMA, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('setSchema', () => { | ||
it('updates schema state', () => { | ||
mount({ schema: {} }); | ||
|
||
SchemaBaseLogic.actions.setSchema(MOCK_SCHEMA); | ||
|
||
expect(SchemaBaseLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
schema: MOCK_SCHEMA, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listeners', () => { | ||
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, 'onSchemaLoad'); | ||
|
||
SchemaBaseLogic.actions.loadSchema(); | ||
await nextTick(); | ||
|
||
expect(http.get).toHaveBeenCalledWith('/api/app_search/engines/some-engine/schema'); | ||
expect(SchemaBaseLogic.actions.onSchemaLoad).toHaveBeenCalledWith(MOCK_RESPONSE); | ||
}); | ||
|
||
it('handles errors', async () => { | ||
http.get.mockReturnValueOnce(Promise.reject('error')); | ||
mount(); | ||
|
||
SchemaBaseLogic.actions.loadSchema(); | ||
await nextTick(); | ||
|
||
expect(flashAPIErrors).toHaveBeenCalledWith('error'); | ||
}); | ||
}); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
...s/enterprise_search/public/applications/app_search/components/schema/schema_base_logic.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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'; | ||
|
||
import { SchemaApiResponse, MetaEngineSchemaApiResponse } from './types'; | ||
|
||
export interface SchemaBaseValues { | ||
dataLoading: boolean; | ||
schema: Schema; | ||
} | ||
|
||
export interface SchemaBaseActions { | ||
loadSchema(): void; | ||
onSchemaLoad( | ||
response: SchemaApiResponse | MetaEngineSchemaApiResponse | ||
): SchemaApiResponse | MetaEngineSchemaApiResponse; | ||
setSchema(schema: Schema): { schema: Schema }; | ||
} | ||
|
||
export const SchemaBaseLogic = kea<MakeLogicType<SchemaBaseValues, SchemaBaseActions>>({ | ||
path: ['enterprise_search', 'app_search', 'schema_base_logic'], | ||
actions: { | ||
loadSchema: true, | ||
onSchemaLoad: (response) => response, | ||
setSchema: (schema) => ({ schema }), | ||
}, | ||
reducers: { | ||
dataLoading: [ | ||
true, | ||
{ | ||
loadSchema: () => true, | ||
onSchemaLoad: () => false, | ||
}, | ||
], | ||
schema: [ | ||
{}, | ||
{ | ||
onSchemaLoad: (_, { schema }) => schema, | ||
setSchema: (_, { schema }) => schema, | ||
}, | ||
], | ||
}, | ||
listeners: ({ actions }) => ({ | ||
loadSchema: async () => { | ||
const { http } = HttpLogic.values; | ||
const { engineName } = EngineLogic.values; | ||
|
||
try { | ||
const response = await http.get(`/api/app_search/engines/${engineName}/schema`); | ||
actions.onSchemaLoad(response); | ||
} catch (e) { | ||
flashAPIErrors(e); | ||
} | ||
}, | ||
}), | ||
}); |
Oops, something went wrong.