mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
add some basic workspace store tests
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
This commit is contained in:
parent
6725a6f7e7
commit
3db9879b1b
@ -51,6 +51,10 @@ export class WorkspaceStore extends BaseStore<WorkspaceStoreModel> {
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
setActive(id = WorkspaceStore.defaultId) {
|
setActive(id = WorkspaceStore.defaultId) {
|
||||||
|
if (!this.getById(id)) {
|
||||||
|
throw new Error(`workspace ${id} doesn't exist`);
|
||||||
|
}
|
||||||
|
|
||||||
this.currentWorkspaceId = id;
|
this.currentWorkspaceId = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
123
src/common/workspace-store_test.ts
Normal file
123
src/common/workspace-store_test.ts
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
import mockFs from "mock-fs"
|
||||||
|
|
||||||
|
jest.mock("electron", () => {
|
||||||
|
return {
|
||||||
|
app: {
|
||||||
|
getVersion: () => '99.99.99',
|
||||||
|
getPath: () => 'tmp',
|
||||||
|
getLocale: () => 'en'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
import { WorkspaceStore } from "./workspace-store"
|
||||||
|
|
||||||
|
describe("workspace store tests", () => {
|
||||||
|
describe("for an empty config", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
WorkspaceStore.resetInstance()
|
||||||
|
mockFs({ tmp: { 'lens-workspace-store.json': "{}" } })
|
||||||
|
|
||||||
|
await WorkspaceStore.getInstance<WorkspaceStore>().load();
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mockFs.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("default workspace should always exist", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
expect(us.workspaces.size).toBe(1);
|
||||||
|
expect(us.getById(WorkspaceStore.defaultId)).not.toBe(null);
|
||||||
|
})
|
||||||
|
|
||||||
|
it("cannot remove the default workspace", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
expect(() => us.removeWorkspace(WorkspaceStore.defaultId)).toThrowError("Cannot remove");
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can update default workspace name", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
us.saveWorkspace({
|
||||||
|
id: WorkspaceStore.defaultId,
|
||||||
|
name: "foobar",
|
||||||
|
});
|
||||||
|
expect(us.currentWorkspace.name).toBe("foobar");
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can add workspaces", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
us.saveWorkspace({
|
||||||
|
id: "123",
|
||||||
|
name: "foobar",
|
||||||
|
});
|
||||||
|
expect(us.getById("123").name).toBe("foobar");
|
||||||
|
})
|
||||||
|
|
||||||
|
it("cannot set a non-existant workspace to be active", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
expect(() => us.setActive("abc")).toThrow("doesn't exist");
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can set a existant workspace to be active", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
us.saveWorkspace({
|
||||||
|
id: "abc",
|
||||||
|
name: "foobar",
|
||||||
|
});
|
||||||
|
expect(() => us.setActive("abc")).not.toThrowError();
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can remove a workspace", () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
us.saveWorkspace({
|
||||||
|
id: "123",
|
||||||
|
name: "foobar",
|
||||||
|
});
|
||||||
|
us.saveWorkspace({
|
||||||
|
id: "1234",
|
||||||
|
name: "foobar 1",
|
||||||
|
});
|
||||||
|
us.removeWorkspace("123");
|
||||||
|
expect(us.workspaces.size).toBe(2);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("for a non-empty config", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
WorkspaceStore.resetInstance()
|
||||||
|
mockFs({
|
||||||
|
tmp: {
|
||||||
|
'lens-workspace-store.json': JSON.stringify({
|
||||||
|
currentWorkspace: "abc",
|
||||||
|
workspaces: [{
|
||||||
|
id: "abc",
|
||||||
|
name: "test"
|
||||||
|
}, {
|
||||||
|
id: "default",
|
||||||
|
name: "default"
|
||||||
|
}]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
await WorkspaceStore.getInstance<WorkspaceStore>().load();
|
||||||
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mockFs.restore()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("doesn't revert to default workspace", async () => {
|
||||||
|
const us = WorkspaceStore.getInstance<WorkspaceStore>();
|
||||||
|
|
||||||
|
expect(us.currentWorkspaceId).toBe("abc");
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Loading…
Reference in New Issue
Block a user