Skip to content

Commit

Permalink
[Workspace] Add a workspace client in workspace plugin (#6094)
Browse files Browse the repository at this point in the history
* feat: add comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: update unit test

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: add CHANGELOG

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: optimize comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: optimize comment

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

* feat: optimize code

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>

---------

Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
(cherry picked from commit 4b89ad0)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
github-actions[bot] committed Apr 11, 2024
1 parent 9ac936c commit 89e4629
Show file tree
Hide file tree
Showing 5 changed files with 553 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/plugins/workspace/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { coreMock } from '../../../core/public/mocks';
import { workspaceClientMock, WorkspaceClientMock } from './workspace_client.mock';
import { chromeServiceMock, coreMock } from '../../../core/public/mocks';
import { WorkspacePlugin } from './plugin';

describe('Workspace plugin', () => {
const getSetupMock = () => ({
...coreMock.createSetup(),
chrome: chromeServiceMock.createSetupContract(),
});
beforeEach(() => {
WorkspaceClientMock.mockClear();
Object.values(workspaceClientMock).forEach((item) => item.mockClear());
});
it('#setup', async () => {
const setupMock = getSetupMock();
const workspacePlugin = new WorkspacePlugin();
await workspacePlugin.setup(setupMock);
expect(WorkspaceClientMock).toBeCalledTimes(1);
});

it('#call savedObjectsClient.setCurrentWorkspace when current workspace id changed', () => {
const workspacePlugin = new WorkspacePlugin();
const coreStart = coreMock.createStart();
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import type { Subscription } from 'rxjs';
import { Plugin, CoreStart } from '../../../core/public';
import { Plugin, CoreStart, CoreSetup } from '../../../core/public';
import { WorkspaceClient } from './workspace_client';

export class WorkspacePlugin implements Plugin<{}, {}, {}> {
private coreStart?: CoreStart;
Expand All @@ -18,7 +19,9 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> {
});
}
}
public async setup() {
public async setup(core: CoreSetup) {
const workspaceClient = new WorkspaceClient(core.http, core.workspaces);
await workspaceClient.init();
return {};
}

Expand Down
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,
}));
212 changes: 212 additions & 0 deletions src/plugins/workspace/public/workspace_client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* 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(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, workspaceMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [
{
id: 'foo',
},
],
},
});
await workspaceClient.update('foo', {
name: 'foo',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'PUT',
body: JSON.stringify({
attributes: {
name: 'foo',
},
}),
});
expect(workspaceMock.workspaceList$.getValue()).toEqual([
{
id: 'foo',
},
]);
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#update with list gives error', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
let callTimes = 0;
httpSetupMock.fetch.mockImplementation(async () => {
callTimes++;
if (callTimes > 1) {
return {
success: false,
error: 'Something went wrong',
};
}

return {
success: true,
};
});
await workspaceClient.update('foo', {
name: 'foo',
});
expect(workspaceMock.workspaceList$.getValue()).toEqual([]);
});
});
Loading

0 comments on commit 89e4629

Please sign in to comment.