From 9abb9c30175c0986ed0019a45e891cc3f756fc80 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Sat, 9 Mar 2024 09:06:38 +0800 Subject: [PATCH] feat: optimize code Signed-off-by: SuZhou-Joe --- .../workspace/public/workspace_client.test.ts | 35 +++++++++++++++++-- .../workspace/public/workspace_client.ts | 2 ++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/plugins/workspace/public/workspace_client.test.ts b/src/plugins/workspace/public/workspace_client.test.ts index 7d05c3f22458..ef5ba154e94f 100644 --- a/src/plugins/workspace/public/workspace_client.test.ts +++ b/src/plugins/workspace/public/workspace_client.test.ts @@ -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', { @@ -171,6 +175,11 @@ describe('#WorkspaceClient', () => { }, }), }); + expect(workspaceMock.workspaceList$.getValue()).toEqual([ + { + id: 'foo', + }, + ]); expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', { method: 'POST', body: JSON.stringify({ @@ -178,4 +187,26 @@ describe('#WorkspaceClient', () => { }), }); }); + + 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([]); + }); }); diff --git a/src/plugins/workspace/public/workspace_client.ts b/src/plugins/workspace/public/workspace_client.ts index fb633cb16906..31a7545db023 100644 --- a/src/plugins/workspace/public/workspace_client.ts +++ b/src/plugins/workspace/public/workspace_client.ts @@ -117,6 +117,8 @@ export class WorkspaceClient { if (result?.success) { this.workspaces.workspaceList$.next(result.result.workspaces); + } else { + this.workspaces.workspaceList$.next([]); } }