|
1 | 1 | import React from "react"; |
2 | | -import { render, waitFor } from "@testing-library/react"; |
| 2 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 3 | +import userEvent from "@testing-library/user-event"; |
| 4 | +import { createPortal } from "react-dom"; |
3 | 5 |
|
4 | 6 | import "@testing-library/jest-dom"; |
5 | 7 |
|
@@ -401,6 +403,150 @@ describe("FieldItem", () => { |
401 | 403 | }); |
402 | 404 | }); |
403 | 405 |
|
| 406 | +describe("FieldItem label activation", () => { |
| 407 | + const renderCombobox = (props: React.ComponentProps<typeof FieldItem> = {}) => |
| 408 | + renderFieldItem({ |
| 409 | + labelProps: { text: "Type" }, |
| 410 | + children: ( |
| 411 | + <div className={`${eccgui}-select`} role="combobox" tabIndex={0}> |
| 412 | + <button type="button" tabIndex={-1}> |
| 413 | + Value |
| 414 | + </button> |
| 415 | + </div> |
| 416 | + ), |
| 417 | + ...props, |
| 418 | + }); |
| 419 | + |
| 420 | + it("focuses the combobox when clicking its label", async () => { |
| 421 | + const user = userEvent.setup(); |
| 422 | + renderCombobox(); |
| 423 | + |
| 424 | + await user.click(screen.getByText("Type")); |
| 425 | + |
| 426 | + expect(screen.getByRole("combobox", { name: "Type" })).toHaveFocus(); |
| 427 | + }); |
| 428 | + |
| 429 | + it("focuses an input when its label is rendered as a span", async () => { |
| 430 | + const user = userEvent.setup(); |
| 431 | + renderFieldItem({ |
| 432 | + labelProps: { text: "Name", isLayoutForElement: "span" }, |
| 433 | + children: <input />, |
| 434 | + }); |
| 435 | + |
| 436 | + await user.click(screen.getByText("Name")); |
| 437 | + |
| 438 | + expect(screen.getByRole("textbox", { name: "Name" })).toHaveFocus(); |
| 439 | + }); |
| 440 | + |
| 441 | + it("focuses the control inside a focusable ancestor", async () => { |
| 442 | + const user = userEvent.setup(); |
| 443 | + render( |
| 444 | + <div tabIndex={-1}> |
| 445 | + <FieldItem labelProps={{ text: "Type" }}> |
| 446 | + <div className={`${eccgui}-select`} role="combobox" tabIndex={0}> |
| 447 | + Value |
| 448 | + </div> |
| 449 | + </FieldItem> |
| 450 | + </div>, |
| 451 | + ); |
| 452 | + |
| 453 | + await user.click(screen.getByText("Type")); |
| 454 | + |
| 455 | + expect(screen.getByRole("combobox")).toHaveFocus(); |
| 456 | + }); |
| 457 | + |
| 458 | + it("preserves native activation of an explicitly labelled second input", async () => { |
| 459 | + const user = userEvent.setup(); |
| 460 | + const { input } = renderFieldItem({ |
| 461 | + labelProps: { text: "Second", htmlFor: "second" }, |
| 462 | + children: ( |
| 463 | + <> |
| 464 | + <input id="first" /> |
| 465 | + <input id="second" /> |
| 466 | + </> |
| 467 | + ), |
| 468 | + }); |
| 469 | + const focusFirst = jest.spyOn(input, "focus"); |
| 470 | + |
| 471 | + await user.click(screen.getByText("Second")); |
| 472 | + |
| 473 | + expect(screen.getByRole("textbox", { name: "Second" })).toHaveFocus(); |
| 474 | + expect(focusFirst).not.toHaveBeenCalled(); |
| 475 | + }); |
| 476 | + |
| 477 | + it.each([false, true])("respects a label click handler with preventDefault=%s", async (cancel) => { |
| 478 | + const user = userEvent.setup(); |
| 479 | + const onClick = jest.fn((event: React.MouseEvent<HTMLLabelElement>) => { |
| 480 | + if (cancel) event.preventDefault(); |
| 481 | + }); |
| 482 | + renderCombobox({ labelProps: { text: "Type", onClick } }); |
| 483 | + |
| 484 | + await user.click(screen.getByText("Type")); |
| 485 | + |
| 486 | + expect(onClick).toHaveBeenCalledTimes(1); |
| 487 | + if (cancel) { |
| 488 | + expect(screen.getByRole("combobox")).not.toHaveFocus(); |
| 489 | + } else { |
| 490 | + expect(screen.getByRole("combobox")).toHaveFocus(); |
| 491 | + } |
| 492 | + }); |
| 493 | + |
| 494 | + it.each(["link", "button", "focusable text"])("preserves clicks on a %s inside the label", async (kind) => { |
| 495 | + const user = userEvent.setup(); |
| 496 | + const help = |
| 497 | + kind === "link" ? ( |
| 498 | + <a href="#field-help">Help</a> |
| 499 | + ) : kind === "button" ? ( |
| 500 | + <button type="button">Help</button> |
| 501 | + ) : ( |
| 502 | + <span tabIndex={0}>Help</span> |
| 503 | + ); |
| 504 | + renderCombobox({ labelProps: { text: "Type", additionalElements: help } }); |
| 505 | + const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus"); |
| 506 | + |
| 507 | + await user.click(screen.getByText("Help")); |
| 508 | + |
| 509 | + expect(focusControl).not.toHaveBeenCalled(); |
| 510 | + expect(screen.getByText("Help")).toHaveFocus(); |
| 511 | + }); |
| 512 | + |
| 513 | + it("preserves clicks on portaled label content", async () => { |
| 514 | + const user = userEvent.setup(); |
| 515 | + renderCombobox({ |
| 516 | + labelProps: { |
| 517 | + text: "Type", |
| 518 | + additionalElements: createPortal(<button type="button">Help</button>, document.body), |
| 519 | + }, |
| 520 | + }); |
| 521 | + const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus"); |
| 522 | + |
| 523 | + await user.click(screen.getByText("Help")); |
| 524 | + |
| 525 | + expect(focusControl).not.toHaveBeenCalled(); |
| 526 | + expect(screen.getByText("Help")).toHaveFocus(); |
| 527 | + }); |
| 528 | + |
| 529 | + it("does not focus the control of a disabled field", async () => { |
| 530 | + const user = userEvent.setup(); |
| 531 | + renderCombobox({ disabled: true }); |
| 532 | + const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus"); |
| 533 | + |
| 534 | + await user.click(screen.getByText("Type")); |
| 535 | + |
| 536 | + expect(focusControl).not.toHaveBeenCalled(); |
| 537 | + }); |
| 538 | + |
| 539 | + it("does not add focus behavior when automatic connections are disabled", async () => { |
| 540 | + const user = userEvent.setup(); |
| 541 | + renderCombobox({ preventAriaAttribution: true }); |
| 542 | + const focusControl = jest.spyOn(screen.getByRole("combobox"), "focus"); |
| 543 | + |
| 544 | + await user.click(screen.getByText("Type")); |
| 545 | + |
| 546 | + expect(focusControl).not.toHaveBeenCalled(); |
| 547 | + }); |
| 548 | +}); |
| 549 | + |
404 | 550 | describe("FieldItem with CodeEditor", () => { |
405 | 551 | beforeAll(() => { |
406 | 552 | // the code editor needs range support that is not provided by jsdom |
@@ -435,4 +581,28 @@ describe("FieldItem with CodeEditor", () => { |
435 | 581 | expect(input).toHaveAttribute("aria-labelledby", label.id); |
436 | 582 | expect(input).toHaveAttribute("aria-describedby", message.id); |
437 | 583 | }); |
| 584 | + |
| 585 | + it.each([ |
| 586 | + [false, false], |
| 587 | + [false, true], |
| 588 | + [true, false], |
| 589 | + [true, true], |
| 590 | + ])("handles label clicks with disabled=%s and readOnly=%s", async (disabled, readOnly) => { |
| 591 | + const user = userEvent.setup(); |
| 592 | + const { container } = render( |
| 593 | + <FieldItem labelProps={{ text: "Expression" }}> |
| 594 | + <CodeEditor name="expression" mode="yaml" disabled={disabled} readOnly={readOnly} /> |
| 595 | + </FieldItem>, |
| 596 | + ); |
| 597 | + const input = container.querySelector(".cm-content"); |
| 598 | + await waitFor(() => expect(input).toHaveAccessibleName("Expression")); |
| 599 | + |
| 600 | + await user.click(screen.getByText("Expression")); |
| 601 | + |
| 602 | + if (disabled) { |
| 603 | + expect(input).not.toHaveFocus(); |
| 604 | + } else { |
| 605 | + expect(input).toHaveFocus(); |
| 606 | + } |
| 607 | + }); |
438 | 608 | }); |
0 commit comments