|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import { createChangeHandler } from "./common.test"; |
| 4 | +import { RadioGroup } from "./RadioGroup"; |
| 5 | + |
| 6 | +describe("RadioGroup", () => { |
| 7 | + it("should render the component", () => { |
| 8 | + render( |
| 9 | + <RadioGroup |
| 10 | + id="rg" |
| 11 | + type={"RadioGroup"} |
| 12 | + label={"Gender"} |
| 13 | + value={"f"} |
| 14 | + children={[ |
| 15 | + { type: "Radio", value: "f", label: "Female" }, |
| 16 | + { type: "Radio", value: "m", label: "Male" }, |
| 17 | + { type: "Radio", value: "v", label: "Varying" }, |
| 18 | + ]} |
| 19 | + onChange={() => {}} |
| 20 | + row |
| 21 | + dense |
| 22 | + />, |
| 23 | + ); |
| 24 | + // to inspect rendered component, do: |
| 25 | + // expect(document.querySelector("#rg")).toEqual({}); |
| 26 | + expect(screen.getByRole("radiogroup")).not.toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should fire 'value' property with text options", () => { |
| 30 | + const { recordedEvents, onChange } = createChangeHandler(); |
| 31 | + render( |
| 32 | + <RadioGroup |
| 33 | + id="rg" |
| 34 | + type={"RadioGroup"} |
| 35 | + label={"Gender"} |
| 36 | + value={"m"} |
| 37 | + children={[ |
| 38 | + { type: "Radio", value: "f", label: "Female" }, |
| 39 | + { type: "Radio", value: "m", label: "Male" }, |
| 40 | + { type: "Radio", value: "v", label: "Varying" }, |
| 41 | + ]} |
| 42 | + onChange={onChange} |
| 43 | + />, |
| 44 | + ); |
| 45 | + fireEvent.click(screen.getByLabelText("Varying")); |
| 46 | + expect(recordedEvents.length).toBe(1); |
| 47 | + expect(recordedEvents[0]).toEqual({ |
| 48 | + componentType: "RadioGroup", |
| 49 | + id: "rg", |
| 50 | + property: "value", |
| 51 | + value: "v", |
| 52 | + }); |
| 53 | + fireEvent.click(screen.getByLabelText("Female")); |
| 54 | + expect(recordedEvents.length).toBe(2); |
| 55 | + expect(recordedEvents[1]).toEqual({ |
| 56 | + componentType: "RadioGroup", |
| 57 | + id: "rg", |
| 58 | + property: "value", |
| 59 | + value: "f", |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments