Skip to content

Commit

Permalink
update function name and modal
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <[email protected]>
  • Loading branch information
raintygao committed Mar 22, 2024
1 parent 6e68a53 commit ada9f5b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/plugins/workspace/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export const WORKSPACE_FATAL_ERROR_APP_ID = 'workspace_fatal_error';
export const WORKSPACE_CREATE_APP_ID = 'workspace_create';
export const WORKSPACE_LIST_APP_ID = 'workspace_list';
export const WORKSPACE_UPDATE_APP_ID = 'workspace_update';
export const WORKSPACE_OVERVIEW_APP_ID = 'workspace_overview';
export const WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace';
export const WORKSPACE_CONFLICT_CONTROL_SAVED_OBJECTS_CLIENT_WRAPPER_ID =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { OpenSearchDashboardsContextProvider } from '../../../../../plugins/open
const defaultProps: DeleteWorkspaceModalProps = {
onClose: jest.fn(),
selectedWorkspace: null,
returnToHome: true,
onDeleteSuccess: jest.fn(),
};

const coreStartMock = coreMock.createStart();
Expand Down Expand Up @@ -58,15 +58,17 @@ describe('DeleteWorkspaceModal', () => {
expect(onClose).toHaveBeenCalledTimes(1);
});

it('should be able to delete workspace and navigate successfully', async () => {
it('should be able to delete workspace and emit onDeleteSuccess', async () => {
const onCloseFn = jest.fn();
const onDeleteSuccessFn = jest.fn();
const newProps = {
...defaultProps,
selectedWorkspace: {
id: 'test',
name: 'test',
},
onClose: onCloseFn,
onDeleteSuccess: onDeleteSuccessFn,
};
const deleteFn = jest.fn().mockReturnValue({
success: true,
Expand All @@ -93,43 +95,7 @@ describe('DeleteWorkspaceModal', () => {
await waitFor(() => {
expect(coreStartMock.notifications.toasts.addSuccess).toHaveBeenCalled();
expect(onCloseFn).toHaveBeenCalled();
expect(coreStartMock.application.navigateToUrl).toHaveBeenCalled();
});
});

it('should not navigate when successfully if returnToHome is false', async () => {
const newProps = {
...defaultProps,
selectedWorkspace: {
id: 'test',
name: 'test',
},
returnToHome: false,
};
const deleteFn = jest.fn().mockReturnValue({
success: true,
});
const newServices = {
...coreStartMock,
workspaceClient: {
...workspaceClientMock,
delete: deleteFn,
},
};
const { getByTestId, findByTestId } = render(
getWrapWorkspaceDeleteModalInContext(newProps, newServices)
);
await findByTestId('delete-workspace-modal-input');
const input = getByTestId('delete-workspace-modal-input');
fireEvent.change(input, {
target: { value: 'delete' },
});
const confirmButton = getByTestId('delete-workspace-modal-confirm');
fireEvent.click(confirmButton);
expect(deleteFn).toHaveBeenCalledWith('test');
await waitFor(() => {
expect(coreStartMock.notifications.toasts.addSuccess).toHaveBeenCalled();
expect(coreStartMock.application.navigateToUrl).not.toHaveBeenCalled();
expect(onDeleteSuccessFn).toHaveBeenCalled();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { WorkspaceClient } from '../../workspace_client';
export interface DeleteWorkspaceModalProps {
onClose: () => void;
selectedWorkspace?: WorkspaceAttribute | null;
returnToHome: boolean;
onDeleteSuccess?: () => void;
}

export function DeleteWorkspaceModal(props: DeleteWorkspaceModalProps) {
const [value, setValue] = useState('');
const { onClose, selectedWorkspace, returnToHome } = props;
const { onClose, selectedWorkspace, onDeleteSuccess } = props;
const {
services: { application, notifications, http, workspaceClient },
services: { notifications, workspaceClient },
} = useOpenSearchDashboards<{ workspaceClient: WorkspaceClient }>();

const deleteWorkspace = async () => {
Expand All @@ -55,15 +55,8 @@ export function DeleteWorkspaceModal(props: DeleteWorkspaceModalProps) {
}),
});
onClose();
if (http && application && returnToHome) {
const homeUrl = application.getUrlForApp('home', {
path: '/',
absolute: false,
});
const targetUrl = http.basePath.prepend(http.basePath.remove(homeUrl), {
withoutWorkspace: true,
});
await application.navigateToUrl(targetUrl);
if (onDeleteSuccess) {
onDeleteSuccess();
}
} else {
notifications?.toasts.addDanger({
Expand Down
14 changes: 10 additions & 4 deletions src/plugins/workspace/public/components/utils/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { switchWorkspace, updateWorkspace } from './workspace';
import { switchWorkspace, navigateToWorkspaceUpdatePage } from './workspace';
import { formatUrlWithWorkspaceId } from '../../../../../core/public/utils';
jest.mock('../../../../../core/public/utils');

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('workspace utils', () => {
});
});

describe('updateWorkspace', () => {
describe('navigateToWorkspaceUpdatePage', () => {
it('should redirect if newUrl is returned', () => {
Object.defineProperty(window, 'location', {
value: {
Expand All @@ -58,7 +58,10 @@ describe('workspace utils', () => {
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => 'new_url');
updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
navigateToWorkspaceUpdatePage(
{ application: coreStartMock.application, http: coreStartMock.http },
''
);
expect(mockNavigateToUrl).toHaveBeenCalledWith('new_url');
});

Expand All @@ -71,7 +74,10 @@ describe('workspace utils', () => {
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => '');
updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
navigateToWorkspaceUpdatePage(
{ application: coreStartMock.application, http: coreStartMock.http },
''
);
expect(mockNavigateToUrl).not.toBeCalled();
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/workspace/public/components/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const switchWorkspace = ({ application, http }: Core, id: string) => {
}
};

export const updateWorkspace = ({ application, http }: Core, id: string) => {
export const navigateToWorkspaceUpdatePage = ({ application, http }: Core, id: string) => {
const newUrl = formatUrlWithWorkspaceId(
application.getUrlForApp(WORKSPACE_UPDATE_APP_ID, {
absolute: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { WorkspaceList } from './index';
import { coreMock } from '../../../../../core/public/mocks';
import { render, fireEvent, screen } from '@testing-library/react';
import { I18nProvider } from '@osd/i18n/react';
import { switchWorkspace, updateWorkspace } from '../utils/workspace';
import { switchWorkspace, navigateToWorkspaceUpdatePage } from '../utils/workspace';

import { of } from 'rxjs';

Expand Down Expand Up @@ -93,7 +93,7 @@ describe('WorkspaceList', () => {
const { getAllByTestId } = render(getWrapWorkspaceListInContext());
const editIcon = getAllByTestId('workspace-list-edit-icon')[0];
fireEvent.click(editIcon);
expect(updateWorkspace).toBeCalled();
expect(navigateToWorkspaceUpdatePage).toBeCalled();
});

it('should be able to call delete modal after clicking delete button', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { i18n } from '@osd/i18n';
import { debounce } from '../../../../../core/public';
import { WorkspaceAttribute } from '../../../../../core/public';
import { useOpenSearchDashboards } from '../../../../../plugins/opensearch_dashboards_react/public';
import { switchWorkspace, updateWorkspace } from '../utils/workspace';
import { switchWorkspace, navigateToWorkspaceUpdatePage } from '../utils/workspace';

import { WORKSPACE_CREATE_APP_ID } from '../../../common/constants';

Expand Down Expand Up @@ -60,7 +60,7 @@ export const WorkspaceList = () => {
const handleUpdateWorkspace = useCallback(
(id: string) => {
if (application && http) {
updateWorkspace({ application, http }, id);
navigateToWorkspaceUpdatePage({ application, http }, id);
}
},
[application, http]
Expand Down Expand Up @@ -213,7 +213,6 @@ export const WorkspaceList = () => {
<DeleteWorkspaceModal
selectedWorkspace={deletedWorkspace}
onClose={() => setDeletedWorkspace(null)}
returnToHome={false}
/>
)}
</EuiPage>
Expand Down

0 comments on commit ada9f5b

Please sign in to comment.