From 17fe05e4673e3652e181730828b27fbcf43c9206 Mon Sep 17 00:00:00 2001 From: tygao Date: Fri, 8 Mar 2024 18:39:18 +0800 Subject: [PATCH] rename and update test Signed-off-by: tygao --- .../delete_workspace_modal.test.tsx | 30 ++++++++----------- .../public/components/utils/workspace.test.ts | 15 ++++++---- .../public/components/utils/workspace.ts | 4 +-- .../components/workspace_list/index.test.tsx | 14 ++++----- 4 files changed, 31 insertions(+), 32 deletions(-) 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 d385ed319741..e51957317c35 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 @@ -6,7 +6,7 @@ import React from 'react'; import { DeleteWorkspaceModal, DeleteWorkspaceModalProps } from './delete_workspace_modal'; import { coreMock } from '../../../../../core/public/mocks'; -import { render, fireEvent, waitFor, cleanup } from '@testing-library/react'; +import { render, fireEvent, waitFor } from '@testing-library/react'; import { workspaceClientMock } from '../../../public/workspace_client.mock'; import { OpenSearchDashboardsContextProvider } from '../../../../../plugins/opensearch_dashboards_react/public'; @@ -18,7 +18,7 @@ const defaultProps: DeleteWorkspaceModalProps = { const coreStartMock = coreMock.createStart(); -function WrapWorkspaceDeleteModalInContext( +function getWrapWorkspaceDeleteModalInContext( props: DeleteWorkspaceModalProps, services = { ...coreStartMock } ) { @@ -28,21 +28,15 @@ function WrapWorkspaceDeleteModalInContext( ); } -beforeEach(() => { - jest.clearAllMocks(); -}); -afterEach(() => { - cleanup(); - jest.clearAllMocks(); -}); + describe('DeleteWorkspaceModal', () => { - beforeEach(() => { + afterEach(() => { jest.clearAllMocks(); }); it('should render normally', () => { const { getByText, baseElement, getByTestId } = render( - WrapWorkspaceDeleteModalInContext(defaultProps) + getWrapWorkspaceDeleteModalInContext(defaultProps) ); expect(getByText('Delete workspace')).toBeInTheDocument(); @@ -57,7 +51,7 @@ describe('DeleteWorkspaceModal', () => { ...defaultProps, onClose, }; - const { getByTestId } = render(WrapWorkspaceDeleteModalInContext(newProps)); + const { getByTestId } = render(getWrapWorkspaceDeleteModalInContext(newProps)); expect(onClose).not.toHaveBeenCalled(); const cancelButton = getByTestId('delete-workspace-modal-cancel-button'); fireEvent.click(cancelButton); @@ -85,7 +79,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); @@ -123,7 +117,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); @@ -155,7 +149,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); @@ -186,7 +180,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); @@ -221,7 +215,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); @@ -253,7 +247,7 @@ describe('DeleteWorkspaceModal', () => { }, }; const { getByTestId, findByTestId } = render( - WrapWorkspaceDeleteModalInContext(newProps, newServices) + getWrapWorkspaceDeleteModalInContext(newProps, newServices) ); await findByTestId('delete-workspace-modal-input'); const input = getByTestId('delete-workspace-modal-input'); diff --git a/src/plugins/workspace/public/components/utils/workspace.test.ts b/src/plugins/workspace/public/components/utils/workspace.test.ts index dd8d7dd28b8b..7b0e93c739c7 100644 --- a/src/plugins/workspace/public/components/utils/workspace.test.ts +++ b/src/plugins/workspace/public/components/utils/workspace.test.ts @@ -10,11 +10,16 @@ jest.mock('../../../../../core/public/utils'); import { coreMock } from '../../../../../core/public/mocks'; const coreStartMock = coreMock.createStart(); +let mockNavigateToUrl = jest.fn(); -window = Object.create(window); const defaultUrl = 'localhost://'; describe('workspace utils', () => { + beforeEach(() => { + mockNavigateToUrl = jest.fn(); + coreStartMock.application.navigateToUrl = mockNavigateToUrl; + }); + describe('switchWorkspace', () => { it('should redirect if newUrl is returned', () => { Object.defineProperty(window, 'location', { @@ -26,7 +31,7 @@ describe('workspace utils', () => { // @ts-ignore formatUrlWithWorkspaceId.mockImplementation(() => 'new_url'); switchWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, ''); - expect(window.location.href).toEqual('new_url'); + expect(mockNavigateToUrl).toHaveBeenCalledWith('new_url'); }); it('should not redirect if newUrl is not returned', () => { @@ -39,7 +44,7 @@ describe('workspace utils', () => { // @ts-ignore formatUrlWithWorkspaceId.mockImplementation(() => ''); switchWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, ''); - expect(window.location.href).toEqual(defaultUrl); + expect(mockNavigateToUrl).not.toBeCalled(); }); }); @@ -54,7 +59,7 @@ describe('workspace utils', () => { // @ts-ignore formatUrlWithWorkspaceId.mockImplementation(() => 'new_url'); updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, ''); - expect(window.location.href).toEqual('new_url'); + expect(mockNavigateToUrl).toHaveBeenCalledWith('new_url'); }); it('should not redirect if newUrl is not returned', () => { @@ -67,7 +72,7 @@ describe('workspace utils', () => { // @ts-ignore formatUrlWithWorkspaceId.mockImplementation(() => ''); updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, ''); - expect(window.location.href).toEqual(defaultUrl); + 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 a0b49c520b01..fb47ca316cbe 100644 --- a/src/plugins/workspace/public/components/utils/workspace.ts +++ b/src/plugins/workspace/public/components/utils/workspace.ts @@ -18,7 +18,7 @@ export const switchWorkspace = ({ application, http }: Core, id: string) => { http.basePath ); if (newUrl) { - window.location.href = newUrl; + application.navigateToUrl(newUrl); } }; @@ -31,6 +31,6 @@ export const updateWorkspace = ({ application, http }: Core, id: string) => { http.basePath ); if (newUrl) { - window.location.href = newUrl; + application.navigateToUrl(newUrl); } }; 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 c3c704ad6bcc..90c7e1a3eda6 100644 --- a/src/plugins/workspace/public/components/workspace_list/index.test.tsx +++ b/src/plugins/workspace/public/components/workspace_list/index.test.tsx @@ -16,7 +16,7 @@ import { OpenSearchDashboardsContextProvider } from '../../../../../plugins/open jest.mock('../utils/workspace'); -function WrapWorkspaceListInContext( +function getWrapWorkspaceListInContext( workspaceList = [ { id: 'id1', name: 'name1' }, { id: 'id2', name: 'name2' }, @@ -48,12 +48,12 @@ describe('WorkspaceList', () => { expect(container).toMatchSnapshot(); }); it('should render data in table based on workspace list data', async () => { - const { getByText } = render(WrapWorkspaceListInContext()); + const { getByText } = render(getWrapWorkspaceListInContext()); expect(getByText('name1')).toBeInTheDocument(); expect(getByText('name2')).toBeInTheDocument(); }); it('should be able to search after input', async () => { - const { getByText, getByRole } = render(WrapWorkspaceListInContext()); + const { getByText, getByRole } = render(getWrapWorkspaceListInContext()); expect(getByText('name1')).toBeInTheDocument(); expect(getByText('name2')).toBeInTheDocument(); const nameInput = getByRole('searchbox'); @@ -69,21 +69,21 @@ describe('WorkspaceList', () => { }); it('should be able to switch workspace after clicking name', async () => { - const { getByText } = render(WrapWorkspaceListInContext()); + const { getByText } = render(getWrapWorkspaceListInContext()); const nameLink = getByText('name1'); fireEvent.click(nameLink); expect(switchWorkspace).toBeCalled(); }); it('should be able to update workspace after clicking name', async () => { - const { getAllByTestId } = render(WrapWorkspaceListInContext()); + const { getAllByTestId } = render(getWrapWorkspaceListInContext()); const editIcon = getAllByTestId('workspace-list-edit-icon')[0]; fireEvent.click(editIcon); expect(updateWorkspace).toBeCalled(); }); it('should be able to call delete modal after clicking delete button', async () => { - const { getAllByTestId } = render(WrapWorkspaceListInContext()); + const { getAllByTestId } = render(getWrapWorkspaceListInContext()); const deleteIcon = getAllByTestId('workspace-list-delete-icon')[0]; fireEvent.click(deleteIcon); expect(screen.getByTestId('delete-workspace-modal-header')).toBeInTheDocument(); @@ -101,7 +101,7 @@ describe('WorkspaceList', () => { { id: 'id5', name: 'name5' }, { id: 'id6', name: 'name6' }, ]; - const { getByTestId, getByText, queryByText } = render(WrapWorkspaceListInContext(list)); + const { getByTestId, getByText, queryByText } = render(getWrapWorkspaceListInContext(list)); expect(getByText('name1')).toBeInTheDocument(); expect(queryByText('name6')).not.toBeInTheDocument(); const paginationButton = getByTestId('pagination-button-next');