Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Wei <[email protected]>
  • Loading branch information
mengweieric committed Jun 22, 2023
1 parent fb84961 commit 40f252f
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
5 changes: 4 additions & 1 deletion public/plugins/datasources/datasource_picker/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const SOURCE_PICKER_TITLE = 'available sources';
export const SOURCE_PICKER_BTN_DEFAULT_TEXT = 'Select a datasource';
export const SOURCE_PICKER_BTN_DEFAULT_WIDTH = '300px';
export const SOURCE_PICKER_PANEL_DEFAULT_WIDTH = '500px';
export const SOURCE_PICKER_PANEL_TEST_SUBJ = 'multiSelectableDatasourcePanel';
export const SOURCE_PICKER_PANEL_SEARCH_TEST_SUBJ = 'selectableDatasourcePanelSearch';
export const SOURCE_PICKER_PANEL_TEST_SUBJ = 'selectableDatasourcePanel';
export const SOURCE_PICKER_FOOTER_CANCEL_BTN_TEXT = 'Cancel';
export const SOURCE_PICKER_FOOTER_SELECT_BTN_TEXT = 'Select';
export const SOURCE_PICKER_FOOTER_SELECT_BTN_TEST_SUBJ = 'datasourcePickerSelect';
export const SOURCE_PICKER_BTN_TEST_SUBJ = 'sourcePickerButton';
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { configure, shallow, ShallowWrapper } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
// @ts-ignore
import { DatasourcePicker } from './datasource_picker';
import { IDatasourceListOption, IDatasourcePickerProps } from '../types';
import { SOURCE_PICKER_FOOTER_SELECT_BTN_TEST_SUBJ } from './constants';
import { OuiSelectable } from '@elastic/eui';

configure({ adapter: new Adapter() });

const dsOptions = [
{ title: '[cluster1].opensearch-dashboards-sample-data-flights' },
{ title: '[cluster1].opensearch-dashboards-sample-data-log' },
{ title: '[cluster2].opensearch-dashboards-sample-data-flights' },
{ title: '[cluster2].opensearch-dashboards-sample-data-log' },
{ title: 'opensearch-dashboards-sample-data-log' },
{ title: 'Spark_S3_sales' },
{ title: 'Spark_S3_Internal' },
] as IDatasourceListOption[];

const props = {
datasourceList: [...dsOptions],
onSelect: jest.fn(),
} as IDatasourcePickerProps;

const getSourcePickerList = (instance: ShallowWrapper) => {
return instance.find(OuiSelectable);
};

const getSourcePickerOptions = (instance: ShallowWrapper) => {
return getSourcePickerList(instance).prop('options');
};

const selectSourcePickerOption = (instance: ShallowWrapper, selectedLabel: string) => {
const options: Array<{ label: string; checked?: 'on' | 'off' }> = getSourcePickerOptions(
instance
).map((option: any) =>
option.label === selectedLabel
? { ...option, checked: 'on' }
: { ...option, checked: undefined }
);
return getSourcePickerList(instance).prop('onChange')!(options);
};

const clickSelect = (instance: ShallowWrapper) => {
instance
.find(`[data-test-subj="${SOURCE_PICKER_FOOTER_SELECT_BTN_TEST_SUBJ}"]`)
.simulate('click');
};

describe('Select datasource', () => {
it('Should allow selecting a datasource', () => {
const instance = shallow(<DatasourcePicker {...props} />);
selectSourcePickerOption(instance, 'Spark_S3_sales');
clickSelect(instance);
expect(props.onSelect).toHaveBeenCalledWith({ title: 'Spark_S3_sales' });
});
});

