Skip to content

Commit

Permalink
rename and update test
Browse files Browse the repository at this point in the history
Signed-off-by: tygao <tygao@amazon.com>
  • Loading branch information
raintygao committed Mar 8, 2024
1 parent 86717c1 commit 17fe05e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -18,7 +18,7 @@ const defaultProps: DeleteWorkspaceModalProps = {

const coreStartMock = coreMock.createStart();

function WrapWorkspaceDeleteModalInContext(
function getWrapWorkspaceDeleteModalInContext(
props: DeleteWorkspaceModalProps,
services = { ...coreStartMock }
) {
Expand All @@ -28,21 +28,15 @@ function WrapWorkspaceDeleteModalInContext(
</OpenSearchDashboardsContextProvider>
);
}
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();
Expand All @@ -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);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/workspace/public/components/utils/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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', () => {
Expand All @@ -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();
});
});

Expand All @@ -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', () => {
Expand All @@ -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();
});
});
});
4 changes: 2 additions & 2 deletions src/plugins/workspace/public/components/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const switchWorkspace = ({ application, http }: Core, id: string) => {
http.basePath
);
if (newUrl) {
window.location.href = newUrl;
application.navigateToUrl(newUrl);
}
};

Expand All @@ -31,6 +31,6 @@ export const updateWorkspace = ({ application, http }: Core, id: string) => {
http.basePath
);
if (newUrl) {
window.location.href = newUrl;
application.navigateToUrl(newUrl);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down Expand Up @@ -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');
Expand All @@ -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();
Expand All @@ -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');
Expand Down

0 comments on commit 17fe05e

Please sign in to comment.