From 6112c2c68e7f62a4ff6b0d05e9967f0271a79232 Mon Sep 17 00:00:00 2001 From: "Hung-Han (Henry) Chen" Date: Mon, 2 Nov 2020 14:12:48 +0800 Subject: [PATCH] Add basic tests for to ensure it initialises/renders without errors Signed-off-by: Hung-Han (Henry) Chen Signed-off-by: Hung-Han (Henry) Chen <1474479+chenhunghan@users.noreply.github.com> --- .../deployment-scale-dialog.test.tsx | 124 ++++++++++++++++++ .../deployment-scale-dialog.tsx | 6 +- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/renderer/components/+workloads-deployments/deployment-scale-dialog.test.tsx diff --git a/src/renderer/components/+workloads-deployments/deployment-scale-dialog.test.tsx b/src/renderer/components/+workloads-deployments/deployment-scale-dialog.test.tsx new file mode 100644 index 0000000000..261c38901c --- /dev/null +++ b/src/renderer/components/+workloads-deployments/deployment-scale-dialog.test.tsx @@ -0,0 +1,124 @@ +import React from 'react'; +import { render, waitFor } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect' + +import { DeploymentScaleDialog } from "./deployment-scale-dialog"; +jest.mock("../../api/endpoints"); +import { deploymentApi } from "../../api/endpoints"; + +const dummyDeployment = { + apiVersion: 'v1', + kind: 'dummy', + metadata: { + uid: 'dummy', + name: 'dummy', + creationTimestamp: 'dummy', + resourceVersion: 'dummy', + selfLink: 'link', + }, + selfLink: 'link', + spec: { + replicas: 1, + selector: { matchLabels: { dummy: 'label' } }, + template: { + metadata: { + labels: { dummy: 'label' }, + }, + spec: { + containers: [{ + name: 'dummy', + image: 'dummy', + resources: { + requests: { + cpu: '1', + memory: '10Mi', + }, + }, + terminationMessagePath: 'dummy', + terminationMessagePolicy: 'dummy', + imagePullPolicy: 'dummy', + }], + restartPolicy: 'dummy', + terminationGracePeriodSeconds: 10, + dnsPolicy: 'dummy', + serviceAccountName: 'dummy', + serviceAccount: 'dummy', + securityContext: {}, + schedulerName: 'dummy', + }, + }, + strategy: { + type: 'dummy', + rollingUpdate: { + maxUnavailable: 1, + maxSurge: 10, + }, + }, + }, + status: { + observedGeneration: 1, + replicas: 1, + updatedReplicas: 1, + readyReplicas: 1, + conditions: [{ + type: 'dummy', + status: 'dummy', + lastUpdateTime: 'dummy', + lastTransitionTime: 'dummy', + reason: 'dummy', + message: 'dummy', + }], + }, + getConditions: jest.fn(), + getConditionsText: jest.fn(), + getReplicas: 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(), + 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('', () => { + + it('renders w/o errors', () => { + const { container } = render(); + expect(container).toBeInstanceOf(HTMLElement); + }); + + it('inits with a dummy deployment with mocked current/desired scale', async () => { + // mock deploymentApi.getReplicas() which will be called + // when rendered. + const initReplicas = 3 + deploymentApi.getReplicas = jest.fn().mockImplementationOnce(async () => initReplicas); + const { getByTestId } = render(); + DeploymentScaleDialog.open(dummyDeployment); + let currentScale, desiredScale + // we need to wait for the DeploymentScaleDialog to show up + // because there is an in which renders null at start. + await waitFor(async () => + [currentScale, desiredScale] = await Promise.all([ + getByTestId('current-scale'), + getByTestId('desired-scale'), + ]) + ); + expect(currentScale).toHaveTextContent(`${initReplicas}`); + expect(desiredScale).toHaveTextContent(`${initReplicas}`); + }); + +}); + diff --git a/src/renderer/components/+workloads-deployments/deployment-scale-dialog.tsx b/src/renderer/components/+workloads-deployments/deployment-scale-dialog.tsx index d421f4692e..bf9dd89651 100644 --- a/src/renderer/components/+workloads-deployments/deployment-scale-dialog.tsx +++ b/src/renderer/components/+workloads-deployments/deployment-scale-dialog.tsx @@ -88,11 +88,11 @@ export class DeploymentScaleDialog extends Component { const warning = currentReplicas < 10 && desiredReplicas > 90; return ( <> -
+
Current replica scale: {currentReplicas}
-
+
Desired number of replicas: {desiredReplicas}
@@ -139,4 +139,4 @@ export class DeploymentScaleDialog extends Component {
); } -} \ No newline at end of file +}