-
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] Added the Documents View (#83947)
- Loading branch information
1 parent
f176e8b
commit 40e206e
Showing
29 changed files
with
1,464 additions
and
19 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
22 changes: 22 additions & 0 deletions
22
...rch/public/applications/app_search/components/documents/document_creation_button.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,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import { EuiButton } from '@elastic/eui'; | ||
|
||
import { DocumentCreationButton } from './document_creation_button'; | ||
|
||
describe('DocumentCreationButton', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should render', () => { | ||
const wrapper = shallow(<DocumentCreationButton />); | ||
expect(wrapper.find(EuiButton).length).toEqual(1); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
...e_search/public/applications/app_search/components/documents/document_creation_button.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,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { EuiButton } from '@elastic/eui'; | ||
|
||
export const DocumentCreationButton: React.FC = () => { | ||
return ( | ||
<EuiButton fill={true} color="primary" data-test-subj="IndexDocumentsButton"> | ||
{i18n.translate('xpack.enterpriseSearch.appSearch.documents.indexDocuments', { | ||
defaultMessage: 'Index documents', | ||
})} | ||
</EuiButton> | ||
); | ||
}; |
73 changes: 73 additions & 0 deletions
73
.../enterprise_search/public/applications/app_search/components/documents/documents.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,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { setMockValues } from '../../../__mocks__/kea.mock'; | ||
|
||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
|
||
import { DocumentCreationButton } from './document_creation_button'; | ||
import { SearchExperience } from './search_experience'; | ||
import { Documents } from '.'; | ||
|
||
describe('Documents', () => { | ||
const values = { | ||
isMetaEngine: false, | ||
myRole: { canManageEngineDocuments: true }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
setMockValues(values); | ||
}); | ||
|
||
it('renders', () => { | ||
const wrapper = shallow(<Documents engineBreadcrumb={['test']} />); | ||
expect(wrapper.find(SearchExperience).exists()).toBe(true); | ||
}); | ||
|
||
it('renders a DocumentCreationButton if the user can manage engine documents', () => { | ||
setMockValues({ | ||
...values, | ||
myRole: { canManageEngineDocuments: true }, | ||
}); | ||
|
||
const wrapper = shallow(<Documents engineBreadcrumb={['test']} />); | ||
expect(wrapper.find(DocumentCreationButton).exists()).toBe(true); | ||
}); | ||
|
||
describe('Meta Engines', () => { | ||
it('renders a Meta Engines message if this is a meta engine', () => { | ||
setMockValues({ | ||
...values, | ||
isMetaEngine: true, | ||
}); | ||
|
||
const wrapper = shallow(<Documents engineBreadcrumb={['test']} />); | ||
expect(wrapper.find('[data-test-subj="MetaEnginesCallout"]').exists()).toBe(true); | ||
}); | ||
|
||
it('does not render a Meta Engines message if this is not a meta engine', () => { | ||
setMockValues({ | ||
...values, | ||
isMetaEngine: false, | ||
}); | ||
|
||
const wrapper = shallow(<Documents engineBreadcrumb={['test']} />); | ||
expect(wrapper.find('[data-test-subj="MetaEnginesCallout"]').exists()).toBe(false); | ||
}); | ||
|
||
it('does not render a DocumentCreationButton even if the user can manage engine documents', () => { | ||
setMockValues({ | ||
...values, | ||
myRole: { canManageEngineDocuments: true }, | ||
isMetaEngine: true, | ||
}); | ||
|
||
const wrapper = shallow(<Documents engineBreadcrumb={['test']} />); | ||
expect(wrapper.find(DocumentCreationButton).exists()).toBe(false); | ||
}); | ||
}); | ||
}); |
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
19 changes: 19 additions & 0 deletions
19
...ic/applications/app_search/components/documents/search_experience/__mocks__/hooks.mock.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,19 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
jest.mock('../hooks', () => ({ | ||
useSearchContextActions: jest.fn(() => ({})), | ||
useSearchContextState: jest.fn(() => ({})), | ||
})); | ||
|
||
import { useSearchContextState, useSearchContextActions } from '../hooks'; | ||
|
||
export const setMockSearchContextState = (values: object) => { | ||
(useSearchContextState as jest.Mock).mockImplementation(() => values); | ||
}; | ||
export const setMockSearchContextActions = (actions: object) => { | ||
(useSearchContextActions as jest.Mock).mockImplementation(() => actions); | ||
}; |
75 changes: 75 additions & 0 deletions
75
...arch/public/applications/app_search/components/documents/search_experience/hooks.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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
const mockAction = jest.fn(); | ||
|
||
let mockSubcription: (state: object) => void; | ||
const mockDriver = { | ||
state: { foo: 'foo' }, | ||
actions: { bar: mockAction }, | ||
subscribeToStateChanges: jest.fn().mockImplementation((fn) => { | ||
mockSubcription = fn; | ||
}), | ||
unsubscribeToStateChanges: jest.fn(), | ||
}; | ||
|
||
jest.mock('react', () => ({ | ||
...(jest.requireActual('react') as object), | ||
useContext: jest.fn(() => ({ | ||
driver: mockDriver, | ||
})), | ||
})); | ||
|
||
import React from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
|
||
import { useSearchContextState, useSearchContextActions } from './hooks'; | ||
|
||
describe('hooks', () => { | ||
describe('useSearchContextState', () => { | ||
const TestComponent = () => { | ||
const { foo } = useSearchContextState(); | ||
return <div>{foo}</div>; | ||
}; | ||
|
||
let wrapper: ReactWrapper; | ||
beforeAll(() => { | ||
wrapper = mount(<TestComponent />); | ||
}); | ||
|
||
it('exposes search state', () => { | ||
expect(wrapper.text()).toEqual('foo'); | ||
}); | ||
|
||
it('subscribes to state changes', () => { | ||
act(() => { | ||
mockSubcription({ foo: 'bar' }); | ||
}); | ||
|
||
expect(wrapper.text()).toEqual('bar'); | ||
}); | ||
|
||
it('unsubscribes to state changes when unmounted', () => { | ||
wrapper.unmount(); | ||
|
||
expect(mockDriver.unsubscribeToStateChanges).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('useSearchContextActions', () => { | ||
it('exposes actions', () => { | ||
const TestComponent = () => { | ||
const { bar } = useSearchContextActions(); | ||
bar(); | ||
return null; | ||
}; | ||
|
||
mount(<TestComponent />); | ||
expect(mockAction).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
...ise_search/public/applications/app_search/components/documents/search_experience/hooks.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,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { useContext, useEffect, useState } from 'react'; | ||
|
||
// @ts-expect-error types are not available for this package yet | ||
import { SearchContext } from '@elastic/react-search-ui'; | ||
|
||
export const useSearchContextState = () => { | ||
const { driver } = useContext(SearchContext); | ||
const [state, setState] = useState(driver.state); | ||
|
||
useEffect(() => { | ||
driver.subscribeToStateChanges((newState: object) => { | ||
setState(newState); | ||
}); | ||
return () => { | ||
driver.unsubscribeToStateChanges(); | ||
}; | ||
}, [state]); | ||
|
||
return state; | ||
}; | ||
|
||
export const useSearchContextActions = () => { | ||
const { driver } = useContext(SearchContext); | ||
return driver.actions; | ||
}; |
7 changes: 7 additions & 0 deletions
7
...ise_search/public/applications/app_search/components/documents/search_experience/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,7 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export { SearchExperience } from './search_experience'; |
26 changes: 26 additions & 0 deletions
26
...public/applications/app_search/components/documents/search_experience/pagination.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,26 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { shallow } from 'enzyme'; | ||
// @ts-expect-error types are not available for this package yet | ||
import { Paging, ResultsPerPage } from '@elastic/react-search-ui'; | ||
|
||
import { Pagination } from './pagination'; | ||
|
||
describe('Pagination', () => { | ||
it('renders', () => { | ||
const wrapper = shallow(<Pagination aria-label="foo" />); | ||
expect(wrapper.find(Paging).exists()).toBe(true); | ||
expect(wrapper.find(ResultsPerPage).exists()).toBe(true); | ||
}); | ||
|
||
it('passes aria-label through to Paging', () => { | ||
const wrapper = shallow(<Pagination aria-label="foo" />); | ||
expect(wrapper.find(Paging).prop('aria-label')).toEqual('foo'); | ||
}); | ||
}); |
22 changes: 22 additions & 0 deletions
22
...arch/public/applications/app_search/components/documents/search_experience/pagination.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,22 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React from 'react'; | ||
|
||
import { EuiFlexGroup, EuiFlexItem } from '@elastic/eui'; | ||
// @ts-expect-error types are not available for this package yet | ||
import { Paging, ResultsPerPage } from '@elastic/react-search-ui'; | ||
import { PagingView, ResultsPerPageView } from './views'; | ||
|
||
export const Pagination: React.FC<{ 'aria-label': string }> = ({ 'aria-label': ariaLabel }) => ( | ||
<EuiFlexGroup alignItems="center" className="documentsSearchExperience__pagingInfo"> | ||
<EuiFlexItem> | ||
<Paging view={PagingView} aria-label={ariaLabel} /> | ||
</EuiFlexItem> | ||
<EuiFlexItem grow={false}> | ||
<ResultsPerPage view={ResultsPerPageView} /> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
); |
Oops, something went wrong.