1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/+workloads-replicasets/replicaset-scale-dialog.test.tsx
2021-02-12 15:39:05 +02:00

168 lines
5.3 KiB
TypeScript
Executable File

import "@testing-library/jest-dom/extend-expect";
jest.mock("../../api/endpoints");
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";
const dummyReplicaSet: ReplicaSet = {
apiVersion: "v1",
kind: "dummy",
metadata: {
uid: "dummy",
name: "dummy",
creationTimestamp: "dummy",
resourceVersion: "dummy",
selfLink: "link",
},
selfLink: "link",
spec: {
replicas: 1,
selector: {
matchLabels: { "label": "label" }
},
template: {
metadata: {
labels: {
app: "label",
},
},
spec: {
containers: [{
name: "dummy",
image: "dummy",
imagePullPolicy: "dummy",
}],
initContainers: [{
name: "dummy",
image: "dummy",
imagePullPolicy: "dummy",
}],
priority: 1,
serviceAccountName: "dummy",
serviceAccount: "dummy",
securityContext: {},
schedulerName: "dummy",
},
},
minReadySeconds: 1,
},
status: {
replicas: 1,
fullyLabeledReplicas: 1,
readyReplicas: 1,
availableReplicas: 1,
observedGeneration: 1,
conditions: [{
type: "dummy",
status: "dummy",
lastUpdateTime: "dummy",
lastTransitionTime: "dummy",
reason: "dummy",
message: "dummy",
}],
},
getDesired: jest.fn(),
getCurrent: jest.fn(),
getReady: jest.fn(),
getImages: jest.fn(),
getSelectors: jest.fn(),
getTemplateLabels: jest.fn(),
getAffinity: jest.fn(),
getTolerations: jest.fn(),
getNodeSelectors: jest.fn(),
getAffinityNumber: jest.fn(),
getId: jest.fn(),
getResourceVersion: jest.fn(),
getName: jest.fn(),
getNs: jest.fn(),
getAge: jest.fn(),
getTimeDiffFromNow: jest.fn(),
getFinalizers: jest.fn(),
getLabels: jest.fn(),
getAnnotations: jest.fn(),
getOwnerRefs: jest.fn(),
getSearchFields: jest.fn(),
toPlainObject: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
};
describe("<ReplicaSetScaleDialog />", () => {
it("renders w/o errors", () => {
const { container } = render(<ReplicaSetScaleDialog/>);
expect(container).toBeInstanceOf(HTMLElement);
});
it("init with a dummy replica set and mocked current/desired scale", async () => {
// mock replicaSetApi.getReplicas() which will be called
// when <ReplicaSetScaleDialog /> rendered.
const initReplicas = 1;
replicaSetApi.getReplicas = jest.fn().mockImplementationOnce(async () => initReplicas);
const { getByTestId } = render(<ReplicaSetScaleDialog/>);
ReplicaSetScaleDialog.open(dummyReplicaSet);
// we need to wait for the replicaSetScaleDialog to show up
// because there is an <Animate /> in <Dialog /> which renders null at start.
await waitFor(async () => {
const [currentScale, desiredScale] = await Promise.all([
getByTestId("current-scale"),
getByTestId("desired-scale"),
]);
expect(currentScale).toHaveTextContent(`${initReplicas}`);
expect(desiredScale).toHaveTextContent(`${initReplicas}`);
});
});
it("changes the desired scale when clicking the icon buttons +/-", async () => {
const initReplicas = 1;
replicaSetApi.getReplicas = jest.fn().mockImplementationOnce(async () => initReplicas);
const component = render(<ReplicaSetScaleDialog/>);
ReplicaSetScaleDialog.open(dummyReplicaSet);
await waitFor(async () => {
expect(await component.findByTestId("desired-scale")).toHaveTextContent(`${initReplicas}`);
expect(await component.findByTestId("current-scale")).toHaveTextContent(`${initReplicas}`);
expect((await component.baseElement.querySelector("input").value)).toBe(`${initReplicas}`);
});
const up = await component.findByTestId("desired-replicas-up");
const down = await component.findByTestId("desired-replicas-down");
fireEvent.click(up);
expect(await component.findByTestId("desired-scale")).toHaveTextContent(`${initReplicas + 1}`);
expect(await component.findByTestId("current-scale")).toHaveTextContent(`${initReplicas}`);
expect((await component.baseElement.querySelector("input").value)).toBe(`${initReplicas + 1}`);
fireEvent.click(down);
expect(await component.findByTestId("desired-scale")).toHaveTextContent(`${initReplicas}`);
expect(await component.findByTestId("current-scale")).toHaveTextContent(`${initReplicas}`);
expect((await component.baseElement.querySelector("input").value)).toBe(`${initReplicas}`);
// edge case, desiredScale must >= 0
let times = 10;
for (let i = 0; i < times; i++) {
fireEvent.click(down);
}
expect(await component.findByTestId("desired-scale")).toHaveTextContent("0");
expect((await component.baseElement.querySelector("input").value)).toBe("0");
// edge case, desiredScale must <= scaleMax (100)
times = 120;
for (let i = 0; i < times; i++) {
fireEvent.click(up);
}
expect(await component.findByTestId("desired-scale")).toHaveTextContent("100");
expect((component.baseElement.querySelector("input").value)).toBe("100");
expect(await component.findByTestId("warning"))
.toHaveTextContent("High number of replicas may cause cluster performance issues");
});
});