|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React, { useContext } from 'react'; |
| 7 | +import userEvent from '@testing-library/user-event'; |
| 8 | +import { render, screen, waitFor } from '../../../test/test_utils'; |
| 9 | +import { DataSourceTopNavMenu, DataSourceTopNavMenuProps } from '../data_source_top_nav_menu'; |
| 10 | +import { coreMock } from '../../../../../src/core/public/mocks'; |
| 11 | +import { DataSourceContext } from '../../contexts'; |
| 12 | + |
| 13 | +function setup(options: Partial<DataSourceTopNavMenuProps> = {}) { |
| 14 | + const user = userEvent.setup({}); |
| 15 | + const coreStart = coreMock.createStart(); |
| 16 | + const DataSourceMenu = ({ componentConfig: { onSelectedDataSources } }) => ( |
| 17 | + <div> |
| 18 | + <div>Data Source Menu</div> |
| 19 | + <div> |
| 20 | + <button |
| 21 | + onClick={() => { |
| 22 | + onSelectedDataSources([]); |
| 23 | + }} |
| 24 | + aria-label="invalidDataSource" |
| 25 | + > |
| 26 | + Invalid data source |
| 27 | + </button> |
| 28 | + <button |
| 29 | + onClick={() => { |
| 30 | + onSelectedDataSources([{ id: 'ds1', label: 'Data Source 1' }]); |
| 31 | + }} |
| 32 | + aria-label="validDataSource" |
| 33 | + > |
| 34 | + Valid data source |
| 35 | + </button> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + ); |
| 39 | + |
| 40 | + const DataSourceConsumer = () => { |
| 41 | + const { selectedDataSourceOption } = useContext(DataSourceContext); |
| 42 | + |
| 43 | + return ( |
| 44 | + <div> |
| 45 | + <input |
| 46 | + value={ |
| 47 | + selectedDataSourceOption === undefined |
| 48 | + ? 'undefined' |
| 49 | + : JSON.stringify(selectedDataSourceOption) |
| 50 | + } |
| 51 | + aria-label="selectedDataSourceOption" |
| 52 | + onChange={() => {}} |
| 53 | + /> |
| 54 | + </div> |
| 55 | + ); |
| 56 | + }; |
| 57 | + |
| 58 | + const renderResult = render( |
| 59 | + <> |
| 60 | + <DataSourceTopNavMenu |
| 61 | + notifications={coreStart.notifications} |
| 62 | + savedObjects={coreStart.savedObjects} |
| 63 | + dataSourceManagement={{ |
| 64 | + registerAuthenticationMethod: jest.fn(), |
| 65 | + ui: { |
| 66 | + DataSourceSelector: () => null, |
| 67 | + getDataSourceMenu: () => DataSourceMenu, |
| 68 | + }, |
| 69 | + }} |
| 70 | + setActionMenu={jest.fn()} |
| 71 | + {...options} |
| 72 | + /> |
| 73 | + <DataSourceConsumer /> |
| 74 | + </> |
| 75 | + ); |
| 76 | + return { user, renderResult }; |
| 77 | +} |
| 78 | + |
| 79 | +describe('<DataSourceTopNavMenu />', () => { |
| 80 | + it('should not render data source menu when data source management not defined', () => { |
| 81 | + setup({ |
| 82 | + dataSourceManagement: undefined, |
| 83 | + }); |
| 84 | + expect(screen.queryByText('Data Source Menu')).not.toBeInTheDocument(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should render data source menu and data source context', () => { |
| 88 | + setup(); |
| 89 | + expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); |
| 90 | + expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('null'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should set selected data source option to undefined', async () => { |
| 94 | + const { user } = setup(); |
| 95 | + expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); |
| 96 | + await user.click(screen.getByLabelText('invalidDataSource')); |
| 97 | + await waitFor(() => { |
| 98 | + expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue('undefined'); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should set selected data source option to valid data source', async () => { |
| 103 | + const { user } = setup(); |
| 104 | + expect(screen.getByText('Data Source Menu')).toBeInTheDocument(); |
| 105 | + await user.click(screen.getByLabelText('validDataSource')); |
| 106 | + await waitFor(() => { |
| 107 | + expect(screen.getByLabelText('selectedDataSourceOption')).toHaveValue( |
| 108 | + JSON.stringify({ id: 'ds1', label: 'Data Source 1' }) |
| 109 | + ); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments