-
Notifications
You must be signed in to change notification settings - Fork 917
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Workspace] Add a workspace client in workspace plugin (#6094)
* feat: add comment Signed-off-by: SuZhou-Joe <[email protected]> * feat: update unit test Signed-off-by: SuZhou-Joe <[email protected]> * feat: add CHANGELOG Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize comment Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize comment Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize code Signed-off-by: SuZhou-Joe <[email protected]> * feat: optimize code Signed-off-by: SuZhou-Joe <[email protected]> --------- Signed-off-by: SuZhou-Joe <[email protected]>
- Loading branch information
1 parent
362ab1e
commit 4b89ad0
Showing
6 changed files
with
554 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |
Oops, something went wrong.