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.
Merge pull request elastic#36 from Elastic-AWP-Platform/search-bar-te…
…sts-and-nav Search Bar improvements
- Loading branch information
Showing
7 changed files
with
219 additions
and
10 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
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
95 changes: 95 additions & 0 deletions
95
x-pack/plugins/session_view/public/components/SessionViewSearchBar/index.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,95 @@ | ||
/* | ||
* 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 from 'react'; | ||
import { processMock } from '../../../common/mocks/constants/session_view_process.mock'; | ||
import { AppContextTestRender, createAppRootMockRenderer } from '../../test'; | ||
import { SessionViewSearchBar } from './index'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { fireEvent } from '@testing-library/dom'; | ||
|
||
describe('SessionViewSearchBar component', () => { | ||
let render: () => ReturnType<AppContextTestRender['render']>; | ||
let renderResult: ReturnType<typeof render>; | ||
let mockedContext: AppContextTestRender; | ||
|
||
beforeEach(() => { | ||
mockedContext = createAppRootMockRenderer(); | ||
}); | ||
|
||
it('handles a typed search query', async () => { | ||
const mockSetSearchQuery = jest.fn((query) => query); | ||
const mockSetSelectedProcess = jest.fn((process) => process); | ||
|
||
renderResult = mockedContext.render( | ||
<SessionViewSearchBar | ||
searchQuery="ls" | ||
searchResults={[]} | ||
setSelectedProcess={mockSetSelectedProcess} | ||
setSearchQuery={mockSetSearchQuery} | ||
/> | ||
); | ||
|
||
const searchInput = renderResult.getByTestId('searchInput').querySelector('input'); | ||
|
||
expect(searchInput?.value).toEqual('ls'); | ||
|
||
if (searchInput) { | ||
userEvent.type(searchInput, ' -la'); | ||
fireEvent.keyUp(searchInput, { key: 'Enter', code: 'Enter' }); | ||
} | ||
|
||
expect(searchInput?.value).toEqual('ls -la'); | ||
expect(mockSetSearchQuery.mock.calls.length).toBe(1); | ||
expect(mockSetSearchQuery.mock.results[0].value).toBe('ls -la'); | ||
}); | ||
|
||
it('shows a results navigator when searchResults provided', async () => { | ||
const processMock2 = { ...processMock }; | ||
const processMock3 = { ...processMock }; | ||
const mockResults = [processMock, processMock2, processMock3]; | ||
const mockSetSearchQuery = jest.fn((query) => query); | ||
const mockSetSelectedProcess = jest.fn((process) => process); | ||
|
||
renderResult = mockedContext.render( | ||
<SessionViewSearchBar | ||
searchQuery="ls" | ||
searchResults={mockResults} | ||
setSelectedProcess={mockSetSelectedProcess} | ||
setSearchQuery={mockSetSearchQuery} | ||
/> | ||
); | ||
|
||
const searchPagination = renderResult.getByTestId('searchPagination'); | ||
expect(searchPagination).toBeTruthy(); | ||
|
||
const paginationTextClass = '.euiPagination__compressedText'; | ||
expect(searchPagination.querySelector(paginationTextClass)?.textContent).toEqual('1 of 3'); | ||
|
||
userEvent.click(renderResult.getByTestId('pagination-button-next')); | ||
expect(searchPagination.querySelector(paginationTextClass)?.textContent).toEqual('2 of 3'); | ||
|
||
const searchInput = renderResult.getByTestId('searchInput').querySelector('input'); | ||
|
||
if (searchInput) { | ||
userEvent.type(searchInput, ' -la'); | ||
fireEvent.keyUp(searchInput, { key: 'Enter', code: 'Enter' }); | ||
} | ||
|
||
// after search is changed, results index should reset to 1 | ||
expect(searchPagination.querySelector(paginationTextClass)?.textContent).toEqual('1 of 3'); | ||
|
||
// setSelectedProcess should be called 3 times: | ||
// 1. searchResults is set so auto select first item | ||
// 2. next button hit, so call with 2nd item | ||
// 3. search changed, so call with first result. | ||
expect(mockSetSelectedProcess.mock.calls.length).toBe(3); | ||
expect(mockSetSelectedProcess.mock.results[0].value).toEqual(processMock); | ||
expect(mockSetSelectedProcess.mock.results[1].value).toEqual(processMock2); | ||
expect(mockSetSelectedProcess.mock.results[1].value).toEqual(processMock); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/session_view/public/components/SessionViewSearchBar/index.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, { useState, useEffect } from 'react'; | ||
import { EuiSearchBar, EuiPagination } from '@elastic/eui'; | ||
import { EuiSearchBarOnChangeArgs } from '@elastic/eui'; | ||
import { Process } from '../../../common/types/process_tree'; | ||
import { useStyles } from './styles'; | ||
|
||
interface SessionViewSearchBarDeps { | ||
searchQuery: string; | ||
setSearchQuery(val: string): void; | ||
searchResults: Process[] | null; | ||
setSelectedProcess(process: Process): void; | ||
} | ||
|
||
/** | ||
* The main wrapper component for the session view. | ||
*/ | ||
export const SessionViewSearchBar = ({ | ||
searchQuery, | ||
setSearchQuery, | ||
setSelectedProcess, | ||
searchResults, | ||
}: SessionViewSearchBarDeps) => { | ||
const styles = useStyles(); | ||
|
||
const [selectedResult, setSelectedResult] = useState(0); | ||
|
||
const onSearch = ({ query }: EuiSearchBarOnChangeArgs) => { | ||
setSelectedResult(0); | ||
|
||
if (query) { | ||
setSearchQuery(query.text); | ||
} else { | ||
setSearchQuery(''); | ||
} | ||
}; | ||
|
||
const renderSearchResults = () => { | ||
if (searchResults?.length) { | ||
return ( | ||
<EuiPagination | ||
data-test-subj="searchPagination" | ||
css={styles.pagination} | ||
pageCount={searchResults.length} | ||
activePage={selectedResult} | ||
onPageClick={setSelectedResult} | ||
compressed | ||
/> | ||
); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (searchResults) { | ||
const process = searchResults[selectedResult]; | ||
|
||
setSelectedProcess(process); | ||
} | ||
}, [searchResults, setSelectedProcess, selectedResult]); | ||
|
||
return ( | ||
<div data-test-subj="searchInput" css={{ position: 'relative' }}> | ||
<EuiSearchBar query={searchQuery} onChange={onSearch} /> | ||
{renderSearchResults()} | ||
</div> | ||
); | ||
}; |
28 changes: 28 additions & 0 deletions
28
x-pack/plugins/session_view/public/components/SessionViewSearchBar/styles.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,28 @@ | ||
/* | ||
* 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 { useMemo } from 'react'; | ||
import { useEuiTheme } from '@elastic/eui'; | ||
import { CSSObject } from '@emotion/react'; | ||
|
||
export const useStyles = () => { | ||
const { euiTheme } = useEuiTheme(); | ||
|
||
const cached = useMemo(() => { | ||
const pagination: CSSObject = { | ||
position: 'absolute', | ||
top: euiTheme.size.s, | ||
right: euiTheme.size.xxl, | ||
}; | ||
|
||
return { | ||
pagination, | ||
}; | ||
}, [euiTheme]); | ||
|
||
return cached; | ||
}; |