-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joshua Li <[email protected]>
- Loading branch information
1 parent
0589b24
commit 04661f7
Showing
11 changed files
with
367 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { SearchBarExtensionConfig } from './search_bar_extension'; | ||
export { SearchBarExtensions } from './search_bar_extensions'; |
94 changes: 94 additions & 0 deletions
94
src/plugins/data/public/ui/search_bar_extensions/search_bar_extension.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,94 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { render, waitFor } from '@testing-library/react'; | ||
import React, { ComponentProps } from 'react'; | ||
import { IIndexPattern } from '../../../common'; | ||
import { SearchBarExtension } from './search_bar_extension'; | ||
|
||
jest.mock('@elastic/eui', () => ({ | ||
...jest.requireActual('@elastic/eui'), | ||
EuiPortal: jest.fn(({ children }) => <div>{children}</div>), | ||
EuiErrorBoundary: jest.fn(({ children }) => <div>{children}</div>), | ||
})); | ||
|
||
type SearchBarExtensionProps = ComponentProps<typeof SearchBarExtension>; | ||
|
||
const mockIndexPattern = { | ||
id: '1234', | ||
title: 'logstash-*', | ||
fields: [ | ||
{ | ||
name: 'response', | ||
type: 'number', | ||
esTypes: ['integer'], | ||
aggregatable: true, | ||
filterable: true, | ||
searchable: true, | ||
}, | ||
], | ||
} as IIndexPattern; | ||
|
||
describe('SearchBarExtension', () => { | ||
const getComponentMock = jest.fn(); | ||
const isEnabledMock = jest.fn(); | ||
|
||
const defaultProps: SearchBarExtensionProps = { | ||
config: { | ||
id: 'test-extension', | ||
order: 1, | ||
isEnabled: isEnabledMock, | ||
getComponent: getComponentMock, | ||
}, | ||
dependencies: { | ||
indexPatterns: [mockIndexPattern], | ||
}, | ||
portalInsert: { sibling: document.createElement('div'), position: 'after' }, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly when isEnabled is true', async () => { | ||
isEnabledMock.mockResolvedValue(true); | ||
getComponentMock.mockReturnValue(<div>Test Component</div>); | ||
|
||
const { getByText } = render(<SearchBarExtension {...defaultProps} />); | ||
|
||
await waitFor(() => { | ||
expect(getByText('Test Component')).toBeInTheDocument(); | ||
}); | ||
|
||
expect(isEnabledMock).toHaveBeenCalled(); | ||
expect(getComponentMock).toHaveBeenCalledWith(defaultProps.dependencies); | ||
}); | ||
|
||
it('does not render when isEnabled is false', async () => { | ||
isEnabledMock.mockResolvedValue(false); | ||
getComponentMock.mockReturnValue(<div>Test Component</div>); | ||
|
||
const { queryByText } = render(<SearchBarExtension {...defaultProps} />); | ||
|
||
await waitFor(() => { | ||
expect(queryByText('Test Component')).toBeNull(); | ||
}); | ||
|
||
expect(isEnabledMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls isEnabled and getComponent correctly', async () => { | ||
isEnabledMock.mockResolvedValue(true); | ||
getComponentMock.mockReturnValue(<div>Test Component</div>); | ||
|
||
render(<SearchBarExtension {...defaultProps} />); | ||
|
||
await waitFor(() => { | ||
expect(isEnabledMock).toHaveBeenCalled(); | ||
}); | ||
|
||
expect(getComponentMock).toHaveBeenCalledWith(defaultProps.dependencies); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
src/plugins/data/public/ui/search_bar_extensions/search_bar_extension.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,65 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { EuiErrorBoundary, EuiPortal } from '@elastic/eui'; | ||
import { EuiPortalProps } from '@opensearch-project/oui'; | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { IIndexPattern } from '../../../common'; | ||
|
||
interface SearchBarExtensionProps { | ||
config: SearchBarExtensionConfig; | ||
dependencies: SearchBarExtensionDependencies; | ||
portalInsert: EuiPortalProps['insert']; | ||
} | ||
|
||
export interface SearchBarExtensionDependencies { | ||
/** | ||
* Currently selected index patterns. | ||
*/ | ||
indexPatterns?: IIndexPattern[]; | ||
} | ||
|
||
export interface SearchBarExtensionConfig { | ||
/** | ||
* The id for the search bar extension. | ||
*/ | ||
id: string; | ||
/** | ||
* Lower order indicates higher position on UI. | ||
*/ | ||
order: number; | ||
/** | ||
* A function that determines if the search bar extension is enabled and should be rendered on UI. | ||
* @returns whether the extension is enabled. | ||
*/ | ||
isEnabled: () => Promise<boolean>; | ||
/** | ||
* A function that returns the mount point for the search bar extension. | ||
* @param dependencies - The dependencies required for the extension. | ||
* @returns The component the search bar extension. | ||
*/ | ||
getComponent: (dependencies: SearchBarExtensionDependencies) => React.ReactElement; | ||
} | ||
|
||
export const SearchBarExtension: React.FC<SearchBarExtensionProps> = (props) => { | ||
const [isEnabled, setIsEnabled] = useState(false); | ||
|
||
const component = useMemo(() => props.config.getComponent(props.dependencies), [ | ||
props.config, | ||
props.dependencies, | ||
]); | ||
|
||
useEffect(() => { | ||
props.config.isEnabled().then(setIsEnabled); | ||
}, [props.dependencies, props.config]); | ||
|
||
if (!isEnabled) return null; | ||
|
||
return ( | ||
<EuiPortal insert={props.portalInsert}> | ||
<EuiErrorBoundary>{component}</EuiErrorBoundary> | ||
</EuiPortal> | ||
); | ||
}; |
Oops, something went wrong.