-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: Jason Stoltzfus <[email protected]>
- Loading branch information
1 parent
5f774b5
commit 56c3813
Showing
8 changed files
with
384 additions
and
32 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...public/applications/app_search/components/curations/components/suggestions_logic.test.tsx
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,152 @@ | ||
/* | ||
* 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__/kea_logic'; | ||
import '../../../__mocks__/engine_logic.mock'; | ||
|
||
import { nextTick } from '@kbn/test/jest'; | ||
|
||
import { DEFAULT_META } from '../../../../shared/constants'; | ||
|
||
import { SuggestionsLogic } from './suggestions_logic'; | ||
|
||
const DEFAULT_VALUES = { | ||
dataLoading: true, | ||
suggestions: [], | ||
meta: { | ||
...DEFAULT_META, | ||
page: { | ||
...DEFAULT_META.page, | ||
size: 10, | ||
}, | ||
}, | ||
}; | ||
|
||
const MOCK_RESPONSE = { | ||
meta: { | ||
page: { | ||
current: 1, | ||
size: 10, | ||
total_results: 1, | ||
total_pages: 1, | ||
}, | ||
}, | ||
results: [ | ||
{ | ||
query: 'foo', | ||
updated_at: '2021-07-08T14:35:50Z', | ||
promoted: ['1', '2'], | ||
}, | ||
], | ||
}; | ||
|
||
describe('SuggestionsLogic', () => { | ||
const { mount } = new LogicMounter(SuggestionsLogic); | ||
const { flashAPIErrors } = mockFlashMessageHelpers; | ||
const { http } = mockHttpValues; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('has expected default values', () => { | ||
mount(); | ||
expect(SuggestionsLogic.values).toEqual(DEFAULT_VALUES); | ||
}); | ||
|
||
describe('actions', () => { | ||
describe('onSuggestionsLoaded', () => { | ||
it('should set suggestion, meta state, & dataLoading to false', () => { | ||
mount(); | ||
|
||
SuggestionsLogic.actions.onSuggestionsLoaded(MOCK_RESPONSE); | ||
|
||
expect(SuggestionsLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
suggestions: MOCK_RESPONSE.results, | ||
meta: MOCK_RESPONSE.meta, | ||
dataLoading: false, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('onPaginate', () => { | ||
it('should update meta', () => { | ||
mount(); | ||
|
||
SuggestionsLogic.actions.onPaginate(2); | ||
|
||
expect(SuggestionsLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
meta: { | ||
...DEFAULT_META, | ||
page: { | ||
...DEFAULT_META.page, | ||
current: 2, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listeners', () => { | ||
describe('loadSuggestions', () => { | ||
it('should set dataLoading state', () => { | ||
mount({ dataLoading: false }); | ||
|
||
SuggestionsLogic.actions.loadSuggestions(); | ||
|
||
expect(SuggestionsLogic.values).toEqual({ | ||
...DEFAULT_VALUES, | ||
dataLoading: true, | ||
}); | ||
}); | ||
|
||
it('should make an API call and set suggestions & meta state', async () => { | ||
http.post.mockReturnValueOnce(Promise.resolve(MOCK_RESPONSE)); | ||
mount(); | ||
jest.spyOn(SuggestionsLogic.actions, 'onSuggestionsLoaded'); | ||
|
||
SuggestionsLogic.actions.loadSuggestions(); | ||
await nextTick(); | ||
|
||
expect(http.post).toHaveBeenCalledWith( | ||
'/internal/app_search/engines/some-engine/search_relevance_suggestions', | ||
{ | ||
body: JSON.stringify({ | ||
page: { | ||
current: 1, | ||
size: 10, | ||
}, | ||
filters: { | ||
status: ['pending'], | ||
type: 'curation', | ||
}, | ||
}), | ||
} | ||
); | ||
|
||
expect(SuggestionsLogic.actions.onSuggestionsLoaded).toHaveBeenCalledWith(MOCK_RESPONSE); | ||
}); | ||
|
||
it('handles errors', async () => { | ||
http.post.mockReturnValueOnce(Promise.reject('error')); | ||
mount(); | ||
|
||
SuggestionsLogic.actions.loadSuggestions(); | ||
await nextTick(); | ||
|
||
expect(flashAPIErrors).toHaveBeenCalledWith('error'); | ||
}); | ||
}); | ||
}); | ||
}); |
98 changes: 98 additions & 0 deletions
98
...arch/public/applications/app_search/components/curations/components/suggestions_logic.tsx
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 { kea, MakeLogicType } from 'kea'; | ||
|
||
import { Meta } from '../../../../../../common/types'; | ||
import { DEFAULT_META } from '../../../../shared/constants'; | ||
import { flashAPIErrors } from '../../../../shared/flash_messages'; | ||
import { HttpLogic } from '../../../../shared/http'; | ||
import { updateMetaPageIndex } from '../../../../shared/table_pagination'; | ||
import { EngineLogic } from '../../engine'; | ||
import { CurationSuggestion } from '../types'; | ||
|
||
interface SuggestionsAPIResponse { | ||
results: CurationSuggestion[]; | ||
meta: Meta; | ||
} | ||
|
||
interface SuggestionsValues { | ||
dataLoading: boolean; | ||
suggestions: CurationSuggestion[]; | ||
meta: Meta; | ||
} | ||
|
||
interface SuggestionActions { | ||
loadSuggestions(): void; | ||
onPaginate(newPageIndex: number): { newPageIndex: number }; | ||
onSuggestionsLoaded(response: SuggestionsAPIResponse): SuggestionsAPIResponse; | ||
} | ||
|
||
export const SuggestionsLogic = kea<MakeLogicType<SuggestionsValues, SuggestionActions>>({ | ||
path: ['enterprise_search', 'app_search', 'curations', 'suggestions_logic'], | ||
actions: () => ({ | ||
onPaginate: (newPageIndex) => ({ newPageIndex }), | ||
onSuggestionsLoaded: ({ results, meta }) => ({ results, meta }), | ||
loadSuggestions: true, | ||
}), | ||
reducers: () => ({ | ||
dataLoading: [ | ||
true, | ||
{ | ||
loadSuggestions: () => true, | ||
onSuggestionsLoaded: () => false, | ||
}, | ||
], | ||
suggestions: [ | ||
[], | ||
{ | ||
onSuggestionsLoaded: (_, { results }) => results, | ||
}, | ||
], | ||
meta: [ | ||
{ | ||
...DEFAULT_META, | ||
page: { | ||
...DEFAULT_META.page, | ||
size: 10, | ||
}, | ||
}, | ||
{ | ||
onSuggestionsLoaded: (_, { meta }) => meta, | ||
onPaginate: (state, { newPageIndex }) => updateMetaPageIndex(state, newPageIndex), | ||
}, | ||
], | ||
}), | ||
listeners: ({ actions, values }) => ({ | ||
loadSuggestions: async () => { | ||
const { meta } = values; | ||
const { http } = HttpLogic.values; | ||
const { engineName } = EngineLogic.values; | ||
|
||
try { | ||
const response = await http.post( | ||
`/internal/app_search/engines/${engineName}/search_relevance_suggestions`, | ||
{ | ||
body: JSON.stringify({ | ||
page: { | ||
current: meta.page.current, | ||
size: meta.page.size, | ||
}, | ||
filters: { | ||
status: ['pending'], | ||
type: 'curation', | ||
}, | ||
}), | ||
} | ||
); | ||
actions.onSuggestionsLoaded(response); | ||
} catch (e) { | ||
flashAPIErrors(e); | ||
} | ||
}, | ||
}), | ||
}); |
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
Oops, something went wrong.