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] Added Sample Response section to Result Settings (elasti…
- Loading branch information
1 parent
6f149e2
commit ec2ae6a
Showing
11 changed files
with
556 additions
and
1 deletion.
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
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
8 changes: 8 additions & 0 deletions
8
...search/public/applications/app_search/components/result_settings/sample_response/index.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,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { SampleResponse } from './sample_response'; |
75 changes: 75 additions & 0 deletions
75
...plications/app_search/components/result_settings/sample_response/sample_response.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,75 @@ | ||
/* | ||
* 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 '../../../../__mocks__/shallow_useeffect.mock'; | ||
import { setMockActions, setMockValues } from '../../../../__mocks__'; | ||
|
||
import React from 'react'; | ||
|
||
import { shallow } from 'enzyme'; | ||
|
||
import { EuiCodeBlock, EuiFieldSearch } from '@elastic/eui'; | ||
|
||
import { SampleResponse } from './sample_response'; | ||
|
||
describe('SampleResponse', () => { | ||
const actions = { | ||
queryChanged: jest.fn(), | ||
getSearchResults: jest.fn(), | ||
}; | ||
|
||
const values = { | ||
reducedServerResultFields: {}, | ||
query: 'foo', | ||
response: { | ||
bar: 'baz', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
setMockActions(actions); | ||
setMockValues(values); | ||
}); | ||
|
||
it('renders a text box with the current user "query" value from state', () => { | ||
const wrapper = shallow(<SampleResponse />); | ||
expect(wrapper.find(EuiFieldSearch).prop('value')).toEqual('foo'); | ||
}); | ||
|
||
it('updates the "query" value in state when a user updates the text in the text box', () => { | ||
const wrapper = shallow(<SampleResponse />); | ||
wrapper.find(EuiFieldSearch).simulate('change', { target: { value: 'bar' } }); | ||
expect(actions.queryChanged).toHaveBeenCalledWith('bar'); | ||
}); | ||
|
||
it('will call getSearchResults with the current value of query and reducedServerResultFields in a useEffect, which updates the displayed response', () => { | ||
const wrapper = shallow(<SampleResponse />); | ||
expect(wrapper.find(EuiFieldSearch).prop('value')).toEqual('foo'); | ||
}); | ||
|
||
it('renders the response from the given user "query" in a code block', () => { | ||
const wrapper = shallow(<SampleResponse />); | ||
expect(wrapper.find(EuiCodeBlock).prop('children')).toEqual('{\n "bar": "baz"\n}'); | ||
}); | ||
|
||
it('renders a plain old string in the code block if the response is a string', () => { | ||
setMockValues({ | ||
response: 'No results.', | ||
}); | ||
const wrapper = shallow(<SampleResponse />); | ||
expect(wrapper.find(EuiCodeBlock).prop('children')).toEqual('No results.'); | ||
}); | ||
|
||
it('will not render a code block at all if there is no response yet', () => { | ||
setMockValues({ | ||
response: null, | ||
}); | ||
const wrapper = shallow(<SampleResponse />); | ||
expect(wrapper.find(EuiCodeBlock).exists()).toEqual(false); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
...ic/applications/app_search/components/result_settings/sample_response/sample_response.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,72 @@ | ||
/* | ||
* 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 React, { useEffect } from 'react'; | ||
|
||
import { useActions, useValues } from 'kea'; | ||
|
||
import { | ||
EuiCodeBlock, | ||
EuiFieldSearch, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiPanel, | ||
EuiSpacer, | ||
EuiTitle, | ||
} from '@elastic/eui'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { ResultSettingsLogic } from '../result_settings_logic'; | ||
|
||
import { SampleResponseLogic } from './sample_response_logic'; | ||
|
||
export const SampleResponse: React.FC = () => { | ||
const { reducedServerResultFields } = useValues(ResultSettingsLogic); | ||
|
||
const { query, response } = useValues(SampleResponseLogic); | ||
const { queryChanged, getSearchResults } = useActions(SampleResponseLogic); | ||
|
||
useEffect(() => { | ||
getSearchResults(query, reducedServerResultFields); | ||
}, [query, reducedServerResultFields]); | ||
|
||
return ( | ||
<EuiPanel hasBorder> | ||
<EuiFlexGroup alignItems="center"> | ||
<EuiFlexItem> | ||
<EuiTitle size="s"> | ||
<h2> | ||
{i18n.translate( | ||
'xpack.enterpriseSearch.appSearch.engine.resultSettings.sampleResponseTitle', | ||
{ defaultMessage: 'Sample response' } | ||
)} | ||
</h2> | ||
</EuiTitle> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
{/* TODO <QueryPerformance queryPerformanceRating={queryPerformanceRating} /> */} | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiSpacer /> | ||
<EuiFieldSearch | ||
value={query} | ||
onChange={(e) => queryChanged(e.target.value)} | ||
placeholder={i18n.translate( | ||
'xpack.enterpriseSearch.appSearch.engine.resultSettings.sampleResponse.inputPlaceholder', | ||
{ defaultMessage: 'Type a search query to test a response...' } | ||
)} | ||
data-test-subj="ResultSettingsQuerySampleResponse" | ||
/> | ||
<EuiSpacer /> | ||
{!!response && ( | ||
<EuiCodeBlock language="json" whiteSpace="pre-wrap"> | ||
{typeof response === 'string' ? response : JSON.stringify(response, null, 2)} | ||
</EuiCodeBlock> | ||
)} | ||
</EuiPanel> | ||
); | ||
}; |
Oops, something went wrong.