Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace] Add a workspace client in workspace plugin #6094

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Test connection schema validation for registered auth types ([#6109](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6109))
- [Workspace] Consume workspace id in saved object client ([#6014](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6014))
- [Multiple Datasource] Export DataSourcePluginRequestContext at top level for plugins to use ([#6108](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6108))

- [Workspace] Add delete saved objects by workspace functionality([#6013](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6013))
- [Workspace] Add a workspace client in workspace plugin ([#6094](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6094))

### 🐛 Bug Fixes

Expand Down
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
Loading