11import React from "react" ;
2- import { render , screen } from "@testing-library/react" ;
2+ import { createEvent , fireEvent , render , screen } from "@testing-library/react" ;
33import userEvent from "@testing-library/user-event" ;
44import Step from "./Step" ;
55import type { Props } from "./Step" ;
@@ -14,6 +14,10 @@ describe("Step component", () => {
1414 handleClick : jest . fn ( ) ,
1515 } ;
1616
17+ beforeEach ( ( ) => {
18+ jest . clearAllMocks ( ) ;
19+ } ) ;
20+
1721 it ( "renders the step with the required props" , ( ) => {
1822 render ( < Step { ...props } /> ) ;
1923 expect ( screen . getByText ( "Title" ) ) . toBeInTheDocument ( ) ;
@@ -47,6 +51,55 @@ describe("Step component", () => {
4751 expect ( props . handleClick ) . toHaveBeenCalled ( ) ;
4852 } ) ;
4953
54+ it ( "exposes the title as a button role" , ( ) => {
55+ render ( < Step { ...props } /> ) ;
56+ expect ( screen . getByRole ( "button" , { name : "Title" } ) ) . toBeInTheDocument ( ) ;
57+ } ) ;
58+
59+ it ( "is keyboard focusable and not aria-disabled when enabled" , ( ) => {
60+ render ( < Step { ...props } /> ) ;
61+ const title = screen . getByText ( "Title" ) ;
62+ expect ( title ) . toHaveAttribute ( "tabindex" , "0" ) ;
63+ expect ( title ) . toHaveAttribute ( "aria-disabled" , "false" ) ;
64+ } ) ;
65+
66+ it ( "calls handleClick when Enter is pressed and enabled" , async ( ) => {
67+ render ( < Step { ...props } /> ) ;
68+ screen . getByText ( "Title" ) . focus ( ) ;
69+ await userEvent . keyboard ( "{Enter}" ) ;
70+ expect ( props . handleClick ) . toHaveBeenCalled ( ) ;
71+ } ) ;
72+
73+ it ( "calls handleClick and prevents default when Space is pressed and enabled" , ( ) => {
74+ render ( < Step { ...props } /> ) ;
75+ const title = screen . getByText ( "Title" ) ;
76+ const event = createEvent . keyDown ( title , { key : " " } ) ;
77+ fireEvent ( title , event ) ;
78+ expect ( props . handleClick ) . toHaveBeenCalled ( ) ;
79+ expect ( event . defaultPrevented ) . toBe ( true ) ;
80+ } ) ;
81+
82+ it ( "is not focusable and is aria-disabled when disabled" , ( ) => {
83+ render ( < Step { ...props } enabled = { false } /> ) ;
84+ const title = screen . getByText ( "Title" ) ;
85+ expect ( title ) . toHaveAttribute ( "tabindex" , "-1" ) ;
86+ expect ( title ) . toHaveAttribute ( "aria-disabled" , "true" ) ;
87+ } ) ;
88+
89+ it ( "does not call handleClick on keyboard activation when disabled" , ( ) => {
90+ render ( < Step { ...props } enabled = { false } /> ) ;
91+ const title = screen . getByText ( "Title" ) ;
92+ fireEvent . keyDown ( title , { key : "Enter" } ) ;
93+ fireEvent . keyDown ( title , { key : " " } ) ;
94+ expect ( props . handleClick ) . not . toHaveBeenCalled ( ) ;
95+ } ) ;
96+
97+ it ( "does not call handleClick when clicked while disabled" , async ( ) => {
98+ render ( < Step { ...props } enabled = { false } /> ) ;
99+ await userEvent . click ( screen . getByText ( "Title" ) ) ;
100+ expect ( props . handleClick ) . not . toHaveBeenCalled ( ) ;
101+ } ) ;
102+
50103 it ( "can display optional label" , ( ) => {
51104 render ( < Step { ...props } label = "Optional label" /> ) ;
52105
0 commit comments