Skip to content

Commit

Permalink
feat: add comment
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Mar 8, 2024
1 parent 89fcf63 commit d55e79c
Show file tree
Hide file tree
Showing 3 changed files with 481 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/plugins/workspace/public/workspace_client.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const workspaceClientMock = {
init: jest.fn(),
enterWorkspace: jest.fn(),
getCurrentWorkspaceId: jest.fn(),
getCurrentWorkspace: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
list: jest.fn(),
get: jest.fn(),
update: jest.fn(),
stop: jest.fn(),
};

export const WorkspaceClientMock = jest.fn(function () {
return workspaceClientMock;
});

jest.doMock('./workspace_client', () => ({
WorkspaceClient: WorkspaceClientMock,
}));
181 changes: 181 additions & 0 deletions src/plugins/workspace/public/workspace_client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { httpServiceMock, workspacesServiceMock } from '../../../core/public/mocks';
import { WorkspaceClient } from './workspace_client';

const getWorkspaceClient = () => {
const httpSetupMock = httpServiceMock.createSetupContract();
const workspaceMock = workspacesServiceMock.createSetupContract();
return {
httpSetupMock,
workspaceMock,
workspaceClient: new WorkspaceClient(httpSetupMock, workspaceMock),
};
};

describe('#WorkspaceClient', () => {
it('#init', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
await workspaceClient.init();
expect(workspaceMock.initialized$.getValue()).toEqual(true);
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#enterWorkspace', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: false,
});
const result = await workspaceClient.enterWorkspace('foo');
expect(result.success).toEqual(false);
httpSetupMock.fetch.mockResolvedValue({
success: true,
});
const successResult = await workspaceClient.enterWorkspace('foo');
expect(workspaceMock.currentWorkspaceId$.getValue()).toEqual('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'GET',
});
expect(successResult.success).toEqual(true);
});

it('#getCurrentWorkspaceId', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
});
await workspaceClient.enterWorkspace('foo');
expect(await workspaceClient.getCurrentWorkspaceId()).toEqual({
success: true,
result: 'foo',
});
});

it('#getCurrentWorkspace', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
},
});
await workspaceClient.enterWorkspace('foo');
expect(await workspaceClient.getCurrentWorkspace()).toEqual({
success: true,
result: {
name: 'foo',
},
});
});

it('#create', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
workspaces: [],
},
});
await workspaceClient.create({
name: 'foo',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces', {
method: 'POST',
body: JSON.stringify({
attributes: {
name: 'foo',
},
}),
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#delete', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
workspaces: [],
},
});
await workspaceClient.delete('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'DELETE',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#list', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [],
},
});
await workspaceClient.list({
perPage: 999,
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#get', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
await workspaceClient.get('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'GET',
});
});

it('#update', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [],
},
});
await workspaceClient.update('foo', {
name: 'foo',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'PUT',
body: JSON.stringify({
attributes: {
name: 'foo',
},
}),
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});
});
Loading

0 comments on commit d55e79c

Please sign in to comment.