Skip to content

Commit

Permalink
Create a test for IpaCheckbox component
Browse files Browse the repository at this point in the history
The `IpaCheckbox` component must have a test
to check its functionality.

Signed-off-by: Carla Martinez <carlmart@redhat.com>
  • Loading branch information
carma12 committed Dec 5, 2024
1 parent 4244444 commit 7db1260
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/components/Form/IpaCheckbox/IpaCheckbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import React from "react";
import { render, screen, fireEvent } from "@testing-library/react";
import "@testing-library/jest-dom";
// Component
import IpaCheckbox, { CheckboxOption } from "./IpaCheckbox";
// Utils
import { updateIpaObject } from "src/utils/ipaObjectUtils";

// Mock of util function: updateIpaObject
jest.mock("src/utils/ipaObjectUtils", () => ({
...jest.requireActual("src/utils/ipaObjectUtils.ts"),
updateIpaObject: jest.fn(),
}));

describe("IpaCheckbox Component", () => {
const mockOnChange = jest.fn();

const mockMetadata = {
objects: {
host: {
name: "host",
takes_params: [
{
alwaysask: false,
attribute: true,
autofill: false,
class: "Boolean",
cli_metavar: "IPAKRBOKASDELEGATE2",
cli_name: "ipakrbokasdelegate2",
confirm: false,
deprecated_cli_aliases: [],
deprecated: false,
doc: "Trusted for delegation.",
flags: [],
label: "Trusted for delegation",
maxlength: 255,
multivalue: false,
name: "ipakrbokasdelegate2",
no_convert: false,
noextrawhitespace: true,
pattern_errmsg: "",
pattern: "",
primary_key: false,
query: false,
required: false,
sortorder: 1,
type: "boolean",
},
],
},
},
};

const defaultProps: CheckboxOption = {
name: "ipakrbokasdelegate2",
ariaLabel: "ipakrbokasdelegate2",
ipaObject: {},
objectName: "host",
onChange: mockOnChange,
required: true,
readOnly: false,
metadata: mockMetadata,
value: "true",
text: "Trusted for delegation",
};

afterEach(() => {
jest.clearAllMocks();
});

it("renders the Checkbox with correct props", () => {
render(<IpaCheckbox {...defaultProps} />);

// Verify the Checkbox exist
const checkboxElem = screen.getByLabelText("ipakrbokasdelegate2");

expect(checkboxElem).toBeInTheDocument();
// As PatternFly doesn't provide a way to
// see if a given rendered Checkbox ('input') element is checked
// in the DOM tree, this needs to be done manually
checkboxElem.setAttribute("checked", "true");

expect(checkboxElem).toHaveAttribute("checked", "true");
expect(checkboxElem).toHaveAttribute("aria-label", "ipakrbokasdelegate2");
expect(checkboxElem).toHaveAttribute("name", "ipakrbokasdelegate2");
expect(checkboxElem).toBeEnabled();
});

it("disables the Checkbox if readOnly is true", () => {
render(<IpaCheckbox {...defaultProps} readOnly={true} />);

const checkboxElem = screen.getByLabelText("ipakrbokasdelegate2");
expect(checkboxElem).toBeDisabled();
});

it("toggles the Checkbox when clicking on it", () => {
// By default, it is checked
render(<IpaCheckbox {...defaultProps} />);

const checkboxElem = screen.getByText("Trusted for delegation");
// By default is checked. As PatternFly doesn't provide a way to
// see if a given rendered Checkbox ('input') element is checked
// in the DOM tree, this needs to be done manually
checkboxElem.setAttribute("checked", "true");

// Uncheck the Checkbox
fireEvent.click(checkboxElem);
checkboxElem.setAttribute("checked", "false");

expect(checkboxElem).not.toBeChecked();
});

it("calls updateIpaObject on change with the correct arguments", () => {
render(<IpaCheckbox {...defaultProps} />);

const checkboxElem = screen.getByLabelText("ipakrbokasdelegate2");

// Checkbox is clicked (to non-checked)
fireEvent.click(checkboxElem);

// Trigger the `onChange` by blurring
fireEvent.blur(checkboxElem);

// Verify that ùpdateIpaObject` is being called
expect(updateIpaObject).toHaveBeenCalledTimes(1);
expect(updateIpaObject).toHaveBeenCalledWith(
defaultProps.ipaObject,
mockOnChange,
expect.any(String),
"ipakrbokasdelegate2"
);
});
});

0 comments on commit 7db1260

Please sign in to comment.