describe('Render datasource picker', () => {
it('Should render component and match sanpshots', () => {
expect(shallow(<DatasourcePicker {...props} />)).toMatchInlineSnapshot(`
<OuiPopover
anchorPosition="downLeft"
button={
<OuiButton
data-test-subj="sourcePickerButton"
iconSide="right"
iconType="arrowDown"
onClick={[Function]}
size="s"
style={
Object {
"width": "300px",
}
}
>
Select a datasource
</OuiButton>
}
closePopover={[Function]}
display="inlineBlock"
hasArrow={true}
id="contextMenuExample"
isOpen={false}
ownFocus={true}
panelPaddingSize="none"
>
<OuiPopoverTitle>
available sources
</OuiPopoverTitle>
<OuiSelectable
aria-label="Multi-selectable source panel"
data-test-subj="selectableDatasourcePanel"
isPreFiltered={false}
listProps={
Object {
"bordered": true,
}
}
onChange={[Function]}
options={
Array [
Object {
"checked": undefined,
"label": "[cluster1].opensearch-dashboards-sample-data-flights",
},
Object {
"checked": undefined,
"label": "[cluster1].opensearch-dashboards-sample-data-log",
},
Object {
"checked": undefined,
"label": "[cluster2].opensearch-dashboards-sample-data-flights",
},
Object {
"checked": undefined,
"label": "[cluster2].opensearch-dashboards-sample-data-log",
},
Object {
"checked": undefined,
"label": "opensearch-dashboards-sample-data-log",
},
Object {
"checked": undefined,
"label": "Spark_S3_sales",
},
Object {
"checked": undefined,
"label": "Spark_S3_Internal",
},
]
}
searchProps={
Object {
"data-test-subj": "selectableDatasourcePanelSearch",
}
}
searchable={true}
singleSelection={true}
style={
Object {
"width": "500px",
}
}
>
<Component />
</OuiSelectable>
<OuiPopoverFooter
paddingSize="s"
>
<OuiFlexGroup>
<OuiFlexItem
grow={3}
/>
<OuiFlexItem
grow={false}
>
<OuiButton
onClick={[Function]}
size="s"
>
Cancel
</OuiButton>
</OuiFlexItem>
<OuiFlexItem
grow={false}
>
<OuiButton
data-test-subj="datasourcePickerSelect"
fill={true}
onClick={[Function]}
size="s"
>
Select
</OuiButton>
</OuiFlexItem>
</OuiFlexGroup>
</OuiPopoverFooter>
</OuiPopover>
`);
});
});
13 changes: 11 additions & 2 deletions public/plugins/datasources/datasource_picker/datasource_picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
SOURCE_PICKER_PANEL_TEST_SUBJ,
SOURCE_PICKER_FOOTER_CANCEL_BTN_TEXT,
SOURCE_PICKER_FOOTER_SELECT_BTN_TEXT,
SOURCE_PICKER_BTN_TEST_SUBJ,
SOURCE_PICKER_PANEL_SEARCH_TEST_SUBJ,
} from './constants';

export const DatasourcePicker = ({
Expand All @@ -49,6 +51,7 @@ export const DatasourcePicker = ({
iconType="arrowDown"
iconSide="right"
onClick={() => setIsPopoverOpen((isOpen) => !isOpen)}
data-test-subj={SOURCE_PICKER_BTN_TEST_SUBJ}
>
{selectedSource?.title || SOURCE_PICKER_BTN_DEFAULT_TEXT}
</OuiButton>
Expand Down Expand Up @@ -78,7 +81,7 @@ export const DatasourcePicker = ({
aria-label="Multi-selectable source panel"
searchable
searchProps={{
'data-test-subj': SOURCE_PICKER_PANEL_TEST_SUBJ,
'data-test-subj': SOURCE_PICKER_PANEL_SEARCH_TEST_SUBJ,
}}
options={datasourceList.map((dsItem: IDatasourceListOption) => {
return {
Expand All @@ -96,6 +99,7 @@ export const DatasourcePicker = ({
});
}}
singleSelection={true}
data-test-subj={SOURCE_PICKER_PANEL_TEST_SUBJ}
>
{(list, search) => (
<>
Expand All @@ -113,7 +117,12 @@ export const DatasourcePicker = ({
</OuiButton>
</OuiFlexItem>
<OuiFlexItem grow={false}>
<OuiButton size="s" fill onClick={onSelectSource}>
<OuiButton
size="s"
fill
onClick={onSelectSource}
data-test-subj="datasourcePickerSelect"
>
{SOURCE_PICKER_FOOTER_SELECT_BTN_TEXT}
</OuiButton>
</OuiFlexItem>
Expand Down

0 comments on commit 40f252f

Please sign in to comment.