forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
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 reindex job errors view (elastic#99941)
* Set up ReindexJobLogic * Update ReindexJob view with load behavior & shared SchemaErrorsAccordion
- Loading branch information
Constance
authored
May 12, 2021
1 parent
537be25
commit ea75823
Showing
4 changed files
with
261 additions
and
3 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
120 changes: 120 additions & 0 deletions
120
...ch/public/applications/app_search/components/schema/reindex_job/reindex_job_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,120 @@ | ||
/* | ||
* 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 { ReindexJobLogic } from './reindex_job_logic'; | ||
|
||
describe('ReindexJobLogic', () => { | ||
const { mount } = new LogicMounter(ReindexJobLogic); | ||
const { http } = mockHttpValues; | ||
const { flashAPIErrors } = mockFlashMessageHelpers; | ||
|
||
const MOCK_RESPONSE = { | ||
fieldCoercionErrors: { | ||
some_erroring_field: [ | ||
{ | ||
external_id: 'document-1', | ||
error: "Value 'some text' cannot be parsed as a number", | ||
}, | ||
], | ||
another_erroring_field: [ | ||
{ | ||
external_id: 'document-2', | ||
error: "Value '123' cannot be parsed as a date", | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
const DEFAULT_VALUES = { | ||
dataLoading: true, | ||
fieldCoercionErrors: {}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('has expected default values', () => { | ||
mount(); | ||
expect(ReindexJobLogic.values).toEqual(DEFAULT_VALUES); | ||
}); | ||
|
||
describe('actions', () => { | ||
describe('onLoadSuccess', () => { | ||
it('stores fieldCoercionErrors state and sets dataLoading to false', () => { | ||
mount({ fieldCoercionErrors: {}, dataLoading: true }); | ||
|
||
ReindexJobLogic.actions.onLoadSuccess(MOCK_RESPONSE); | ||
|
||
expect(ReindexJobLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
dataLoading: false, | ||
fieldCoercionErrors: MOCK_RESPONSE.fieldCoercionErrors, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('onLoadError', () => { | ||
it('sets dataLoading to false', () => { | ||
mount({ dataLoading: true }); | ||
|
||
ReindexJobLogic.actions.onLoadError(); | ||
|
||
expect(ReindexJobLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
dataLoading: false, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listeners', () => { | ||
describe('loadReindexJob', () => { | ||
it('sets dataLoading to true', () => { | ||
mount({ dataLoading: false }); | ||
|
||
ReindexJobLogic.actions.loadReindexJob('some-job-id'); | ||
|
||
expect(ReindexJobLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
dataLoading: true, | ||
}); | ||
}); | ||
|
||
it('should make an API call and then set schema state', async () => { | ||
http.get.mockReturnValueOnce(Promise.resolve(MOCK_RESPONSE)); | ||
mount(); | ||
jest.spyOn(ReindexJobLogic.actions, 'onLoadSuccess'); | ||
|
||
ReindexJobLogic.actions.loadReindexJob('some-job-id'); | ||
await nextTick(); | ||
|
||
expect(http.get).toHaveBeenCalledWith( | ||
'/api/app_search/engines/some-engine/reindex_job/some-job-id' | ||
); | ||
expect(ReindexJobLogic.actions.onLoadSuccess).toHaveBeenCalledWith(MOCK_RESPONSE); | ||
}); | ||
|
||
it('handles errors', async () => { | ||
http.get.mockReturnValueOnce(Promise.reject('error')); | ||
mount(); | ||
jest.spyOn(ReindexJobLogic.actions, 'onLoadError'); | ||
|
||
ReindexJobLogic.actions.loadReindexJob('some-bad-id'); | ||
await nextTick(); | ||
|
||
expect(flashAPIErrors).toHaveBeenCalledWith('error'); | ||
expect(ReindexJobLogic.actions.onLoadError).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
64 changes: 64 additions & 0 deletions
64
..._search/public/applications/app_search/components/schema/reindex_job/reindex_job_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,64 @@ | ||
/* | ||
* 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 { EngineLogic } from '../../engine'; | ||
|
||
import { ReindexJobApiResponse } from '../types'; | ||
|
||
export interface ReindexJobValues { | ||
dataLoading: boolean; | ||
fieldCoercionErrors: ReindexJobApiResponse['fieldCoercionErrors']; | ||
} | ||
|
||
export interface ReindexJobActions { | ||
loadReindexJob(id: string): string; | ||
onLoadSuccess(response: ReindexJobApiResponse): ReindexJobApiResponse; | ||
onLoadError(): void; | ||
} | ||
|
||
export const ReindexJobLogic = kea<MakeLogicType<ReindexJobValues, ReindexJobActions>>({ | ||
path: ['enterprise_search', 'app_search', 'reindex_job_logic'], | ||
actions: { | ||
loadReindexJob: (id) => id, | ||
onLoadSuccess: (response) => response, | ||
onLoadError: true, | ||
}, | ||
reducers: { | ||
dataLoading: [ | ||
true, | ||
{ | ||
loadReindexJob: () => true, | ||
onLoadSuccess: () => false, | ||
onLoadError: () => false, | ||
}, | ||
], | ||
fieldCoercionErrors: [ | ||
{}, | ||
{ | ||
onLoadSuccess: (_, { fieldCoercionErrors }) => fieldCoercionErrors, | ||
}, | ||
], | ||
}, | ||
listeners: ({ actions }) => ({ | ||
loadReindexJob: async (id) => { | ||
const { http } = HttpLogic.values; | ||
const { engineName } = EngineLogic.values; | ||
|
||
try { | ||
const response = await http.get(`/api/app_search/engines/${engineName}/reindex_job/${id}`); | ||
actions.onLoadSuccess(response); | ||
} catch (e) { | ||
flashAPIErrors(e); | ||
actions.onLoadError(); | ||
} | ||
}, | ||
}), | ||
}); |