-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from yaswanth-deriv/yaswanth/FEQ-1980_Added_t…
…est_for_toogleswitch_component [FEQ]Yaswanth/FEQ-1980 Added test cases for toggle switch component
- Loading branch information
Showing
3 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/components/ToggleSwitch/__test__/ToggleSwitch.spec.tsx
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,62 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import { ToggleSwitch } from ".."; | ||
import userEvent from "@testing-library/user-event"; | ||
|
||
describe("ToggleSwitch Component", () => { | ||
it("should renders with correct value passed as prop", () => { | ||
const { getByRole, rerender } = render(<ToggleSwitch value={true} />); | ||
const toggleSwitch = getByRole("checkbox"); | ||
expect(toggleSwitch).toBeChecked(); | ||
rerender(<ToggleSwitch value={false} />); | ||
expect(toggleSwitch).not.toBeChecked(); | ||
}); | ||
|
||
it("should be set to false when no value prop is provided", () => { | ||
const { getByRole } = render(<ToggleSwitch/>); | ||
const toggleSwitch = getByRole("checkbox"); | ||
expect(toggleSwitch).not.toBeChecked(); | ||
}); | ||
it("should update the value when user clicks on it", async () => { | ||
const { getByRole } = render(<ToggleSwitch/>); | ||
const toggleSwitch = getByRole("checkbox"); | ||
expect(toggleSwitch).not.toBeChecked(); | ||
await userEvent.click(toggleSwitch); | ||
expect(toggleSwitch).toBeChecked(); | ||
}); | ||
|
||
it("should call the onChange prop when user clicks on it", async () => { | ||
const mockedOnChange = jest.fn(); | ||
const { getByRole } = render(<ToggleSwitch onChange={mockedOnChange} />); | ||
const toggleSwitch = getByRole("checkbox"); | ||
await userEvent.click(toggleSwitch); | ||
expect(mockedOnChange).toHaveBeenCalled(); | ||
}); | ||
|
||
|
||
it("should apply wrapperClassName and className correctly", () => { | ||
const { container } = render( | ||
<ToggleSwitch | ||
wrapperClassName="wrapper-class" | ||
className="custom-class" | ||
/> | ||
); | ||
const wrapper = container.querySelector(".deriv-toggle-switch.wrapper-class"); | ||
expect(wrapper).toBeInTheDocument(); | ||
const input = wrapper?.querySelector("input"); | ||
expect(input).toHaveClass("custom-class"); | ||
}); | ||
|
||
it("applies wrapperStyle and style correctly", () => { | ||
const { container } = render( | ||
<ToggleSwitch | ||
wrapperStyle={{ backgroundColor: "red" }} | ||
style={{ fontSize: "16px" }} | ||
/> | ||
); | ||
const input = container.querySelector(".deriv-toggle-switch input"); | ||
expect(input).toHaveStyle({ fontSize: "16px" }); | ||
const label = input?.parentElement; | ||
expect(label).toHaveStyle({ backgroundColor: "red" }); | ||
}); | ||
}); |
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
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