Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Mar 9, 2024
1 parent 1620f4a commit 56ba159
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/plugins/workspace/public/workspace_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,15 @@ describe('#WorkspaceClient', () => {
});

it('#update', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [],
workspaces: [
{
id: 'foo',
},
],
},
});
await workspaceClient.update('foo', {
Expand All @@ -171,11 +175,38 @@ describe('#WorkspaceClient', () => {
},
}),
});
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([]);
});
});
2 changes: 2 additions & 0 deletions src/plugins/workspace/public/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ export class WorkspaceClient {

if (result?.success) {
this.workspaces.workspaceList$.next(result.result.workspaces);
} else {
this.workspaces.workspaceList$.next([]);
}
}

Expand Down

0 comments on commit 56ba159

Please sign in to comment.