From eabb00d3062fe040d7b4a076091cdb8ce3a75aa4 Mon Sep 17 00:00:00 2001 From: tygao Date: Fri, 22 Mar 2024 19:31:23 +0800 Subject: [PATCH] update function name and modal Signed-off-by: tygao --- src/plugins/workspace/common/constants.ts | 1 + .../delete_workspace_modal.test.tsx | 44 +++---------------- .../delete_workspace_modal.tsx | 17 +++---- .../public/components/utils/workspace.test.ts | 14 ++++-- .../public/components/utils/workspace.ts | 2 +- .../components/workspace_list/index.test.tsx | 4 +- .../components/workspace_list/index.tsx | 5 +-- 7 files changed, 26 insertions(+), 61 deletions(-) diff --git a/src/plugins/workspace/common/constants.ts b/src/plugins/workspace/common/constants.ts index d2da08acb52d..8d356ac9cbed 100644 --- a/src/plugins/workspace/common/constants.ts +++ b/src/plugins/workspace/common/constants.ts @@ -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 = diff --git a/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.test.tsx b/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.test.tsx index d2ba2e7876ef..0304aa238ada 100644 --- a/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.test.tsx +++ b/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.test.tsx @@ -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(); @@ -58,8 +58,9 @@ 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: { @@ -67,6 +68,7 @@ describe('DeleteWorkspaceModal', () => { name: 'test', }, onClose: onCloseFn, + onDeleteSuccess: onDeleteSuccessFn, }; const deleteFn = jest.fn().mockReturnValue({ success: true, @@ -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(); }); }); diff --git a/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.tsx b/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.tsx index 4273134805a8..157c2ca8570a 100644 --- a/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.tsx +++ b/src/plugins/workspace/public/components/delete_workspace_modal/delete_workspace_modal.tsx @@ -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 () => { @@ -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({ diff --git a/src/plugins/workspace/public/components/utils/workspace.test.ts b/src/plugins/workspace/public/components/utils/workspace.test.ts index 7b0e93c739c7..926455feed34 100644 --- a/src/plugins/workspace/public/components/utils/workspace.test.ts +++ b/src/plugins/workspace/public/components/utils/workspace.test.ts @@ -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'); @@ -48,7 +48,7 @@ describe('workspace utils', () => { }); }); - describe('updateWorkspace', () => { + describe('navigateToWorkspaceUpdatePage', () => { it('should redirect if newUrl is returned', () => { Object.defineProperty(window, 'location', { value: { @@ -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'); }); @@ -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(); }); }); diff --git a/src/plugins/workspace/public/components/utils/workspace.ts b/src/plugins/workspace/public/components/utils/workspace.ts index fb47ca316cbe..63ed5953dbfa 100644 --- a/src/plugins/workspace/public/components/utils/workspace.ts +++ b/src/plugins/workspace/public/components/utils/workspace.ts @@ -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, diff --git a/src/plugins/workspace/public/components/workspace_list/index.test.tsx b/src/plugins/workspace/public/components/workspace_list/index.test.tsx index 1719bfe7a109..d75ddf0d513f 100644 --- a/src/plugins/workspace/public/components/workspace_list/index.test.tsx +++ b/src/plugins/workspace/public/components/workspace_list/index.test.tsx @@ -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'; @@ -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 () => { diff --git a/src/plugins/workspace/public/components/workspace_list/index.tsx b/src/plugins/workspace/public/components/workspace_list/index.tsx index bc92a01f8f58..b22a0fdb99fd 100644 --- a/src/plugins/workspace/public/components/workspace_list/index.tsx +++ b/src/plugins/workspace/public/components/workspace_list/index.tsx @@ -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'; @@ -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] @@ -213,7 +213,6 @@ export const WorkspaceList = () => { setDeletedWorkspace(null)} - returnToHome={false} /> )}