Skip to content

Commit

Permalink
Merge branch 'main' into list-padding
Browse files Browse the repository at this point in the history
  • Loading branch information
yujin-emma authored May 16, 2024
2 parents b06c88d + f728b5a commit 701787a
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6755.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Add test for toast button and validation form ([#6755](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6755))
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { shallow } from 'enzyme';
import { getManageDataSourceButton } from './manage_data_source_button';
import { coreMock } from '../../../../../core/public/mocks';
import { DSM_APP_ID } from '../../plugin';
import { render } from '@testing-library/react';

describe('ManageDataSourceButton', () => {
const applicationMock = coreMock.createStart().application;

it('renders without crashing', () => {
const wrapper = render(getManageDataSourceButton());
expect(wrapper).toBeTruthy();
});

it('renders a button with correct label', () => {
const { getByTestId } = render(getManageDataSourceButton(applicationMock));
const container = getByTestId('manageDataSourceButtonContainer');
expect(container).toBeInTheDocument();
expect(container).toHaveTextContent('Manage data sources');
});

it('navigates to management app on button click', () => {
const { getByTestId } = render(getManageDataSourceButton(applicationMock));
const button = getByTestId('manageDataSourceButton');
button.click();
expect(applicationMock.navigateToApp).toHaveBeenCalledTimes(1);

expect(applicationMock.navigateToApp).toHaveBeenCalledWith('management', {
path: `opensearch-dashboards/${DSM_APP_ID}`, // Assuming DSM_APP_ID is replaced with a value
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import { DSM_APP_ID } from '../../plugin';
export const getManageDataSourceButton = (application?: ApplicationStart) => {
return (
<>
<EuiFlexGroup justifyContent="flexEnd" gutterSize="s">
<EuiFlexGroup
data-test-subj="manageDataSourceButtonContainer"
justifyContent="flexEnd"
gutterSize="s"
>
<EuiFlexItem grow={false}>
<EuiButton
data-test-subj="manageDataSourceButton"
size="s"
onClick={() =>
application?.navigateToApp('management', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { render, fireEvent } from '@testing-library/react';
import { getReloadButton } from './reload_button';

describe('getReloadButton', () => {
it('renders button with correct label', () => {
const { getByText } = render(getReloadButton());
expect(getByText('Refresh the page')).toBeInTheDocument();
});

it('calls window.location.reload() on button click', () => {
const reloadMock = jest.fn();
Object.defineProperty(window, 'location', {
value: { reload: reloadMock },
writable: true,
});

const { getByText } = render(getReloadButton());
fireEvent.click(getByText('Refresh the page'));
expect(reloadMock).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import { AuthType } from '../../types';
import { CreateDataSourceState } from '../create_data_source_wizard/components/create_form/create_data_source_form';
import { EditDataSourceState } from '../edit_data_source/components/edit_form/edit_data_source_form';
import { defaultValidation, performDataSourceFormValidation } from './datasource_form_validation';
import { mockDataSourceAttributesWithAuth } from '../../mocks';
import {
mockDataSourceAttributesWithAuth,
mockDataSourceAttributesWithSigV4Auth,
} from '../../mocks';
import { AuthenticationMethod, AuthenticationMethodRegistry } from '../../auth_registry';

describe('DataSourceManagement: Form Validation', () => {
describe('validate create/edit datasource', () => {
describe('validate create/edit datasource for Username and Password auth type', () => {
let authenticationMethodRegistry = new AuthenticationMethodRegistry();
let form: CreateDataSourceState | EditDataSourceState = {
formErrorsByField: { ...defaultValidation },
Expand Down Expand Up @@ -117,4 +120,113 @@ describe('DataSourceManagement: Form Validation', () => {
expect(result).toBe(true);
});
});

describe('validate create/edit datasource for SigV4 auth type', () => {
let authenticationMethodRegistry = new AuthenticationMethodRegistry();
let form: CreateDataSourceState | EditDataSourceState = {
formErrorsByField: { ...defaultValidation },
title: '',
description: '',
endpoint: '',
auth: {
type: AuthType.SigV4,
credentials: {
accesskey: 'test123',
secretKey: 'test123',
service: 'es',
region: 'us-east-1',
},
},
};
test('should fail validation when title is empty', () => {
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(false);
});
test('should fail validation on duplicate title', () => {
form.title = 'test';
const result = performDataSourceFormValidation(
form,
['oldTitle', 'test'],
'oldTitle',
authenticationMethodRegistry
);
expect(result).toBe(false);
});
test('should fail validation when title is longer than 32 characters', () => {
form.title = 'test'.repeat(10);
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(false);
});
test('should fail validation when endpoint is not valid', () => {
form.endpoint = mockDataSourceAttributesWithSigV4Auth.endpoint;
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(false);
});
test('should fail validation when accesskey is empty', () => {
form.auth.credentials!.accessKey = 'test';
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(false);
});
test('should fail validation when secrectKey is empty', () => {
form.auth.credentials!.accessKey = 'test';
form.auth.credentials!.secretKey = '';
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(false);
});
test('should NOT fail validation on empty accesskey/secretKey when No Auth is selected', () => {
form.auth.type = AuthType.NoAuth;
form.title = 'test';
form.endpoint = mockDataSourceAttributesWithSigV4Auth.endpoint;
const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry);
expect(result).toBe(true);
});
test('should NOT fail validation on all fields', () => {
form = { ...form, ...mockDataSourceAttributesWithSigV4Auth };
const result = performDataSourceFormValidation(
form,
[mockDataSourceAttributesWithSigV4Auth.title],
mockDataSourceAttributesWithSigV4Auth.title,
authenticationMethodRegistry
);
expect(result).toBe(true);
});
test('should NOT fail validation when registered auth type is selected and related credential field not empty', () => {
authenticationMethodRegistry = new AuthenticationMethodRegistry();
const authMethodToBeTested = {
name: 'Some Auth Type',
credentialSourceOption: {
value: 'Some Auth Type',
inputDisplay: 'some input',
},
credentialForm: jest.fn(),
credentialFormField: {
userNameRegistered: 'some filled in userName from registed auth credential form',
passWordRegistered: 'some filled in password from registed auth credential form',
},
} as AuthenticationMethod;

authenticationMethodRegistry.registerAuthenticationMethod(authMethodToBeTested);

const formWithRegisteredAuth: CreateDataSourceState | EditDataSourceState = {
formErrorsByField: { ...defaultValidation },
title: 'test registered auth type',
description: '',
endpoint: 'https://test.com',
auth: {
type: 'Some Auth Type',
credentials: {
userNameRegistered: 'some filled in userName from registed auth credential form',
passWordRegistered: 'some filled in password from registed auth credential form',
},
},
};
const result = performDataSourceFormValidation(
formWithRegisteredAuth,
[],
'',
authenticationMethodRegistry
);
expect(result).toBe(true);
});
});
});
1 change: 1 addition & 0 deletions src/plugins/data_source_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export const mockDataSourceAttributesWithSigV4Auth = {
accessKey: 'test123',
secretKey: 'test123',
region: 'us-east-1',
service: 'es',
},
},
};
Expand Down

0 comments on commit 701787a

Please sign in to comment.