From 99eac44479a566c3a19c63d162b50851c5dab4d2 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Fri, 15 Oct 2021 11:13:55 -0400 Subject: [PATCH] Fix unit tests - Remove prettier as snapShot testing is too tight of a bound Signed-off-by: Sebastian Malton --- package.json | 1 - .../components/+catalog/catalog.test.tsx | 112 ++++++------------ yarn.lock | 5 - 3 files changed, 35 insertions(+), 83 deletions(-) diff --git a/package.json b/package.json index ab927fd245..b067b4b8c7 100644 --- a/package.json +++ b/package.json @@ -357,7 +357,6 @@ "postcss": "^8.3.6", "postcss-loader": "4.3.0", "postinstall-postinstall": "^2.1.0", - "prettier": "^2.4.1", "progress-bar-webpack-plugin": "^2.1.0", "randomcolor": "^0.6.2", "raw-loader": "^4.0.2", diff --git a/src/renderer/components/+catalog/catalog.test.tsx b/src/renderer/components/+catalog/catalog.test.tsx index bb1ce35a5b..417496a038 100644 --- a/src/renderer/components/+catalog/catalog.test.tsx +++ b/src/renderer/components/+catalog/catalog.test.tsx @@ -26,7 +26,7 @@ import { Catalog } from "./catalog"; import { createMemoryHistory } from "history"; import { mockWindow } from "../../../../__mocks__/windowMock"; import { kubernetesClusterCategory } from "../../../common/catalog-entities/kubernetes-cluster"; -import { catalogCategoryRegistry, CatalogCategoryRegistry } from "../../../common/catalog"; +import { catalogCategoryRegistry, CatalogCategoryRegistry, CatalogEntity, CatalogEntityActionContext, CatalogEntityData } from "../../../common/catalog"; import { CatalogEntityRegistry } from "../../../renderer/api/catalog-entity-registry"; import { CatalogEntityDetailRegistry } from "../../../extensions/registries"; import { CatalogEntityItem } from "./catalog-entity-item"; @@ -46,6 +46,18 @@ jest.mock("@electron/remote", () => { }; }); +class MockCatalogEntity extends CatalogEntity { + public apiVersion = "api"; + public kind = "kind"; + + constructor(data: CatalogEntityData, public onRun: (context: CatalogEntityActionContext) => void | Promise) { + super(data); + } + + public onContextMenuOpen(): void | Promise {} + public onSettingsOpen(): void | Promise {} +} + describe("", () => { const history = createMemoryHistory(); const mockLocation = { @@ -66,30 +78,21 @@ describe("", () => { url: "", }; - const catalogEntityUid = "a_catalogEntity_uid"; - const catalogEntity = { - enabled: true, - apiVersion: "api", - kind: "kind", - metadata: { - uid: catalogEntityUid, - name: "a catalog entity", - labels: { - test: "label", + function createMockCatalogEntity(onRun: (context: CatalogEntityActionContext) => void | Promise) { + return new MockCatalogEntity({ + metadata: { + uid: "a_catalogEntity_uid", + name: "a catalog entity", + labels: { + test: "label", + }, }, - }, - status: { - phase: "", - }, - spec: {}, - }; - const catalogEntityItemMethods = { - getId: () => catalogEntity.metadata.uid, - getName: () => catalogEntity.metadata.name, - onContextMenuOpen: () => {}, - onSettingsOpen: () => {}, - onRun: () => {}, - }; + status: { + phase: "", + }, + spec: {}, + }, onRun); + } beforeEach(() => { CatalogEntityDetailRegistry.createInstance(); @@ -117,11 +120,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest @@ -130,29 +129,8 @@ describe("", () => { catalogEntityRegistry.addOnBeforeRun( (event) => { - expect(event.target).toMatchInlineSnapshot(` - Object { - "apiVersion": "api", - "enabled": true, - "getId": [Function], - "getName": [Function], - "kind": "kind", - "metadata": Object { - "labels": Object { - "test": "label", - }, - "name": "a catalog entity", - "uid": "a_catalogEntity_uid", - }, - "onContextMenuOpen": [Function], - "onRun": [MockFunction], - "onSettingsOpen": [Function], - "spec": Object {}, - "status": Object { - "phase": "", - }, - } - `); + expect(event.target.getId()).toBe("a_catalogEntity_uid"); + expect(event.target.getName()).toBe("a catalog entity"); setTimeout(() => { expect(onRun).toHaveBeenCalled(); @@ -178,11 +156,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest @@ -216,11 +190,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest @@ -255,11 +225,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(() => done()); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest @@ -289,11 +255,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest @@ -330,11 +292,7 @@ describe("", () => { const catalogEntityRegistry = new CatalogEntityRegistry(catalogCategoryRegistry); const catalogEntityStore = new CatalogEntityStore(catalogEntityRegistry); const onRun = jest.fn(); - const catalogEntityItem = new CatalogEntityItem({ - ...catalogEntity, - ...catalogEntityItemMethods, - onRun, - }, catalogEntityRegistry); + const catalogEntityItem = new CatalogEntityItem(createMockCatalogEntity(onRun), catalogEntityRegistry); // mock as if there is a selected item > the detail panel opens jest diff --git a/yarn.lock b/yarn.lock index 32ae056719..521337e4ca 100644 --- a/yarn.lock +++ b/yarn.lock @@ -11220,11 +11220,6 @@ prepend-http@^2.0.0: resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897" integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc= -prettier@^2.4.1: - version "2.4.1" - resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.1.tgz#671e11c89c14a4cfc876ce564106c4a6726c9f5c" - integrity sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA== - pretty-error@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/pretty-error/-/pretty-error-2.1.1.tgz#5f4f87c8f91e5ae3f3ba87ab4cf5e03b1a17f1a3"