Skip to content

Commit

Permalink
fix some tests
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed May 20, 2021
1 parent e0a9fff commit f25dc4a
Show file tree
Hide file tree
Showing 20 changed files with 244 additions and 108 deletions.
20 changes: 10 additions & 10 deletions .eslintrc.js
Expand Up @@ -33,15 +33,6 @@ module.exports = {
}
},
overrides: [
{
files: [
"extensions/**/*.ts",
"extensions/**/*.tsx",
],
rules: {
"import/no-unresolved": "off", // warns on @k8slens/extensions
}
},
{
files: [
"**/*.js"
Expand Down Expand Up @@ -229,6 +220,15 @@ module.exports = {
{ "blankLine": "any", "prev": ["const", "let", "var"], "next": ["const", "let", "var"]},
]
},
}
},
{
files: [
"extensions/**/*.ts",
"extensions/**/*.tsx",
],
rules: {
"import/no-unresolved": "off", // warns on @k8slens/extensions
}
},
]
};
32 changes: 29 additions & 3 deletions src/common/__tests__/search-store.test.ts
Expand Up @@ -22,6 +22,7 @@
import { SearchStore } from "../search-store";
import { Console } from "console";
import { stdout, stderr } from "process";
import { DockStore } from "../../renderer/components/dock";

jest.mock("electron", () => ({
app: {
Expand All @@ -31,24 +32,33 @@ jest.mock("electron", () => ({

console = new Console(stdout, stderr);

let searchStore: SearchStore = null;
const logs = [
"1:M 30 Oct 2020 16:17:41.553 # Connection with replica 172.17.0.12:6379 lost",
"1:M 30 Oct 2020 16:17:41.623 * Replica 172.17.0.12:6379 asks for synchronization",
"1:M 30 Oct 2020 16:17:41.623 * Starting Partial resynchronization request from 172.17.0.12:6379 accepted. Sending 0 bytes of backlog starting from offset 14407."
];

describe("search store tests", () => {
beforeEach(async () => {
searchStore = new SearchStore();
beforeEach(() => {
DockStore.createInstance();
SearchStore.createInstance();
});

afterEach(() => {
DockStore.resetInstance();
SearchStore.resetInstance();
})

it("does nothing with empty search query", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch([], "");
expect(searchStore.occurrences).toEqual([]);
});

it("doesn't break if no text provided", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(null, "replica");
expect(searchStore.occurrences).toEqual([]);

Expand All @@ -57,33 +67,45 @@ describe("search store tests", () => {
});

it("find 3 occurrences across 3 lines", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "172");
expect(searchStore.occurrences).toEqual([0, 1, 2]);
});

it("find occurrences within 1 line (case-insensitive)", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "Starting");
expect(searchStore.occurrences).toEqual([2, 2]);
});

it("sets overlay index equal to first occurrence", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "Replica");
expect(searchStore.activeOverlayIndex).toBe(0);
});

it("set overlay index to next occurrence", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "172");
searchStore.setNextOverlayActive();
expect(searchStore.activeOverlayIndex).toBe(1);
});

it("sets overlay to last occurrence", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "172");
searchStore.setPrevOverlayActive();
expect(searchStore.activeOverlayIndex).toBe(2);
});

it("gets line index where overlay is located", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "synchronization");
expect(searchStore.activeOverlayLine).toBe(1);
});
Expand All @@ -95,12 +117,16 @@ describe("search store tests", () => {
});

it("gets active find number", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "172");
searchStore.setNextOverlayActive();
expect(searchStore.activeFind).toBe(2);
});

it("gets total finds number", () => {
const searchStore = SearchStore.getInstance();

searchStore.onSearch(logs, "Starting");
expect(searchStore.totalFinds).toBe(2);
});
Expand Down
14 changes: 7 additions & 7 deletions src/common/search-store.ts
Expand Up @@ -20,10 +20,10 @@
*/

import { action, computed, observable,reaction } from "mobx";
import { dockStore } from "../renderer/components/dock/dock.store";
import { autobind } from "../renderer/utils";
import { DockStore } from "../renderer/components/dock";
import { autobind, Singleton } from "../renderer/utils";

