Skip to content

Commit

Permalink
Fix some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Dec 12, 2024
1 parent 9920e80 commit 3b5f39e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 39 deletions.
27 changes: 14 additions & 13 deletions web/src/components/overview/StorageSection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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: [] },
];
});

Expand Down
3 changes: 2 additions & 1 deletion web/src/components/overview/StorageSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export default function StorageSection() {
};

if (drives.length === 0) return <Text>{_("No device selected yet")}</Text>;

if (!drives.find((dr) => devices.map((d) => d.name).includes(dr.name)))
return <Text>{_("No device selected yet")}</Text>;
if (drives.length > 1) {
return (
<Content>
Expand Down
33 changes: 9 additions & 24 deletions web/src/components/storage/ProposalTransactionalInfo.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { plainRender } from "~/test-utils";
import { ProposalTransactionalInfo } from "~/components/storage";
import { ProposalSettings, ProposalTarget, Volume, VolumeTarget } from "~/types/storage";

Check failure on line 27 in web/src/components/storage/ProposalTransactionalInfo.test.tsx

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

'ProposalSettings' is defined but never used

Check failure on line 27 in web/src/components/storage/ProposalTransactionalInfo.test.tsx

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

'ProposalTarget' is defined but never used

let mockVolumes: Volume[] = [];
jest.mock("~/queries/software", () => ({
...jest.requireActual("~/queries/software"),
useProduct: () => ({
Expand All @@ -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: "/",
Expand All @@ -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(<ProposalTransactionalInfo {...props} />);
const { container } = plainRender(<ProposalTransactionalInfo />);
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(<ProposalTransactionalInfo {...props} />);
plainRender(<ProposalTransactionalInfo />);

screen.getByText("Transactional root file system");
});
Expand Down
1 change: 0 additions & 1 deletion web/src/components/storage/ProposalTransactionalInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 3b5f39e

Please sign in to comment.