mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Setting new workspace name restrictions (#832)
* Preventing to create empty and existent workspaces Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Autofocusing name input Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Save workspace when hitting Enter Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Refactoring of save workspace process Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
1 parent
e11cbc19fe
commit
fd3eb41179
3 files changed
+80
-11
No files matched your search
@@ -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;
|
||||
@@ -68,13 +72,16 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
|
||||
|
||||
@action
|
||||
saveWorkspace(workspace: Workspace) {
|
||||
const id = workspace.id;
|
||||
const { id, name } = workspace;
|
||||
const existingWorkspace = this.getById(id);
|
||||
if (!name.trim() || this.getByName(name.trim())) {
|
||||
return;
|
||||
}
|
||||
if (existingWorkspace) {
|
||||
Object.assign(existingWorkspace, workspace);
|
||||
} else {
|
||||
this.workspaces.set(id, workspace);
|
||||
}
|
||||
this.workspaces.set(id, workspace);
|
||||
return workspace;
|
||||
}
|
||||
|
||||
@action
|
||||
|
||||
@@ -92,6 +92,50 @@ describe("workspace store tests", () => {
|
||||
|
||||
expect(ws.workspaces.size).toBe(2);
|
||||
})
|
||||
|
||||
it("cannot create workspace 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 workspace with empty name", () => {
|
||||
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||
|
||||
ws.saveWorkspace({
|
||||
id: "random",
|
||||
name: "",
|
||||
});
|
||||
|
||||
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||
})
|
||||
|
||||
it("cannot create workspace with ' ' name", () => {
|
||||
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||
|
||||
ws.saveWorkspace({
|
||||
id: "random",
|
||||
name: " ",
|
||||
});
|
||||
|
||||
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||
})
|
||||
|
||||
it("trim workspace name", () => {
|
||||
const ws = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||
|
||||
ws.saveWorkspace({
|
||||
id: "random",
|
||||
name: "default ",
|
||||
});
|
||||
|
||||
expect(ws.workspacesList.length).toBe(1); // default workspace only
|
||||
})
|
||||
})
|
||||
|
||||
describe("for a non-empty config", () => {
|
||||
|
||||
Reference in new issue
Block a user