forked from opensearch-project/ml-commons-dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_top_nav_menu.test.tsx
112 lines (103 loc) · 3.4 KB
/
data_source_top_nav_menu.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* 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 } 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 { selectedDataSourceOption } = useContext(DataSourceContext);
return (
<div>
<input
value={
selectedDataSourceOption === undefined
? 'undefined'
: JSON.stringify(selectedDataSourceOption)
}
aria-label="selectedDataSourceOption"
onChange={() => {}}
/>
</div>
);
};
const renderResult = render(
<>
<DataSourceTopNavMenu
notifications={coreStart.notifications}
savedObjects={coreStart.savedObjects}
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 management not defined', () => {
setup({
dataSourceManagement: undefined,
});
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('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' })
);
});
});
});