-
Notifications
You must be signed in to change notification settings - Fork 23
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: Lin Wang <[email protected]>
- Loading branch information
Showing
50 changed files
with
2,124 additions
and
129 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
150 changes: 150 additions & 0 deletions
150
public/components/__tests__/data_source_top_nav_menu.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,150 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useContext } from 'react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { render, screen, waitFor } from '../../../test/test_utils'; | ||
import { DataSourceTopNavMenu, DataSourceTopNavMenuProps } from '../data_source_top_nav_menu'; | ||
import { coreMock } from '../../../../../src/core/public/mocks'; | ||
import { DataSourceContext, DataSourceContextProvider } from '../../contexts'; | ||
|
||
function setup(options: Partial<DataSourceTopNavMenuProps> = {}) { | ||
const user = userEvent.setup({}); | ||
const coreStart = coreMock.createStart(); | ||
const DataSourceMenu = ({ componentConfig: { onSelectedDataSources } }) => ( | ||
<div> | ||
<div>Data Source Menu</div> | ||
<div> | ||
<button | ||
onClick={() => { | ||
onSelectedDataSources([]); | ||
}} | ||
aria-label="invalidDataSource" | ||
> | ||
Invalid data source | ||
</button> | ||
<button | ||
onClick={() => { | ||
onSelectedDataSources([{ id: 'ds1', label: 'Data Source 1' }]); | ||
}} | ||
aria-label="validDataSource" | ||
> | ||
Valid data source | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
|
||
const DataSourceConsumer = () => { | ||
const { dataSourceEnabled, selectedDataSourceOption } = useContext(DataSourceContext); | ||
|
||
return ( | ||
<div> | ||
<input | ||
value={JSON.stringify(dataSourceEnabled)} | ||
aria-label="dataSourceEnabled" | ||
onChange={() => {}} | ||
/> | ||
<input | ||
value={ | ||
selectedDataSourceOption === undefined | ||
? 'undefined' | ||
: JSON.stringify(selectedDataSourceOption) | ||
} | ||
aria-label="selectedDataSourceOption" | ||
onChange={() => {}} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
const renderResult = render( | ||
<> | ||
<DataSourceTopNavMenu | ||
notifications={coreStart.notifications} | ||
savedObjects={coreStart.savedObjects} | ||
dataSource={{ | ||
dataSourceEnabled: true, | ||
noAuthenticationTypeEnabled: true, | ||
awsSigV4AuthEnabled: true, | ||
hideLocalCluster: false, | ||
usernamePasswordAuthEnabled: true, | ||
}} | ||
dataSourceManagement={{ | ||
registerAuthenticationMethod: jest.fn(), | ||
ui: { | ||
DataSourceSelector: () => null, | ||
getDataSourceMenu: () => DataSourceMenu, | ||
}, | ||
}} | ||
setActionMenu={jest.fn()} | ||
{...options} | ||
/> | ||
<DataSourceConsumer /> | ||
</> | ||
); | ||
return { user, renderResult }; | ||
} | ||
|
||
describe('<DataSourceTopNavMenu />', () => { | ||
it('should not render data source menu when data source and data source management not defined', () => { | ||
setup({ | ||
dataSource: undefined, | ||
dataSourceManagement: undefined, | ||
}); | ||
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); | ||
}); | ||
it('should not render data source menu when data source not defined', () => { | ||
setup({ | ||
dataSource: undefined, | ||
}); | ||
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); | ||
}); | ||
it('should not render data source menu when data source management not defined', () => { | ||
setup({ | ||
dataSourceManagement: undefined, | ||
}); | ||
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); | ||
}); | ||
it('should not render data source menu when data source not enabled', () => { | ||
setup({ | ||
dataSource: { | ||
dataSourceEnabled: false, | ||
noAuthenticationTypeEnabled: false, | ||
awsSigV4AuthEnabled: false, | ||
hideLocalCluster: false, | ||
usernamePasswordAuthEnabled: false, | ||
}, | ||
}); | ||
expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render data source menu and data source context', () => { | ||
setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
expect(screen.getByLabelText('dataSourceEnabled')).toHaveValue('true'); | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('null'); | ||
}); | ||
|
||
it('should set selected data source option to undefined', async () => { | ||
const { user } = setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
await user.click(screen.getByLabelText('invalidDataSource')); | ||
await waitFor(() => { | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('undefined'); | ||
}); | ||
}); | ||
|
||
it('should set selected data source option to valid data source', async () => { | ||
const { user } = setup(); | ||
expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); | ||
await user.click(screen.getByLabelText('validDataSource')); | ||
await waitFor(() => { | ||
expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue( | ||
JSON.stringify({ id: 'ds1', label: 'Data Source 1' }) | ||
); | ||
}); | ||
}); | ||
}); |
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.