export class SearchStore {
export class SearchStore extends Singleton {
/**
* An utility methods escaping user string to safely pass it into new Regex(variable)
* @param value Unescaped string
Expand Down Expand Up @@ -54,8 +54,10 @@ export class SearchStore {
@observable activeOverlayIndex = -1;

constructor() {
reaction(() => dockStore.selectedTabId, () => {
searchStore.reset();
super();

reaction(() => DockStore.getInstance().selectedTabId, () => {
this.reset();
});
}

Expand Down Expand Up @@ -173,5 +175,3 @@ export class SearchStore {
this.occurrences = [];
}
}

export const searchStore = new SearchStore;
13 changes: 13 additions & 0 deletions src/renderer/api/__tests__/crd.test.ts
Expand Up @@ -19,10 +19,23 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { Cluster } from "../../../main/cluster";
import { ApiManager } from "../api-manager";
import { CustomResourceDefinition } from "../endpoints";
import type { IKubeObjectMetadata } from "../kube-object";

describe("Crds", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

describe("getVersion", () => {
it("should get the first version name from the list of versions", () => {
const crd = new CustomResourceDefinition({
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/api/__tests__/kube-api.test.ts
Expand Up @@ -19,10 +19,23 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { Cluster } from "../../../main/cluster";
import { ApiManager } from "../api-manager";
import { KubeApi } from "../kube-api";
import { KubeObject } from "../kube-object";

describe("KubeApi", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

it("uses url from apiBase if apiBase contains the resource", async () => {
(fetch as any).mockResponse(async (request: any) => {
if (request.url === "/api-kube/apis/networking.k8s.io/v1") {
Expand Down
13 changes: 13 additions & 0 deletions src/renderer/api/__tests__/pods.test.ts
Expand Up @@ -19,6 +19,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { Cluster } from "../../../main/cluster";
import { ApiManager } from "../api-manager";
import { Pod } from "../endpoints";

interface GetDummyPodOptions {
Expand Down Expand Up @@ -164,6 +166,17 @@ function getDummyPod(opts: GetDummyPodOptions = getDummyPodDefaultOptions()): Po
}

describe("Pods", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

const podTests = [];

for (let r = 0; r < 3; r += 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/api/api-manager.ts
Expand Up @@ -92,14 +92,14 @@ export class ApiManager extends Singleton {
}

@action
registerStore<Store extends KubeObjectStore<KubeObject>>(storeConstructor: KubeObjectStoreConstructor<Store>, apis?: KubeApi[]): this {
registerStore<Store extends KubeObjectStore<KubeObject>>(storeConstructor: KubeObjectStoreConstructor<Store>, apis?: KubeApi[]): Store {
const store = new storeConstructor(this.cluster);

for (const api of apis ?? [store.api]) {
this.stores.set(api.apiBase, store);
}

return this;
return store;
}

getStore<Store extends KubeObjectStore<KubeObject>>(api: string | KubeApi): Store | undefined {
Expand Down
Expand Up @@ -24,8 +24,10 @@ import { render, waitFor, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";

import { DeploymentScaleDialog } from "./deployment-scale-dialog";
jest.mock("../../api/endpoints");
import { Deployment, deploymentApi } from "../../api/endpoints";
jest.mock("../../api/endpoints/deployment.api");
import { Deployment, deploymentApi } from "../../api/endpoints/deployment.api";
import { ApiManager } from "../../api/api-manager";
import { Cluster } from "../../../main/cluster";

const dummyDeployment: Deployment = {
apiVersion: "v1",
Expand Down Expand Up @@ -116,6 +118,16 @@ const dummyDeployment: Deployment = {
};

describe("<DeploymentScaleDialog />", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

it("renders w/o errors", () => {
const { container } = render(<DeploymentScaleDialog />);
Expand Down
Expand Up @@ -26,6 +26,8 @@ import { ReplicaSetScaleDialog } from "./replicaset-scale-dialog";
import { render, waitFor, fireEvent } from "@testing-library/react";
import React from "react";
import { ReplicaSet, replicaSetApi } from "../../api/endpoints/replica-set.api";
import { Cluster } from "../../../main/cluster";
import { ApiManager } from "../../api/api-manager";

const dummyReplicaSet: ReplicaSet = {
apiVersion: "v1",
Expand Down Expand Up @@ -111,6 +113,17 @@ const dummyReplicaSet: ReplicaSet = {
};

describe("<ReplicaSetScaleDialog />", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

it("renders w/o errors", () => {
const { container } = render(<ReplicaSetScaleDialog/>);

Expand Down
Expand Up @@ -26,6 +26,8 @@ import { StatefulSet, statefulSetApi } from "../../api/endpoints";
import { StatefulSetScaleDialog } from "./statefulset-scale-dialog";
import { render, waitFor, fireEvent } from "@testing-library/react";
import React from "react";
import { Cluster } from "../../../main/cluster";
import { ApiManager } from "../../api/api-manager";

const dummyStatefulSet: StatefulSet = {
apiVersion: "v1",
Expand Down Expand Up @@ -121,6 +123,17 @@ const dummyStatefulSet: StatefulSet = {
};

describe("<StatefulSetScaleDialog />", () => {
beforeEach(() => {
ApiManager.createInstance(new Cluster({
id: "foo",
kubeConfigPath: "/bar",
}));
});

afterEach(() => {
ApiManager.resetInstance();
});

it("renders w/o errors", () => {
const { container } = render(<StatefulSetScaleDialog/>);

Expand Down

0 comments on commit f25dc4a

Please sign in to comment.