33import { cleanup , fireEvent , render } from "@testing-library/react" ;
44import { afterEach , describe , expect , test , vi } from "vitest" ;
55
6- import { KeyCode } from "@cloudscape-design/component-toolkit/internal" ;
6+ import * as ComponentToolkitInternal from "@cloudscape-design/component-toolkit/internal" ;
77
88import SupportPromptGroup , { SupportPromptGroupProps } from "../../../lib/components/support-prompt-group" ;
99import createWrapper from "../../../lib/components/test-utils/dom" ;
@@ -42,6 +42,10 @@ export function renderSupportPromptGroup(
4242}
4343
4444describe ( "Support prompt group" , ( ) => {
45+ afterEach ( ( ) => {
46+ cleanup ( ) ;
47+ } ) ;
48+
4549 test ( "Renders null with no items" , ( ) => {
4650 const wrapper = renderSupportPromptGroup ( {
4751 items : [ ] ,
@@ -59,7 +63,7 @@ describe("Support prompt group", () => {
5963 test ( "Finds item by id" , ( ) => {
6064 const wrapper = renderSupportPromptGroup ( { } ) ;
6165
62- expect ( wrapper . findItemById ( "item-1" ) ? .getElement ( ) ) . toHaveTextContent ( "Item 1" ) ;
66+ expect ( wrapper . findItemById ( "item-1" ) ! . getElement ( ) ) . toHaveTextContent ( "Item 1" ) ;
6367 } ) ;
6468
6569 test ( "fires onClick" , ( ) => {
@@ -92,16 +96,37 @@ describe("Support prompt group", () => {
9296 } ) ;
9397 } ) ;
9498
95- describe ( "Keyboard navigation" , ( ) => {
96- afterEach ( ( ) => {
97- cleanup ( ) ;
99+ describe ( "focus" , ( ) => {
100+ const ref : { current : SupportPromptGroupProps . Ref | null } = { current : null } ;
101+
102+ test ( "Focuses on element using ref" , ( ) => {
103+ const wrapper = renderSupportPromptGroup ( { } , ref ) ;
104+
105+ ref . current ! . focus ( "item-1" ) ;
106+
107+ expect ( wrapper . findItemById ( "item-1" ) ! . getElement ( ) ) . toHaveFocus ( ) ;
108+ } ) ;
109+
110+ test ( "Throws console warning when no ID is found" , ( ) => {
111+ renderSupportPromptGroup ( { } , ref ) ;
112+ const warnOnce = vi . spyOn ( ComponentToolkitInternal , "warnOnce" ) ;
113+
114+ ref . current ! . focus ( "doesnt-exist" ) ;
115+
116+ expect ( document . body ) . toHaveFocus ( ) ;
117+
118+ expect ( warnOnce ) . toHaveBeenCalledTimes ( 1 ) ;
119+ expect ( warnOnce ) . toHaveBeenCalledWith ( "SupportPromptGroup" , `No matching ID found to focus.` ) ;
98120 } ) ;
121+ } ) ;
122+ describe ( "Keyboard navigation" , ( ) => {
99123 const ref : { current : SupportPromptGroupProps . Ref | null } = { current : null } ;
124+ const { KeyCode } = ComponentToolkitInternal ;
100125
101126 test ( "Arrow keys move focus in vertical alignment" , ( ) => {
102127 const wrapper = renderSupportPromptGroup ( { } , ref ) ;
103128
104- ref . current ? .focus ( "item-1" ) ;
129+ ref . current ! . focus ( "item-1" ) ;
105130
106131 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . right } ) ;
107132 expect ( wrapper . findItemById ( "item-2" ) ! . getElement ( ) ) . toHaveFocus ( ) ;
@@ -113,7 +138,7 @@ describe("Support prompt group", () => {
113138 test ( "Arrow keys move focus in horizontal alignment" , ( ) => {
114139 const wrapper = renderSupportPromptGroup ( { alignment : "horizontal" } , ref ) ;
115140
116- ref . current ? .focus ( "item-1" ) ;
141+ ref . current ! . focus ( "item-1" ) ;
117142
118143 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . down } ) ;
119144 expect ( wrapper . findItemById ( "item-2" ) ! . getElement ( ) ) . toHaveFocus ( ) ;
@@ -125,7 +150,7 @@ describe("Support prompt group", () => {
125150 test ( "Focus loops" , ( ) => {
126151 const wrapper = renderSupportPromptGroup ( { } , ref ) ;
127152
128- ref . current ? .focus ( "item-1" ) ;
153+ ref . current ! . focus ( "item-1" ) ;
129154
130155 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . right } ) ;
131156 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . right } ) ;
@@ -137,25 +162,15 @@ describe("Support prompt group", () => {
137162 test ( "Modifier keys don't move focus" , ( ) => {
138163 const wrapper = renderSupportPromptGroup ( { } , ref ) ;
139164
140- ref . current ? .focus ( "item-1" ) ;
165+ ref . current ! . focus ( "item-1" ) ;
141166
142167 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . down , ctrlKey : true } ) ;
143168 expect ( wrapper . findItemById ( "item-1" ) ! . getElement ( ) ) . toHaveFocus ( ) ;
144169 } ) ;
145170
146171 test ( "Nonexistent target doesn't move focus" , ( ) => {
147172 const wrapper = renderSupportPromptGroup ( { } , ref ) ;
148- ref . current ?. focus ( "doesnt-exist" ) ;
149-
150- fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . down } ) ;
151- expect ( document . body ) . toHaveFocus ( ) ;
152- } ) ;
153-
154- test ( "Null container ref doesn't move focus" , ( ) => {
155- const wrapper = renderSupportPromptGroup ( { } ) ;
156- const ref : { current : SupportPromptGroupProps . Ref | null } = { current : null } ;
157-
158- ref . current ?. focus ( "item-1" ) ;
173+ ref . current ! . focus ( "doesnt-exist" ) ;
159174
160175 fireEvent . keyDown ( wrapper . getElement ( ) , { keyCode : KeyCode . down } ) ;
161176 expect ( document . body ) . toHaveFocus ( ) ;
0 commit comments