Skip to content

Commit

Permalink
Add passing tests for Switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ty2k committed Aug 27, 2024
1 parent a3d2c28 commit c43e32c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/react-components/src/components/Switch/Switch.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "@testing-library/jest-dom";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";

import Switch from "./Switch";

afterEach(() => {
cleanup();
});

describe("Switch", () => {
it("renders an HTML input component", () => {
render(<Switch>Flip me</Switch>);

const switchElements = screen.getAllByLabelText(/flip me/i);

expect(switchElements).toHaveLength(1);
expect(switchElements[0].tagName).toBe("INPUT");
expect(switchElements[0]).toHaveAccessibleName(/flip me/i);
});

it("onChange handler fires on click", () => {
const handleChange = jest.fn();
render(<Switch onChange={handleChange}>Power</Switch>);

const switchElement = screen.getByLabelText(/power/i);

fireEvent.click(switchElement);

expect(handleChange).toHaveBeenCalled();
});

it("defaultSelected adds checked attribute", () => {
render(<Switch defaultSelected>Standby</Switch>);

const switchElement = screen.getByLabelText(/standby/i);

expect(switchElement).toHaveAttribute("checked");
});
});

0 comments on commit c43e32c

Please sign in to comment.