import React from "react"; import { render, waitFor } from "@testing-library/react"; import "@testing-library/jest-dom"; import { CLASSPREFIX as eccgui } from "../../../configuration/constants"; import { CodeEditor } from "../../../extensions/codemirror/CodeMirror"; import FieldItem from "../FieldItem"; const renderFieldItem = (props: React.ComponentProps) => { const { container } = render(); const fieldItem = container.firstElementChild as HTMLElement; return { fieldItem, label: fieldItem.getElementsByClassName(`${eccgui}-fielditem__label`)[0] as HTMLElement, input: fieldItem.querySelector(`.${eccgui}-fielditem__inputfields input`) as HTMLElement, help: fieldItem.getElementsByClassName(`${eccgui}-fielditem__helpertext`)[0] as HTMLElement, message: fieldItem.getElementsByClassName(`${eccgui}-fielditem__message`)[0] as HTMLElement, }; }; describe("FieldItem", () => { describe("connection of its parts", () => { it("should set IDs for all parts that do not have one already", () => { const { label, input, help, message } = renderFieldItem({ labelProps: { text: "Label text" }, children: , helperText: "Helper text", messageText: "Message text", }); expect(label.id).toMatch(/^label_/); expect(input.id).toMatch(/^input_/); expect(help.id).toMatch(/^help_/); expect(message.id).toMatch(/^message_/); expect([label.id, input.id, help.id, message.id].map((id) => id.split("_")[1])).toEqual( new Array(4).fill(label.id.split("_")[1]), ); }); it("should create unique IDs for each field item", () => { const { container } = render( <> } /> } /> , ); const inputs = container.querySelectorAll("input"); expect(inputs[0].id).not.toBe(inputs[1].id); }); it("should connect label and input element via `for`", () => { const { label, input } = renderFieldItem({ labelProps: { text: "Label text" }, children: , }); expect(label.tagName).toBe("LABEL"); expect(label).toHaveAttribute("for", input.id); expect(input).not.toHaveAttribute("aria-labelledby"); }); it("should keep a `for` that refers to the ID of the input element", () => { const { label } = renderFieldItem({ labelProps: { text: "Label text", htmlFor: "custominput" }, children: , }); expect(label).toHaveAttribute("for", "custominput"); }); it("should replace a `for` that does not refer to the ID of the input element", () => { const { label, input } = renderFieldItem({ labelProps: { text: "Label text", htmlFor: "otherelement" }, children: , }); expect(input.id).toMatch(/^input_/); expect(label).toHaveAttribute("for", input.id); }); it("should keep a `for` that refers to another input element of the field item", () => { const { label } = renderFieldItem({ labelProps: { text: "Label text", htmlFor: "otherinput" }, children: ( <> ), }); expect(label).toHaveAttribute("for", "otherinput"); }); it("should replace a `for` that refers to an input element outside of the field item", () => { const { container } = render( <> , ); const label = container.getElementsByClassName(`${eccgui}-fielditem__label`)[0]; expect(label).toHaveAttribute("for", "custominput"); }); it("should replace a `for` that refers to an input element of a nested field item", () => { const { container } = render( , ); const outerLabel = container.getElementsByClassName(`${eccgui}-fielditem__label`)[0]; expect(outerLabel).toHaveAttribute("for", "outerinput"); }); it("should connect label and input element via `aria-labelledby` if the label is no `label` element", () => { const { label, input } = renderFieldItem({ disabled: true, labelProps: { text: "Label text" }, children: , }); expect(label.tagName).not.toBe("LABEL"); expect(input).toHaveAttribute("aria-labelledby", label.id); }); it("should connect helper text and message to the input element via `aria-describedby`", () => { const { input, help, message } = renderFieldItem({ children: , helperText: "Helper text", messageText: "Message text", }); expect(input).toHaveAttribute("aria-describedby", `${message.id} ${help.id}`); }); it("should only refer to existing parts via `aria-describedby`", () => { const { input, help } = renderFieldItem({ children: , helperText: "Helper text", }); expect(input).toHaveAttribute("aria-describedby", help.id); }); it("should not set `aria-describedby` if there is neither helper text nor message", () => { const { input } = renderFieldItem({ children: }); expect(input).not.toHaveAttribute("aria-describedby"); }); it("should keep IDs and connections that are set already", () => { const { label, help, message } = renderFieldItem({ labelProps: { text: "Label text", id: "customlabel", htmlFor: "custominput" }, children: , helperText: "Helper text", messageText: "Message text", }); const input = document.getElementById("custominput") as HTMLElement; expect(label).toHaveAttribute("id", "customlabel"); expect(label).toHaveAttribute("for", "custominput"); expect(input).toHaveAttribute("aria-describedby", `customhelp ${message.id} ${help.id}`); }); it("should not set IDs and connections if `preventAriaAttribution` is set", () => { const { label, input, help, message } = renderFieldItem({ preventAriaAttribution: true, labelProps: { text: "Label text" }, children: , helperText: "Helper text", messageText: "Message text", }); [label, input, help, message].forEach((element) => { expect(element.id).toBe(""); }); expect(label).not.toHaveAttribute("for"); expect(input).not.toHaveAttribute("aria-labelledby"); expect(input).not.toHaveAttribute("aria-describedby"); }); it("should also connect `textarea`, `select` and select target elements", () => { const inputElements = { textarea: