Skip to content

Commit

Permalink
[web] Add tests for ProposalSettingsSection
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 2, 2022
1 parent ca8ef9f commit 35416c4
Showing 1 changed file with 126 additions and 0 deletions.
126 changes: 126 additions & 0 deletions web/src/components/storage/ProposalSettingsSection.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) [2022] SUSE LLC
*
* All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, contact SUSE LLC.
*
* To contact SUSE LLC about this file by physical or electronic mail, you may
* find current contact information at www.suse.com.
*/

import React from "react";
import { screen, waitFor, within } from "@testing-library/react";
import { installerRender } from "@/test-utils";
import { ProposalSettingsSection } from "@components/storage";

const FakeProposalSettingsForm = ({ id, onSubmit }) => {
const accept = (e) => {
e.preventDefault();
onSubmit({});
};

return <form id={id} onSubmit={accept} aria-label="Settings form" />;
}

jest.mock("@components/storage/ProposalSettingsForm", () => FakeProposalSettingsForm);

let candidateDevices = ["/dev/sda"];
let encryptionPassword = "";
let lvm = false;
let volumes = [{ mountPoint: "/test1" }, { mountPoint: "/test2" }];

const proposal = {
candidateDevices,
encryptionPassword,
lvm,
volumes
};

it("renders the list of the volumes to create", () => {
installerRender(<ProposalSettingsSection proposal={proposal} />);

screen.getByText(/Create the following file systems/);
screen.getByText("/test1");
screen.getByText("/test2");
});

it("renders an icon for configuring the settings", () => {
installerRender(<ProposalSettingsSection proposal={proposal} />);

screen.getByRole("button", { name: "Settings section action icon" });
});

it("does not show the popup by default", async () => {
installerRender(<ProposalSettingsSection proposal={proposal} />);

await waitFor(() => {
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
});

it("shows the popup with the form when the icon is clicked", async () => {
const { user } = installerRender(<ProposalSettingsSection proposal={proposal} />);

const button = screen.getByRole("button", { name: "Settings section action icon" });
await user.click(button);

await screen.findByRole("dialog");
screen.getByRole("form", { name: "Settings form" });
});

it("closes the popup without submitting the form when cancel is clicked", async () => {
const calculateFn = jest.fn();

const { user } = installerRender(<ProposalSettingsSection proposal={proposal} calculateProposal={calculateFn} />);

const button = screen.getByRole("button", { name: "Settings section action icon" });
await user.click(button);

const popup = await screen.findByRole("dialog");
const cancel = within(popup).getByRole("button", { name: "Cancel" });
await user.click(cancel);

expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(calculateFn).not.toHaveBeenCalled();
});

it("closes the popup and submits the form when accept is clicked", async () => {
const calculateFn = jest.fn();

const { user } = installerRender(<ProposalSettingsSection proposal={proposal} calculateProposal={calculateFn} />);

const button = screen.getByRole("button", { name: "Settings section action icon" });
await user.click(button);

const popup = await screen.findByRole("dialog");
const accept = within(popup).getByRole("button", { name: "Accept" });
await user.click(accept);

expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(calculateFn).toHaveBeenCalled();
});

describe("when lvm and encryption are not selected", () => {
beforeEach(() => {
lvm = false;
encryptionPassword = "";
});

it("renders the proper description for the selected settings", () => {
installerRender(<ProposalSettingsSection proposal={proposal} />);

screen.getByText(/Create file systems over partitions/);
});
});

0 comments on commit 35416c4

Please sign in to comment.