1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Tuning hotbar store tests

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2021-06-18 14:59:25 +03:00
parent 80803f78a6
commit de9885a666
2 changed files with 25 additions and 13 deletions

View File

@ -156,6 +156,13 @@ describe("HotbarStore", () => {
expect(hotbarStore.getActive().items.length).toEqual(12); expect(hotbarStore.getActive().items.length).toEqual(12);
}); });
it("initially adds catalog entity as first item", () => {
const hotbarStore = HotbarStore.createInstance();
hotbarStore.load();
expect(hotbarStore.getActive().items[0].entity.name).toEqual("Catalog");
});
it("adds items", () => { it("adds items", () => {
const hotbarStore = HotbarStore.createInstance(); const hotbarStore = HotbarStore.createInstance();
@ -163,7 +170,7 @@ describe("HotbarStore", () => {
hotbarStore.addToHotbar(testCluster); hotbarStore.addToHotbar(testCluster);
const items = hotbarStore.getActive().items.filter(Boolean); const items = hotbarStore.getActive().items.filter(Boolean);
expect(items.length).toEqual(1); expect(items.length).toEqual(2);
}); });
it("removes items", () => { it("removes items", () => {
@ -172,6 +179,7 @@ describe("HotbarStore", () => {
hotbarStore.load(); hotbarStore.load();
hotbarStore.addToHotbar(testCluster); hotbarStore.addToHotbar(testCluster);
hotbarStore.removeFromHotbar("test"); hotbarStore.removeFromHotbar("test");
hotbarStore.removeFromHotbar("catalog-entity");
const items = hotbarStore.getActive().items.filter(Boolean); const items = hotbarStore.getActive().items.filter(Boolean);
expect(items.length).toEqual(0); expect(items.length).toEqual(0);
@ -185,7 +193,7 @@ describe("HotbarStore", () => {
hotbarStore.removeFromHotbar("invalid uid"); hotbarStore.removeFromHotbar("invalid uid");
const items = hotbarStore.getActive().items.filter(Boolean); const items = hotbarStore.getActive().items.filter(Boolean);
expect(items.length).toEqual(1); expect(items.length).toEqual(2);
}); });
it("moves item to empty cell", () => { it("moves item to empty cell", () => {
@ -196,12 +204,12 @@ describe("HotbarStore", () => {
hotbarStore.addToHotbar(minikubeCluster); hotbarStore.addToHotbar(minikubeCluster);
hotbarStore.addToHotbar(awsCluster); hotbarStore.addToHotbar(awsCluster);
expect(hotbarStore.getActive().items[5]).toBeNull(); expect(hotbarStore.getActive().items[6]).toBeNull();
hotbarStore.restackItems(1, 5); hotbarStore.restackItems(1, 5);
expect(hotbarStore.getActive().items[5]).toBeTruthy(); expect(hotbarStore.getActive().items[5]).toBeTruthy();
expect(hotbarStore.getActive().items[5].entity.uid).toEqual("minikube"); expect(hotbarStore.getActive().items[5].entity.uid).toEqual("test");
}); });
it("moves items down", () => { it("moves items down", () => {
@ -212,12 +220,12 @@ describe("HotbarStore", () => {
hotbarStore.addToHotbar(minikubeCluster); hotbarStore.addToHotbar(minikubeCluster);
hotbarStore.addToHotbar(awsCluster); hotbarStore.addToHotbar(awsCluster);
// aws -> test // aws -> catalog
hotbarStore.restackItems(2, 0); hotbarStore.restackItems(3, 0);
const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null); const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null);
expect(items.slice(0, 4)).toEqual(["aws", "test", "minikube", null]); expect(items.slice(0, 4)).toEqual(["aws", "catalog-entity", "test", "minikube"]);
}); });
it("moves items up", () => { it("moves items up", () => {
@ -229,11 +237,11 @@ describe("HotbarStore", () => {
hotbarStore.addToHotbar(awsCluster); hotbarStore.addToHotbar(awsCluster);
// test -> aws // test -> aws
hotbarStore.restackItems(0, 2); hotbarStore.restackItems(1, 3);
const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null); const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null);
expect(items.slice(0, 4)).toEqual(["minikube", "aws", "test", null]); expect(items.slice(0, 4)).toEqual(["catalog-entity", "minikube", "aws", "test"]);
}); });
it("does nothing when item moved to same cell", () => { it("does nothing when item moved to same cell", () => {
@ -241,9 +249,9 @@ describe("HotbarStore", () => {
hotbarStore.load(); hotbarStore.load();
hotbarStore.addToHotbar(testCluster); hotbarStore.addToHotbar(testCluster);
hotbarStore.restackItems(0, 0); hotbarStore.restackItems(1, 1);
expect(hotbarStore.getActive().items[0].entity.uid).toEqual("test"); expect(hotbarStore.getActive().items[1].entity.uid).toEqual("test");
}); });
it("new items takes first empty cell", () => { it("new items takes first empty cell", () => {

View File

@ -92,7 +92,7 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
return this.hotbarIndex(this.activeHotbarId); return this.hotbarIndex(this.activeHotbarId);
} }
get initialItems() { get defaultHotbarInitialItems() {
const { metadata: { uid, name, source } } = catalogEntity; const { metadata: { uid, name, source } } = catalogEntity;
const initialItem = { entity: { uid, name, source }}; const initialItem = { entity: { uid, name, source }};
@ -102,12 +102,16 @@ export class HotbarStore extends BaseStore<HotbarStoreModel> {
]; ];
} }
get initialItems() {
return [...Array.from(Array(defaultHotbarCells).fill(null))];
}
@action protected async fromStore(data: Partial<HotbarStoreModel> = {}) { @action protected async fromStore(data: Partial<HotbarStoreModel> = {}) {
if (data.hotbars?.length === 0) { if (data.hotbars?.length === 0) {
this.hotbars = [{ this.hotbars = [{
id: uuid.v4(), id: uuid.v4(),
name: "Default", name: "Default",
items: this.initialItems, items: this.defaultHotbarInitialItems,
}]; }];
} else { } else {
this.hotbars = data.hotbars; this.hotbars = data.hotbars;