From 6b188a2c1ed5991f3b263985506594f835ac4eeb Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Mon, 6 Sep 2021 11:00:27 +0300 Subject: [PATCH] Updating the tests Signed-off-by: Alex Andreev --- .../__tests__/delete-cluster-dialog.test.tsx | 241 ++++++++++++------ 1 file changed, 159 insertions(+), 82 deletions(-) diff --git a/src/renderer/components/delete-cluster-dialog/__tests__/delete-cluster-dialog.test.tsx b/src/renderer/components/delete-cluster-dialog/__tests__/delete-cluster-dialog.test.tsx index faf68fe075..7d861a71c7 100644 --- a/src/renderer/components/delete-cluster-dialog/__tests__/delete-cluster-dialog.test.tsx +++ b/src/renderer/components/delete-cluster-dialog/__tests__/delete-cluster-dialog.test.tsx @@ -65,108 +65,185 @@ users: token: kubeconfig-user-q4lm4:xxxyyyy `; +const singleClusterConfig = ` +apiVersion: v1 +clusters: +- cluster: + server: http://localhost + name: other-cluster +contexts: +- context: + cluster: other-cluster + user: test + name: other-context +current-context: other-context +kind: Config +preferences: {} +users: +- name: test + user: + token: kubeconfig-user-q4lm4:xxxyyyy +`; + let config: KubeConfig; describe("", () => { - beforeEach(async () => { - const mockOpts = { - "temp-kube-config": kubeconfig, - }; + describe("Kubeconfig with different clusters", () => { + beforeEach(async () => { + const mockOpts = { + "temp-kube-config": kubeconfig, + }; - mockFs(mockOpts); + mockFs(mockOpts); - config = new KubeConfig(); - config.loadFromString(kubeconfig); - }); - - afterEach(() => { - mockFs.restore(); - }); - - it("renders w/o errors", () => { - const { container } = render(); - - expect(container).toBeInstanceOf(HTMLElement); - }); - - it("shows warning when deleting non-current-context cluster", () => { - const cluster = new Cluster({ - id: "test", - contextName: "test", - preferences: { - clusterName: "minikube" - }, - kubeConfigPath: "./temp-kube-config", + config = new KubeConfig(); + config.loadFromString(kubeconfig); }); - DeleteClusterDialog.open({ cluster, config }); - const { getByText } = render(); - - const message = "The contents of kubeconfig file will be changed!"; - - expect(getByText(message)).toBeInstanceOf(HTMLElement); - }); - - it("shows warning when deleting current-context cluster", () => { - const cluster = new Cluster({ - id: "other-cluster", - contextName: "other-context", - preferences: { - clusterName: "other-cluster" - }, - kubeConfigPath: "./temp-kube-config", + afterEach(() => { + mockFs.restore(); }); - DeleteClusterDialog.open({ cluster, config }); + it("renders w/o errors", () => { + const { container } = render(); - const { getByTestId } = render(); - - expect(getByTestId("context-warning")).toBeInstanceOf(HTMLElement); - }); - - it("shows context switcher when deleting current cluster", async () => { - const cluster = new Cluster({ - id: "other-cluster", - contextName: "other-context", - preferences: { - clusterName: "other-cluster" - }, - kubeConfigPath: "./temp-kube-config", + expect(container).toBeInstanceOf(HTMLElement); }); - DeleteClusterDialog.open({ cluster, config }); + it("shows warning when deleting non-current-context cluster", () => { + const cluster = new Cluster({ + id: "test", + contextName: "test", + preferences: { + clusterName: "minikube" + }, + kubeConfigPath: "./temp-kube-config", + }); - const { getByText } = render(); + DeleteClusterDialog.open({ cluster, config }); + const { getByText } = render(); - expect(getByText("Select...")).toBeInTheDocument(); - selectEvent.openMenu(getByText("Select...")); + const message = "The contents of kubeconfig file will be changed!"; - expect(getByText("test")).toBeInTheDocument(); - expect(getByText("test2")).toBeInTheDocument(); - }); - - it("shows context switcher after checkbox click", async () => { - const cluster = new Cluster({ - id: "some-cluster", - contextName: "test", - preferences: { - clusterName: "test" - }, - kubeConfigPath: "./temp-kube-config", + expect(getByText(message)).toBeInstanceOf(HTMLElement); }); - DeleteClusterDialog.open({ cluster, config }); + it("shows warning when deleting current-context cluster", () => { + const cluster = new Cluster({ + id: "other-cluster", + contextName: "other-context", + preferences: { + clusterName: "other-cluster" + }, + kubeConfigPath: "./temp-kube-config", + }); - const { getByText, getByTestId } = render(); - const link = getByTestId("context-switch"); + DeleteClusterDialog.open({ cluster, config }); - expect(link).toBeInstanceOf(HTMLElement); - fireEvent.click(link); + const { getByTestId } = render(); - expect(getByText("Select...")).toBeInTheDocument(); - selectEvent.openMenu(getByText("Select...")); + expect(getByTestId("current-context-warning")).toBeInstanceOf(HTMLElement); + }); - expect(getByText("test")).toBeInTheDocument(); - expect(getByText("test2")).toBeInTheDocument(); + it("shows context switcher when deleting current cluster", async () => { + const cluster = new Cluster({ + id: "other-cluster", + contextName: "other-context", + preferences: { + clusterName: "other-cluster" + }, + kubeConfigPath: "./temp-kube-config", + }); + + DeleteClusterDialog.open({ cluster, config }); + + const { getByText } = render(); + + expect(getByText("Select...")).toBeInTheDocument(); + selectEvent.openMenu(getByText("Select...")); + + expect(getByText("test")).toBeInTheDocument(); + expect(getByText("test2")).toBeInTheDocument(); + }); + + it("shows context switcher after checkbox click", async () => { + const cluster = new Cluster({ + id: "some-cluster", + contextName: "test", + preferences: { + clusterName: "test" + }, + kubeConfigPath: "./temp-kube-config", + }); + + DeleteClusterDialog.open({ cluster, config }); + + const { getByText, getByTestId } = render(); + const link = getByTestId("context-switch"); + + expect(link).toBeInstanceOf(HTMLElement); + fireEvent.click(link); + + expect(getByText("Select...")).toBeInTheDocument(); + selectEvent.openMenu(getByText("Select...")); + + expect(getByText("test")).toBeInTheDocument(); + expect(getByText("test2")).toBeInTheDocument(); + }); + + it("shows warning for internal kubeconfig cluster", () => { + const cluster = new Cluster({ + id: "some-cluster", + contextName: "test", + preferences: { + clusterName: "test" + }, + kubeConfigPath: "./temp-kube-config", + }); + + const spy = jest.spyOn(cluster, "isInLocalKubeconfig").mockImplementation(() => true); + + DeleteClusterDialog.open({ cluster, config }); + + const { getByTestId } = render(); + + expect(getByTestId("internal-kubeconfig-warning")).toBeInstanceOf(HTMLElement); + + spy.mockRestore(); + }); + }); + + describe("Kubeconfig with single cluster", () => { + beforeEach(async () => { + const mockOpts = { + "temp-kube-config": singleClusterConfig, + }; + + mockFs(mockOpts); + + config = new KubeConfig(); + config.loadFromString(singleClusterConfig); + }); + + afterEach(() => { + mockFs.restore(); + }); + + it("shows warning if no other contexts left", () => { + const cluster = new Cluster({ + id: "other-cluster", + contextName: "other-context", + preferences: { + clusterName: "other-cluster" + }, + kubeConfigPath: "./temp-kube-config", + }); + + DeleteClusterDialog.open({ cluster, config }); + + const { getByTestId } = render(); + + expect(getByTestId("no-more-contexts-warning")).toBeInstanceOf(HTMLElement); + }); }); });