-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a test for IpaCheckbox component
The `IpaCheckbox` component must have a test to check its functionality. Signed-off-by: Carla Martinez <carlmart@redhat.com>
- Loading branch information
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}); | ||
}); |