Skip to content

Commit

Permalink
add more unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
  • Loading branch information
zhyuanqi committed Mar 19, 2024
1 parent 68eb710 commit 898ecea
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,12 @@ describe('Datasource Management: Edit Datasource Form', () => {
expect(mockFn).toHaveBeenCalled();
});

test('should set as default datasource confirmation from header', () => {
// @ts-ignore
component.find('Header').prop('onClickSetDefault')();
expect(mockFn).toHaveBeenCalled();
});

/* Save Changes */
test('should update the form with NoAuth on click save changes', async () => {
await new Promise((resolve) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { act } from 'react-dom/test-utils';
const headerTitleIdentifier = '[data-test-subj="editDataSourceTitle"]';
const deleteIconIdentifier = '[data-test-subj="editDatasourceDeleteIcon"]';
const confirmModalIdentifier = '[data-test-subj="editDatasourceDeleteConfirmModal"]';
const setDefaultButtonIdentifier = '[data-test-subj="editSetDefaultDataSource"]';

describe('Datasource Management: Edit Datasource Header', () => {
const mockedContext = mockManagementPlugin.createDataSourceManagementContext();
Expand All @@ -31,6 +32,8 @@ describe('Datasource Management: Edit Datasource Header', () => {
onClickDeleteIcon={mockFn}
onClickTestConnection={mockFn}
dataSourceName={dataSourceName}
onClickSetDefault={mockFn}
isDefault={false}
/>
),
{
Expand Down Expand Up @@ -82,6 +85,8 @@ describe('Datasource Management: Edit Datasource Header', () => {
onClickDeleteIcon={mockFn}
onClickTestConnection={mockFn}
dataSourceName={dataSourceName}
onClickSetDefault={mockFn}
isDefault={false}
/>
),
{
Expand All @@ -97,4 +102,76 @@ describe('Datasource Management: Edit Datasource Header', () => {
expect(component.find(deleteIconIdentifier).exists()).toBe(false);
});
});
});
describe('should render default icon as "Set as default" when isDefaultDataSourceState is false', () => {
const onClickSetDefault = jest.fn();
const isDefaultDataSourceState = false;
beforeEach(() => {
component = mount(
wrapWithIntl(
<Header
isFormValid={true}
showDeleteIcon={true}
onClickDeleteIcon={mockFn}
onClickTestConnection={mockFn}
dataSourceName={dataSourceName}
onClickSetDefault={onClickSetDefault}
isDefault={isDefaultDataSourceState}
/>
),
{
wrappingComponent: OpenSearchDashboardsContextProvider,
wrappingComponentProps: {
services: mockedContext,
},
}
);
});

test('should render normally', () => {
expect(component.find(setDefaultButtonIdentifier).exists()).toBe(true);
});
test('default button shoould show as "Set as default" and should be clickable', () => {
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Set as default');
expect(component.find(setDefaultButtonIdentifier).first().prop('disabled')).toBe(false);
expect(component.find(setDefaultButtonIdentifier).first().prop('iconType')).toBe('starEmpty');

Check failure on line 137 in src/plugins/data_source_management/public/components/edit_data_source/components/header/header.test.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Delete `····`
component.find(setDefaultButtonIdentifier).first().simulate('click');
expect(onClickSetDefault).toHaveBeenCalled();
});
});
describe('should render default icon as "Set as default" when isDefaultDataSourceState is true', () => {
const onClickSetDefault = jest.fn();
const isDefaultDataSourceState = true;
beforeEach(() => {
component = mount(
wrapWithIntl(
<Header
isFormValid={true}
showDeleteIcon={true}
onClickDeleteIcon={mockFn}
onClickTestConnection={mockFn}
dataSourceName={dataSourceName}
onClickSetDefault={onClickSetDefault}
isDefault={isDefaultDataSourceState}
/>
),
{
wrappingComponent: OpenSearchDashboardsContextProvider,
wrappingComponentProps: {
services: mockedContext,
},
}
);
});

test('should render normally', () => {
expect(component.find(setDefaultButtonIdentifier).exists()).toBe(true);
});
test('default button shoould show as "Set as default" and should be clickable', () => {
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Default');
expect(component.find(setDefaultButtonIdentifier).first().prop('disabled')).toBe(true);
expect(component.find(setDefaultButtonIdentifier).first().prop('iconType')).toBe('starFilled');

Check failure on line 173 in src/plugins/data_source_management/public/components/edit_data_source/components/header/header.test.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Replace `'starFilled'` with `⏎········'starFilled'⏎······`
});
});
});

Check failure on line 176 in src/plugins/data_source_management/public/components/edit_data_source/components/header/header.test.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Delete `·⏎`

Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const Header = ({
disabled={isDefaultDataSourceState}
iconType={isDefaultDataSourceState ? 'starFilled' : 'starEmpty'}
aria-label={setDefaultAriaLabel}
data-test-subj="datasource-edit-setDefaultDataSource"
data-test-subj="editSetDefaultDataSource"
>
{isDefaultDataSourceState ? 'Default' : 'Set as default'}
</EuiButtonEmpty>
Expand Down

0 comments on commit 898ecea

Please sign in to comment.