Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Mar 23, 2023
1 parent bea5848 commit 27a8a84
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 18 deletions.
99 changes: 96 additions & 3 deletions web/src/client/storage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jest.mock("./dbus");

const cockpitProxies = {};

const cockpitCallbacks = {};

const contexts = {
withoutProposal: () => {
cockpitProxies.proposal = null;
Expand Down Expand Up @@ -105,6 +107,7 @@ const contexts = {

const mockProxy = (iface, path) => {
switch (iface) {
case "org.opensuse.DInstaller.Storage1": return cockpitProxies.storage;
case "org.opensuse.DInstaller.Storage1.Proposal": return cockpitProxies.proposal;
case "org.opensuse.DInstaller.Storage1.Proposal.Calculator": return cockpitProxies.proposalCalculator;
case "org.opensuse.DInstaller.Storage1.ISCSI.Initiator": return cockpitProxies.iscsiInitiator;
Expand All @@ -118,18 +121,98 @@ const mockProxies = (iface) => {
}
};

let client;
const mockOnObjectChanged = (path, iface, handler) => {
if (!cockpitCallbacks[path]) cockpitCallbacks[path] = {};
cockpitCallbacks[path][iface] = handler;
};

const emitSignal = (path, iface, data) => {
if (!cockpitCallbacks[path]) return;

const handler = cockpitCallbacks[path][iface];
if(!handler) return;

return handler(data);
};

beforeEach(() => {
// @ts-ignore
DBusClient.mockImplementation(() => {
return {
proxy: mockProxy,
proxies: mockProxies
proxies: mockProxies,
onObjectChanged: mockOnObjectChanged
};
});
});

let client;

describe("#probe", () => {
beforeEach(() => {
cockpitProxies.storage = {
Probe: jest.fn()
};

client = new StorageClient();
});

client = new StorageClient();
it("probes the system", async () => {
await client.probe();
expect(cockpitProxies.storage.Probe).toHaveBeenCalled();
});
});

describe("#isDeprecated", () => {
describe("if the system is not deprecated", () => {
beforeEach(() => {
cockpitProxies.storage = {
DeprecatedSystem: false
};

client = new StorageClient();
});

it("returns false", async () => {
const result = await client.isDeprecated();
expect(result).toEqual(false)
});
});
});

describe("#onDeprecate", () => {
const handler = jest.fn();

beforeEach(() => {
client = new StorageClient();
client.onDeprecate(handler);
});

describe("if the system was not deprecated", () => {
beforeEach(() => {
emitSignal(
"/org/opensuse/DInstaller/Storage1",
"org.opensuse.DInstaller.Storage1",
{});
});

it("does not run the handler", async () => {
expect(handler).not.toHaveBeenCalled();
});
});

describe("if the system was deprecated", () => {
beforeEach(() => {
emitSignal(
"/org/opensuse/DInstaller/Storage1",
"org.opensuse.DInstaller.Storage1",
{ DeprecatedSystem: true });
});

it("runs the handler", async () => {
expect(handler).not.toHaveBeenCalled();
});
});
});

describe("#proposal", () => {
Expand Down Expand Up @@ -170,6 +253,7 @@ describe("#proposal", () => {
beforeEach(() => {
contexts.withAvailableDevices();
contexts.withProposal();
client = new StorageClient();
});

it("returns the available devices and the proposal result", async () => {
Expand All @@ -182,6 +266,7 @@ describe("#proposal", () => {
describe("#getAvailableDevices", () => {
beforeEach(() => {
contexts.withAvailableDevices();
client = new StorageClient();
});

it("returns the list of available devices", async () => {
Expand All @@ -194,6 +279,7 @@ describe("#proposal", () => {
describe("if there is no proposal yet", () => {
beforeEach(() => {
contexts.withoutProposal();
client = new StorageClient();
});

it("returns undefined", async () => {
Expand All @@ -205,6 +291,7 @@ describe("#proposal", () => {
describe("if there is a proposal", () => {
beforeEach(() => {
contexts.withProposal();
client = new StorageClient();
});

it("returns the proposal settings and actions", async () => {
Expand All @@ -219,6 +306,8 @@ describe("#proposal", () => {
cockpitProxies.proposalCalculator = {
Calculate: jest.fn()
};

client = new StorageClient();
});

it("calculates a default proposal when no settings are given", async () => {
Expand Down Expand Up @@ -276,6 +365,10 @@ describe("#proposal", () => {
});

describe("#iscsi", () => {
beforeEach(() => {
client = new StorageClient();
});

describe("#getInitiatorName", () => {
beforeEach(() => {
cockpitProxies.iscsiInitiator = {
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/core/ValidationErrors.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
* Copyright (c) [2022] SUSE LLC
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/core/ValidationErrors.test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) [2022] SUSE LLC
* Copyright (c) [2022-2023] SUSE LLC
*
* All Rights Reserved.
*
Expand Down Expand Up @@ -43,7 +43,7 @@ describe("when there are multiple errors", () => {
];

const { user } = plainRender(<ValidationErrors title="Errors" errors={errors} />);
const button = await screen.findByRole("link", { name: "2 errors found" });
const button = await screen.findByRole("button", { name: "2 errors found" });
await user.click(button);

await waitFor(() => {
Expand Down
4 changes: 3 additions & 1 deletion web/src/components/overview/StorageSection.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ beforeEach(() => {
}),
onProgressChange: noop,
getValidationErrors: jest.fn().mockResolvedValue(errors),
onStatusChange: onStatusChangeFn
onStatusChange: onStatusChangeFn,
isDeprecated: jest.fn().mockResolvedValue(false),
onDeprecate: noop
},
};
});
Expand Down
5 changes: 4 additions & 1 deletion web/src/components/storage/ProposalPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import React from "react";
import { screen, waitForElementToBeRemoved } from "@testing-library/react";
import { installerRender, mockComponent } from "~/test-utils";
import { noop } from "~/utils";
import { createClient } from "~/client";
import { ProposalPage } from "~/components/storage";

Expand Down Expand Up @@ -50,7 +51,9 @@ beforeEach(() => {
getData: jest.fn().mockResolvedValue(proposal),
calculate: jest.fn().mockResolvedValue(0)
},
getValidationErrors: jest.fn().mockResolvedValue([])
getValidationErrors: jest.fn().mockResolvedValue([]),
isDeprecated: jest.fn().mockResolvedValue(false),
onDeprecate: noop
}
};
});
Expand Down
28 changes: 23 additions & 5 deletions web/src/components/storage/ProposalSummary.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,40 @@ describe("ProposalSummary", () => {
});

describe("when the proposal is calculated", () => {
it("renders the candidate device label", () => {
const proposal = {
let proposal;

beforeEach(() => {
proposal = {
result: {
candidateDevices: ["sdb"]
},
availableDevices: [
{ id: "sda", label: "/dev/sda" },
{ id: "sdb", label: "/dev/sdb" },
{ id: "sda", label: "/dev/sda 300 MiB" },
{ id: "sdb", label: "/dev/sdb 5 GiB" },
]
};
});

it("renders the candidate device label", () => {
installerRender(
<ProposalSummary proposal={proposal} />
);

screen.getByText("/dev/sdb");
screen.getByText("/dev/sdb 5 GiB");
});

describe("and the candidate device is missing", () => {
beforeEach(() => {
proposal.result.candidateDevices = ["sdc"];
});

it("renders the candidate device name", () => {
installerRender(
<ProposalSummary proposal={proposal} />
);

screen.getByText("sdc");
});
});
});
});
10 changes: 5 additions & 5 deletions web/src/components/storage/ProposalTargetForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import React from "react";

import { screen, within } from "@testing-library/react";
import { installerRender } from "~/test-utils";
import { plainRender } from "~/test-utils";
import { ProposalTargetForm } from "~/components/storage";

const proposal = {
Expand All @@ -38,7 +38,7 @@ const onSubmitFn = jest.fn();

describe("ProposalTargetForm", () => {
it("renders a selector for choosing candidate devices among available devices in given proposal", () => {
installerRender(
plainRender(
<ProposalTargetForm proposal={proposal} />
);

Expand All @@ -49,7 +49,7 @@ describe("ProposalTargetForm", () => {

describe("Selector for choosing candidate devices", () => {
it("gets its initial value from given proposal", () => {
installerRender(
plainRender(
<ProposalTargetForm proposal={proposal} />
);

Expand All @@ -58,7 +58,7 @@ describe("ProposalTargetForm", () => {
});

it("changes its value when user changes the selection", async () => {
const { user } = installerRender(
const { user } = plainRender(
<ProposalTargetForm proposal={proposal} />
);

Expand Down Expand Up @@ -88,7 +88,7 @@ describe("ProposalTargetForm", () => {
);
};

const { user } = installerRender(<FormWrapper />);
const { user } = plainRender(<FormWrapper />);

const deviceSelector = screen.getByRole("combobox");
const sdbOption = within(deviceSelector).getByRole("option", { name: "/dev/sdb, 650 GiB" });
Expand Down

0 comments on commit 27a8a84

Please sign in to comment.