/* * Copyright 2016 Palantir Technologies, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { render, screen } from "@testing-library/react"; import { describe, expect, it } from "@blueprintjs/test-commons/vitest"; import { Classes } from "../../common"; import { Checkbox, Radio, Switch } from "./controls"; describe("Controls", () => { describe("Checkbox", () => { it("should render", () => { render(); const checkbox = screen.getByRole("checkbox"); const control = checkbox.closest(`.${Classes.CONTROL}`); expect(checkbox).to.exist; expect(control).to.exist; expect(checkbox.getAttribute("type")).to.equal("checkbox"); expect([...control!.classList]).to.include(Classes.CHECKBOX); }); it("should support JSX children", () => { render( Label Text , ); expect(screen.getByText("Label Text")).to.exist; }); it("should support JSX labelElement", () => { render(Label Text} />); expect(screen.getByText("Label Text")).to.exist; }); it("should support indeterminate state", () => { render(); expect(screen.getByRole("checkbox").indeterminate).to.be.true; }); it("should support defaultIndeterminate state", () => { render(); expect(screen.getByRole("checkbox").indeterminate).to.be.true; }); }); describe("Radio", () => { it("should render", () => { render(); const radio = screen.getByRole("radio"); const control = radio.closest(`.${Classes.CONTROL}`); expect(radio).to.exist; expect(control).to.exist; expect(radio.getAttribute("type")).to.equal("radio"); expect([...control!.classList]).to.include(Classes.RADIO); }); it("should support JSX children", () => { render( Label Text , ); expect(screen.getByText("Label Text")).to.exist; }); it("should support JSX labelElement", () => { render(Label Text} />); expect(screen.getByText("Label Text")).to.exist; }); }); describe("Switch", () => { it("should render", () => { render(); const checkbox = screen.getByRole("checkbox"); const control = checkbox.closest(`.${Classes.CONTROL}`); expect(checkbox).to.exist; expect(control).to.exist; expect(checkbox.getAttribute("type")).to.equal("checkbox"); expect([...control!.classList]).to.include(Classes.SWITCH); }); it("should support JSX children", () => { render( Label Text , ); expect(screen.getByText("Label Text")).to.exist; }); it("should support JSX labelElement", () => { render(Label Text} />); expect(screen.getByText("Label Text")).to.exist; }); it("should render both innerLabels when both defined", () => { render(); expect(screen.getByText("checked")).to.exist; expect(screen.getByText("unchecked")).to.exist; }); it("should not render innerLabel components when neither defined", () => { render(); expect(screen.queryByText("checked")).not.toBeInTheDocument(); expect(screen.queryByText("unchecked")).not.toBeInTheDocument(); }); it("should render innerLabel when innerLabelChecked is defined", () => { render(); const labels = screen.getAllByText("checked"); expect(labels).to.have.length(1); }); it("should render two innerLabels when innerLabel is defined and innerLabelChecked is not", () => { render(); const labels = screen.getAllByText("test"); expect(labels).to.have.length(2); }); }); });