Preventing to create empty and existent workspaces

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev committed 2020-09-09 14:32:16 +03:00
1 parent 295333d055
commit 67e9e9b62a
3 files changed
+43 -8

No files matched your search

+16 -6
View File
@@ -51,6 +51,10 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
return this.workspaces.get(id);
}
getByName(name: string): Workspace {
return this.workspacesList.find(workspace => workspace.name === name);
}
@action
setActive(id = WorkspaceStore.defaultId, { redirectToLanding = true, resetActiveCluster = true } = {}) {
if (id === this.currentWorkspaceId) return;
@@ -67,14 +71,20 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
}
@action
saveWorkspace(workspace: Workspace) {
const id = workspace.id;
saveWorkspace(workspace: Workspace): { workspace?: Workspace, error?: string} {
const { id, name } = workspace;
const existingWorkspace = this.getById(id);
if (existingWorkspace) {
Object.assign(existingWorkspace, workspace);
} else {
this.workspaces.set(id, workspace);
if (!name) {
return { error: "Workspace should has a name." };
}
if (this.getByName(name)) {
return { error: `Workspace '${name}' already exist.` };
}
if (existingWorkspace) {
return { workspace: Object.assign(existingWorkspace, workspace) };
}
this.workspaces.set(id, workspace);
return { workspace };
}
@action
+22
View File
@@ -92,6 +92,28 @@ describe("workspace store tests", () => {
expect(ws.workspaces.size).toBe(2);
})
it("cannot create namespace with existent name", () => {
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
ws.saveWorkspace({
id: "someid",
name: "default",
});
expect(ws.workspacesList.length).toBe(1); // default workspace only
})
it("cannot create namespace with empty name", () => {
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
ws.saveWorkspace({
id: "random",
name: "",
});
expect(ws.workspacesList.length).toBe(1); // default workspace only
})
})
describe("for a non-empty config", () => {