From 0deead641b86e50bf2f74113cd30670eab4dd789 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Mon, 3 May 2021 08:26:16 +0300 Subject: [PATCH] Adding test coverage Signed-off-by: Alex Andreev --- src/common/__tests__/hotbar-store.test.ts | 197 ++++++++++++++++++ src/common/hotbar-store.ts | 5 +- .../components/hotbar/hotbar-icon.tsx | 2 +- 3 files changed, 200 insertions(+), 4 deletions(-) diff --git a/src/common/__tests__/hotbar-store.test.ts b/src/common/__tests__/hotbar-store.test.ts index ceb45e15e0..8e4748e515 100644 --- a/src/common/__tests__/hotbar-store.test.ts +++ b/src/common/__tests__/hotbar-store.test.ts @@ -1,7 +1,71 @@ import mockFs from "mock-fs"; +import { CatalogEntityItem } from "../../renderer/components/+catalog/catalog-entity.store"; import { ClusterStore } from "../cluster-store"; import { HotbarStore } from "../hotbar-store"; +const testCluster = { + uid: "test", + name: "test", + apiVersion: "v1", + kind: "Cluster", + status: { + phase: "Running" + }, + spec: {}, + getName: jest.fn(), + getId: jest.fn(), + onDetailsOpen: jest.fn(), + onContextMenuOpen: jest.fn(), + onSettingsOpen: jest.fn(), + metadata: { + uid: "test", + name: "test", + labels: {} + } +}; + +const minikubeCluster = { + uid: "minikube", + name: "minikube", + apiVersion: "v1", + kind: "Cluster", + status: { + phase: "Running" + }, + spec: {}, + getName: jest.fn(), + getId: jest.fn(), + onDetailsOpen: jest.fn(), + onContextMenuOpen: jest.fn(), + onSettingsOpen: jest.fn(), + metadata: { + uid: "minikube", + name: "minikube", + labels: {} + } +}; + +const awsCluster = { + uid: "aws", + name: "aws", + apiVersion: "v1", + kind: "Cluster", + status: { + phase: "Running" + }, + spec: {}, + getName: jest.fn(), + getId: jest.fn(), + onDetailsOpen: jest.fn(), + onContextMenuOpen: jest.fn(), + onSettingsOpen: jest.fn(), + metadata: { + uid: "aws", + name: "aws", + labels: {} + } +}; + describe("HotbarStore", () => { beforeEach(() => { ClusterStore.resetInstance(); @@ -31,4 +95,137 @@ describe("HotbarStore", () => { expect(hotbarStore.hotbars.length).toEqual(2); }); }); + + describe("hotbar items", () => { + it("initially creates 12 empty cells", () => { + const hotbarStore = HotbarStore.createInstance(); + + hotbarStore.load(); + expect(hotbarStore.getActive().items.length).toEqual(12); + }); + + it("adds items", () => { + const hotbarStore = HotbarStore.createInstance(); + const entity = new CatalogEntityItem(testCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(entity); + const items = hotbarStore.getActive().items.filter(Boolean); + + expect(items.length).toEqual(1); + }); + + it("removes items", () => { + const hotbarStore = HotbarStore.createInstance(); + const entity = new CatalogEntityItem(testCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(entity); + hotbarStore.removeFromHotbar("test"); + const items = hotbarStore.getActive().items.filter(Boolean); + + expect(items.length).toEqual(0); + }); + + it("does nothing if removing with invalid uid", () => { + const hotbarStore = HotbarStore.createInstance(); + const entity = new CatalogEntityItem(testCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(entity); + hotbarStore.removeFromHotbar("invalid uid"); + const items = hotbarStore.getActive().items.filter(Boolean); + + expect(items.length).toEqual(1); + }); + + it("moves item to empty cell", () => { + const hotbarStore = HotbarStore.createInstance(); + const test = new CatalogEntityItem(testCluster); + const minikube = new CatalogEntityItem(minikubeCluster); + const aws = new CatalogEntityItem(awsCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(test); + hotbarStore.addToHotbar(minikube); + hotbarStore.addToHotbar(aws); + + expect(hotbarStore.getActive().items[5]).toBeNull(); + + hotbarStore.restackItems(1, 5); + + expect(hotbarStore.getActive().items[5]).toBeTruthy(); + expect(hotbarStore.getActive().items[5].entity.uid).toEqual("minikube"); + }); + + it("moves items down", () => { + const hotbarStore = HotbarStore.createInstance(); + const test = new CatalogEntityItem(testCluster); + const minikube = new CatalogEntityItem(minikubeCluster); + const aws = new CatalogEntityItem(awsCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(test); + hotbarStore.addToHotbar(minikube); + hotbarStore.addToHotbar(aws); + + // aws -> test + hotbarStore.restackItems(2, 0); + + const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null); + + expect(items.slice(0, 4)).toEqual(["aws", "test", "minikube", null]); + }); + + it("moves items up", () => { + const hotbarStore = HotbarStore.createInstance(); + const test = new CatalogEntityItem(testCluster); + const minikube = new CatalogEntityItem(minikubeCluster); + const aws = new CatalogEntityItem(awsCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(test); + hotbarStore.addToHotbar(minikube); + hotbarStore.addToHotbar(aws); + + // test -> aws + hotbarStore.restackItems(0, 2); + + const items = hotbarStore.getActive().items.map(item => item?.entity.uid || null); + + expect(items.slice(0, 4)).toEqual(["minikube", "aws", "test", null]); + }); + + it("does nothing when item moved to same cell", () => { + const hotbarStore = HotbarStore.createInstance(); + const test = new CatalogEntityItem(testCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(test); + hotbarStore.restackItems(0, 0); + + expect(hotbarStore.getActive().items[0].entity.uid).toEqual("test"); + }); + + it("throws if invalid arguments provided", () => { + // Prevent writing to stderr during this render. + const err = console.error; + + console.error = jest.fn(); + + const hotbarStore = HotbarStore.createInstance(); + const test = new CatalogEntityItem(testCluster); + + hotbarStore.load(); + hotbarStore.addToHotbar(test); + + expect(() => hotbarStore.restackItems(-5, 0)).toThrow(); + expect(() => hotbarStore.restackItems(2, -1)).toThrow(); + expect(() => hotbarStore.restackItems(14, 1)).toThrow(); + expect(() => hotbarStore.restackItems(11, 112)).toThrow(); + + // Restore writing to stderr. + console.error = err; + }); + }); }); diff --git a/src/common/hotbar-store.ts b/src/common/hotbar-store.ts index d81cc2b03a..e4a871f093 100644 --- a/src/common/hotbar-store.ts +++ b/src/common/hotbar-store.ts @@ -4,7 +4,6 @@ import migrations from "../migrations/hotbar-store"; import * as uuid from "uuid"; import { CatalogEntityItem } from "../renderer/components/+catalog/catalog-entity.store"; import isNull from "lodash/isNull"; -import { CatalogEntity } from "./catalog/catalog-entity"; export interface HotbarItem { entity: { @@ -147,9 +146,9 @@ export class HotbarStore extends BaseStore { } } - removeFromHotbar(item: CatalogEntity) { + removeFromHotbar(uid: string) { const hotbar = this.getActive(); - const index = hotbar.items.findIndex((i) => i?.entity.uid === item.getId()); + const index = hotbar.items.findIndex((i) => i?.entity.uid === uid); if (index == -1) { return; diff --git a/src/renderer/components/hotbar/hotbar-icon.tsx b/src/renderer/components/hotbar/hotbar-icon.tsx index 5bc6489c8c..82be0ee88c 100644 --- a/src/renderer/components/hotbar/hotbar-icon.tsx +++ b/src/renderer/components/hotbar/hotbar-icon.tsx @@ -94,7 +94,7 @@ export class HotbarIcon extends React.Component { remove(item: CatalogEntity) { const hotbar = HotbarStore.getInstance(); - hotbar.removeFromHotbar(item); + hotbar.removeFromHotbar(item.getId()); } onMenuItemClick(menuItem: CatalogEntityContextMenu) {