@@ -38,6 +38,102 @@ describe("createComponentInteraction", () => {
3838 ) ;
3939 } ) ;
4040
41+ it ( "uses the first matching element" , async ( ) => {
42+ const firstElement = { click : vi . fn ( ) . mockResolvedValue ( "first-clicked" ) } ;
43+ const element = {
44+ first : vi . fn ( ( ) => firstElement ) ,
45+ last : vi . fn ( ) ,
46+ nth : vi . fn ( ) ,
47+ } ;
48+ const interaction = createComponentInteraction (
49+ "$component: I click on $element." ,
50+ ( { element } ) => element . click ( ) ,
51+ ) ;
52+
53+ const result = await interaction (
54+ {
55+ name : "search-form" ,
56+ elements : { submit : element } ,
57+ } as never ,
58+ [ "submit" , "first" ] ,
59+ ) ;
60+
61+ expect ( result ) . toBe ( "first-clicked" ) ;
62+ expect ( element . first ) . toHaveBeenCalledTimes ( 1 ) ;
63+ expect ( element . last ) . not . toHaveBeenCalled ( ) ;
64+ expect ( element . nth ) . not . toHaveBeenCalled ( ) ;
65+ expect ( firstElement . click ) . toHaveBeenCalledTimes ( 1 ) ;
66+ expect ( stepMock ) . toHaveBeenCalledWith (
67+ "search-form: I click on submit::first." ,
68+ expect . any ( Function ) ,
69+ ) ;
70+ } ) ;
71+
72+ it ( "uses the last matching element" , async ( ) => {
73+ const lastElement = { click : vi . fn ( ) . mockResolvedValue ( "last-clicked" ) } ;
74+ const element = {
75+ first : vi . fn ( ) ,
76+ last : vi . fn ( ( ) => lastElement ) ,
77+ nth : vi . fn ( ) ,
78+ } ;
79+ const interaction = createComponentInteraction (
80+ "$component: I click on $element." ,
81+ ( { element } ) => element . click ( ) ,
82+ ) ;
83+
84+ const result = await interaction (
85+ {
86+ name : "search-form" ,
87+ elements : { submit : element } ,
88+ } as never ,
89+ [ "submit" , "last" ] ,
90+ ) ;
91+
92+ expect ( result ) . toBe ( "last-clicked" ) ;
93+ expect ( element . first ) . not . toHaveBeenCalled ( ) ;
94+ expect ( element . last ) . toHaveBeenCalledTimes ( 1 ) ;
95+ expect ( element . nth ) . not . toHaveBeenCalled ( ) ;
96+ expect ( lastElement . click ) . toHaveBeenCalledTimes ( 1 ) ;
97+ expect ( stepMock ) . toHaveBeenCalledWith (
98+ "search-form: I click on submit::last." ,
99+ expect . any ( Function ) ,
100+ ) ;
101+ } ) ;
102+
103+ it ( "uses the nth matching element" , async ( ) => {
104+ const nthElement = { click : vi . fn ( ) . mockResolvedValue ( "nth-clicked" ) } ;
105+ const element = {
106+ first : vi . fn ( ) ,
107+ last : vi . fn ( ) ,
108+ nth : vi . fn ( ( index : number ) => {
109+ expect ( index ) . toBe ( 2 ) ;
110+ return nthElement ;
111+ } ) ,
112+ } ;
113+ const interaction = createComponentInteraction (
114+ "$component: I click on $element." ,
115+ ( { element } ) => element . click ( ) ,
116+ ) ;
117+
118+ const result = await interaction (
119+ {
120+ name : "search-form" ,
121+ elements : { submit : element } ,
122+ } as never ,
123+ [ "submit" , 2 ] ,
124+ ) ;
125+
126+ expect ( result ) . toBe ( "nth-clicked" ) ;
127+ expect ( element . first ) . not . toHaveBeenCalled ( ) ;
128+ expect ( element . last ) . not . toHaveBeenCalled ( ) ;
129+ expect ( element . nth ) . toHaveBeenCalledWith ( 2 ) ;
130+ expect ( nthElement . click ) . toHaveBeenCalledTimes ( 1 ) ;
131+ expect ( stepMock ) . toHaveBeenCalledWith (
132+ "search-form: I click on submit::2." ,
133+ expect . any ( Function ) ,
134+ ) ;
135+ } ) ;
136+
41137 it ( "throws when the element is missing" , ( ) => {
42138 const interaction = createComponentInteraction (
43139 "$component: I click on $element." ,
@@ -71,6 +167,23 @@ describe("createComponentInteraction", () => {
71167 "Missing element \"submit\" on component \"search-form\". Available elements: none." ,
72168 ) ;
73169 } ) ;
170+
171+ it ( "lists available elements when the selected element is missing" , ( ) => {
172+ const interaction = createComponentInteraction (
173+ "$component: I click on $element." ,
174+ ( ) => undefined ,
175+ ) ;
176+
177+ expect ( ( ) => ( interaction as ( ...args : any [ ] ) => unknown ) (
178+ {
179+ name : "search-form" ,
180+ elements : { reset : { } } ,
181+ } ,
182+ "submit" ,
183+ ) ) . toThrowError (
184+ "Missing element \"submit\" on component \"search-form\". Available elements: reset." ,
185+ ) ;
186+ } ) ;
74187} ) ;
75188
76189describe ( "createStepWrapper" , ( ) => {
0 commit comments