Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.x] [Feature] Acceleration Actions Implementation #1563

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { mount, configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { EuiOverlayMask, EuiConfirmModal, EuiFieldText } from '@elastic/eui';
import {
AccelerationActionOverlay,
AccelerationActionOverlayProps,
} from '../manage/accelerations/acceleration_action_overlay';
import { skippingIndexAcceleration } from '../../../../../test/datasources';
import { act } from 'react-dom/test-utils';

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

describe('AccelerationActionOverlay Component Tests', () => {
let props: AccelerationActionOverlayProps;

beforeEach(() => {
props = {
isVisible: true,
actionType: 'delete',
acceleration: skippingIndexAcceleration,
dataSourceName: 'test-datasource',
onCancel: jest.fn(),
onConfirm: jest.fn(),
};
});

it('renders correctly', () => {
const wrapper = mount(<AccelerationActionOverlay {...props} />);
expect(wrapper.find(EuiOverlayMask).exists()).toBe(true);
expect(wrapper.find(EuiConfirmModal).exists()).toBe(true);
expect(wrapper.text()).toContain('Delete acceleration');
});

it('calls onConfirm when confirm button is clicked and confirm is enabled', async () => {
const wrapper = mount(<AccelerationActionOverlay {...props} />);

if (props.actionType === 'vacuum') {
await act(async () => {
const onChange = wrapper.find(EuiFieldText).first().prop('onChange');
if (typeof onChange === 'function') {
onChange({
target: { value: props.acceleration!.indexName },
} as any);

Check warning on line 49 in public/components/datasources/components/__tests__/acceleration_action_overlay.test.tsx

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}
});
wrapper.update();
}
wrapper
.find('button')
.filterWhere((button) => button.text().includes('Delete'))
.simulate('click');
expect(props.onConfirm).toHaveBeenCalled();
});

it('calls onCancel when cancel button is clicked', () => {
const wrapper = mount(<AccelerationActionOverlay {...props} />);

wrapper
.find('button')
.filterWhere((button) => button.text() === 'Cancel')
.simulate('click');

expect(props.onCancel).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { renderHook, act } from '@testing-library/react-hooks';
import { useAccelerationOperation } from '../manage/accelerations/acceleration_operation';
import * as useDirectQueryModule from '../../../../framework/datasources/direct_query_hook';
import * as useToastModule from '../../../common/toast';
import { DirectQueryLoadingStatus } from '../../../../../common/types/explorer';
import { skippingIndexAcceleration } from '../../../../../test/datasources';

jest.mock('../../../../framework/datasources/direct_query_hook', () => ({
useDirectQuery: jest.fn(),
}));

jest.mock('../../../common/toast', () => ({
useToast: jest.fn(),
}));

describe('useAccelerationOperation', () => {
beforeEach(() => {
jest.clearAllMocks();

(useDirectQueryModule.useDirectQuery as jest.Mock).mockReturnValue({
startLoading: jest.fn(),
stopLoading: jest.fn(),
loadStatus: DirectQueryLoadingStatus.INITIAL,
});

(useToastModule.useToast as jest.Mock).mockReturnValue({
setToast: jest.fn(),
});
});

it('performs acceleration operation and handles success', async () => {
(useDirectQueryModule.useDirectQuery as jest.Mock).mockReturnValue({
startLoading: jest.fn(),
stopLoading: jest.fn(),
loadStatus: DirectQueryLoadingStatus.SUCCESS,
});

const { result } = renderHook(() => useAccelerationOperation('test-datasource'));

act(() => {
result.current.performOperation(skippingIndexAcceleration, 'delete');
});

expect((useDirectQueryModule.useDirectQuery as jest.Mock).mock.calls.length).toBeGreaterThan(0);
expect((useToastModule.useToast as jest.Mock).mock.calls.length).toBeGreaterThan(0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ describe('AccelerationTable Component', () => {
});
wrapper!.update();

expect(wrapper!.text()).toContain(accelerationCache.lastUpdated);
const expectedLocalizedTime = accelerationCache.lastUpdated
? new Date(accelerationCache.lastUpdated).toLocaleString()
: '';

expect(wrapper!.text()).toContain(expectedLocalizedTime);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('AssociatedObjectsDetailsFlyout Integration Tests', () => {
wrapper.update();
});

const accName = getAccelerationName(mockTableDetail.accelerations[0], 'flint_s3');
const accName = getAccelerationName(mockTableDetail.accelerations[0]);
const accLink = wrapper
.find('EuiLink')
.findWhere((node) => node.text() === accName)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState } from 'react';
import { EuiOverlayMask, EuiConfirmModal, EuiFormRow, EuiFieldText } from '@elastic/eui';
import { CachedAcceleration } from '../../../../../../common/types/data_connections';
import {
ACC_DELETE_MSG,
ACC_VACUUM_MSG,
ACC_SYNC_MSG,
AccelerationActionType,
getAccelerationName,
getAccelerationFullPath,
} from './utils/acceleration_utils';

export interface AccelerationActionOverlayProps {
isVisible: boolean;
actionType: AccelerationActionType;
acceleration: CachedAcceleration | null;
dataSourceName: string;
onCancel: () => void;
onConfirm: () => void;
}

export const AccelerationActionOverlay: React.FC<AccelerationActionOverlayProps> = ({
isVisible,
actionType,
acceleration,
dataSourceName,
onCancel,
onConfirm,
}) => {
const [confirmationInput, setConfirmationInput] = useState('');

if (!isVisible || !acceleration) {
return null;

Check warning on line 38 in public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx#L38

Added line #L38 was not covered by tests
}

const displayIndexName = getAccelerationName(acceleration);
const displayFullPath = getAccelerationFullPath(acceleration, dataSourceName);

let title = '';
let description = '';
let confirmButtonText = 'Confirm';
let confirmEnabled = true;

switch (actionType) {
case 'vacuum':
title = `Vacuum acceleration ${displayIndexName} on ${displayFullPath}?`;
description = ACC_VACUUM_MSG;
confirmButtonText = 'Vacuum';
confirmEnabled = confirmationInput === displayIndexName;
break;

Check warning on line 55 in public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx#L50-L55

Added lines #L50 - L55 were not covered by tests
case 'delete':
title = `Delete acceleration ${displayIndexName} on ${displayFullPath}?`;
description = ACC_DELETE_MSG;
confirmButtonText = 'Delete';
break;
case 'sync':
title = 'Manual sync data?';
description = ACC_SYNC_MSG;
confirmButtonText = 'Sync';
break;

Check warning on line 65 in public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx#L61-L65

Added lines #L61 - L65 were not covered by tests
}

return (
<EuiOverlayMask>
<EuiConfirmModal
title={title}
onCancel={onCancel}
onConfirm={() => onConfirm()}
cancelButtonText="Cancel"
confirmButtonText={confirmButtonText}
buttonColor="danger"
defaultFocusedButton="confirm"
confirmButtonDisabled={!confirmEnabled}
>
<p>{description}</p>
{actionType === 'vacuum' && (
<EuiFormRow label={`To confirm, type ${displayIndexName}`}>

Check warning on line 82 in public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx#L82

Added line #L82 was not covered by tests
<EuiFieldText
name="confirmationInput"
value={confirmationInput}
onChange={(e) => setConfirmationInput(e.target.value)}

Check warning on line 86 in public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx

View check run for this annotation

Codecov / codecov/patch

public/components/datasources/components/manage/accelerations/acceleration_action_overlay.tsx#L86

Added line #L86 was not covered by tests
/>
</EuiFormRow>
)}
</EuiConfirmModal>
</EuiOverlayMask>
);
};
Loading
Loading