Skip to content

Commit

Permalink
test: add tests for workspace util
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 a68c0ec commit efc4c95
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/plugins/workspace/public/components/utils/workspace.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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

import { coreMock } from '../../../../../core/public/mocks';

const coreStartMock = coreMock.createStart();

window = Object.create(window);
const defaultUrl = 'localhost://';

describe('workspace utils', () => {
describe('switchWorkspace', () => {
it('should redirect if newUrl is returned', () => {
Object.defineProperty(window, 'location', {
value: {
href: defaultUrl,
},
writable: true,
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => 'new_url');
switchWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
expect(window.location.href).toEqual('new_url');
});

it('should not redirect if newUrl is not returned', () => {
Object.defineProperty(window, 'location', {
value: {
href: defaultUrl,
},
writable: true,
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => '');
switchWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
expect(window.location.href).toEqual(defaultUrl);
});
});

describe('updateWorkspace', () => {
it('should redirect if newUrl is returned', () => {
Object.defineProperty(window, 'location', {
value: {
href: defaultUrl,
},
writable: true,
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => 'new_url');
updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
expect(window.location.href).toEqual('new_url');
});

it('should not redirect if newUrl is not returned', () => {
Object.defineProperty(window, 'location', {
value: {
href: defaultUrl,
},
writable: true,
});
// @ts-ignore
formatUrlWithWorkspaceId.mockImplementation(() => '');
updateWorkspace({ application: coreStartMock.application, http: coreStartMock.http }, '');
expect(window.location.href).toEqual(defaultUrl);
});
});
});

0 comments on commit efc4c95

Please sign in to comment.