From 3b5f39ea2a3af90f39c73775d3e125372f26332c Mon Sep 17 00:00:00 2001 From: Knut Anderssen Date: Thu, 12 Dec 2024 17:30:21 +0000 Subject: [PATCH] Fix some unit tests --- .../overview/StorageSection.test.tsx | 27 +++++++-------- .../components/overview/StorageSection.tsx | 3 +- .../ProposalTransactionalInfo.test.tsx | 33 +++++-------------- .../storage/ProposalTransactionalInfo.tsx | 1 - 4 files changed, 25 insertions(+), 39 deletions(-) diff --git a/web/src/components/overview/StorageSection.test.tsx b/web/src/components/overview/StorageSection.test.tsx index bc85cd1fdd..811f83d0e2 100644 --- a/web/src/components/overview/StorageSection.test.tsx +++ b/web/src/components/overview/StorageSection.test.tsx @@ -24,24 +24,25 @@ import React from "react"; import { screen } from "@testing-library/react"; import { plainRender } from "~/test-utils"; import { StorageSection } from "~/components/overview"; -import * as ConfigModel from "~/storage/model/config"; +import * as ConfigModel from "~/api/storage/types/config-model"; const mockDevices = [ - { name: "/dev/sda", size: 536870912000 }, - { name: "/dev/sdb", size: 697932185600 }, + { name: "/dev/sda", size: 536870912000, volumeGroups: [] }, + { name: "/dev/sdb", size: 697932185600, volumeGroups: [] }, ]; -const mockConfig = { devices: [] as ConfigModel.Device[] }; +const mockConfig = { drives: [] as ConfigModel.Drive[] }; jest.mock("~/queries/storage", () => ({ ...jest.requireActual("~/queries/storage"), + useConfigModel: () => mockConfig, useDevices: () => mockDevices, - useConfigDevices: () => mockConfig.devices, + useConfigDevices: () => mockConfig.drives, })); describe("when the configuration does not include any device", () => { beforeEach(() => { - mockConfig.devices = []; + mockConfig.drives = []; }); it("indicates that a device is not selected", async () => { @@ -53,7 +54,7 @@ describe("when the configuration does not include any device", () => { describe("when the configuration contains one drive", () => { beforeEach(() => { - mockConfig.devices = [{ name: "/dev/sda", spacePolicy: "delete" }]; + mockConfig.drives = [{ name: "/dev/sda", spacePolicy: "delete", volumeGroups: [] }]; }); it("renders the proposal summary", async () => { @@ -66,7 +67,7 @@ describe("when the configuration contains one drive", () => { describe("and the space policy is set to 'resize'", () => { beforeEach(() => { - mockConfig.devices[0].spacePolicy = "resize"; + mockConfig.drives[0].spacePolicy = "resize"; }); it("indicates that partitions may be shrunk", async () => { @@ -78,7 +79,7 @@ describe("when the configuration contains one drive", () => { describe("and the space policy is set to 'keep'", () => { beforeEach(() => { - mockConfig.devices[0].spacePolicy = "keep"; + mockConfig.drives[0].spacePolicy = "keep"; }); it("indicates that partitions will be kept", async () => { @@ -90,7 +91,7 @@ describe("when the configuration contains one drive", () => { describe("and the drive matches no disk", () => { beforeEach(() => { - mockConfig.devices[0].name = null; + mockConfig.drives[0].name = null; }); it("indicates that a device is not selected", async () => { @@ -103,9 +104,9 @@ describe("when the configuration contains one drive", () => { describe("when the configuration contains several drives", () => { beforeEach(() => { - mockConfig.devices = [ - { name: "/dev/sda", spacePolicy: "delete" }, - { name: "/dev/sdb", spacePolicy: "delete" }, + mockConfig.drives = [ + { name: "/dev/sda", spacePolicy: "delete", volumeGroups: [] }, + { name: "/dev/sdb", spacePolicy: "delete", volumeGroups: [] }, ]; }); diff --git a/web/src/components/overview/StorageSection.tsx b/web/src/components/overview/StorageSection.tsx index b0c0b6082a..37ad6215fd 100644 --- a/web/src/components/overview/StorageSection.tsx +++ b/web/src/components/overview/StorageSection.tsx @@ -90,7 +90,8 @@ export default function StorageSection() { }; if (drives.length === 0) return {_("No device selected yet")}; - + if (!drives.find((dr) => devices.map((d) => d.name).includes(dr.name))) + return {_("No device selected yet")}; if (drives.length > 1) { return ( diff --git a/web/src/components/storage/ProposalTransactionalInfo.test.tsx b/web/src/components/storage/ProposalTransactionalInfo.test.tsx index ebcd2c93b2..19833b341b 100644 --- a/web/src/components/storage/ProposalTransactionalInfo.test.tsx +++ b/web/src/components/storage/ProposalTransactionalInfo.test.tsx @@ -26,6 +26,7 @@ import { plainRender } from "~/test-utils"; import { ProposalTransactionalInfo } from "~/components/storage"; import { ProposalSettings, ProposalTarget, Volume, VolumeTarget } from "~/types/storage"; +let mockVolumes: Volume[] = []; jest.mock("~/queries/software", () => ({ ...jest.requireActual("~/queries/software"), useProduct: () => ({ @@ -34,20 +35,10 @@ jest.mock("~/queries/software", () => ({ useProductChanges: () => jest.fn(), })); -const settings: ProposalSettings = { - target: ProposalTarget.DISK, - targetDevice: "/dev/sda", - targetPVDevices: [], - configureBoot: false, - bootDevice: "", - defaultBootDevice: "", - encryptionPassword: "", - encryptionMethod: "", - spacePolicy: "delete", - spaceActions: [], - volumes: [], - installationDevices: [], -}; +jest.mock("~/queries/storage", () => ({ + ...jest.requireActual("~/queries/storage"), + useVolumeTemplates: () => mockVolumes, +})); const rootVolume: Volume = { mountPath: "/", @@ -70,30 +61,24 @@ const rootVolume: Volume = { }, }; -const props = { settings }; - -beforeEach(() => { - settings.volumes = []; -}); - describe("if the system is not transactional", () => { beforeEach(() => { - settings.volumes = [rootVolume]; + mockVolumes = [rootVolume]; }); it("renders nothing", () => { - const { container } = plainRender(); + const { container } = plainRender(); expect(container).toBeEmptyDOMElement(); }); }); describe("if the system is transactional", () => { beforeEach(() => { - settings.volumes = [{ ...rootVolume, transactional: true }]; + mockVolumes = [{ ...rootVolume, transactional: true }]; }); it("renders an explanation about the transactional system", () => { - plainRender(); + plainRender(); screen.getByText("Transactional root file system"); }); diff --git a/web/src/components/storage/ProposalTransactionalInfo.tsx b/web/src/components/storage/ProposalTransactionalInfo.tsx index f4ed0b582e..2a5d3e9321 100644 --- a/web/src/components/storage/ProposalTransactionalInfo.tsx +++ b/web/src/components/storage/ProposalTransactionalInfo.tsx @@ -33,7 +33,6 @@ import { isTransactionalSystem } from "~/components/storage/utils"; * @component * * @param props - * @param props.settings - Settings used for calculating a proposal. */ export default function ProposalTransactionalInfo() { const { selectedProduct } = useProduct({ suspense: true });