1
- import React , { useMemo , useState , useEffect } from "react" ;
2
- import { screen , userEvent } from "@storybook/testing-library" ;
3
- import { expect } from "@storybook/jest" ;
1
+ import React , { useMemo , useState , useEffect , use } from "react" ;
2
+ import { screen , userEvent , waitFor } from "@storybook/testing-library" ;
3
+ import { expect , jest } from "@storybook/jest" ;
4
4
import { Box } from "../box" ;
5
5
import { matchSorter } from "match-sorter" ;
6
6
import { InputSearch } from "./" ;
@@ -267,6 +267,8 @@ const ControlledTemplate: StoryFn<typeof InputSearch.Root> = (args) => {
267
267
openOnFocus
268
268
onSelect = { ( value ) => {
269
269
setTerm ( value ) ;
270
+ console . log ( "onSelect" , value ) ;
271
+ args . onSelect && args . onSelect ( value ) ;
270
272
} }
271
273
>
272
274
< InputSearch . Input
@@ -294,16 +296,21 @@ const ControlledTemplate: StoryFn<typeof InputSearch.Root> = (args) => {
294
296
295
297
export const Controlled = {
296
298
render : ControlledTemplate ,
297
- args : { } ,
298
-
299
+ args : {
300
+ onSelect : jest . fn ( ) ,
301
+ } ,
299
302
parameters : {
300
303
chromatic : { disableSnapshot : true } ,
301
304
} ,
302
305
} ;
303
306
304
- const InteractionsTemplate : StoryFn < typeof InputSearch . Root > = ( ) => (
307
+ const InteractionsTemplate : StoryFn < typeof InputSearch . Root > = ( args ) => (
305
308
< Box css = { { width : "275px" , height : "340px" } } >
306
- < InputSearch . Root aria-label = "Example-Search" openOnFocus >
309
+ < InputSearch . Root
310
+ aria-label = "Example-Search"
311
+ openOnFocus
312
+ onSelect = { args . onSelect }
313
+ >
307
314
< InputSearch . Input name = "city" id = "city" />
308
315
< InputSearch . Popover >
309
316
< InputSearch . List >
@@ -320,21 +327,28 @@ const InteractionsTemplate: StoryFn<typeof InputSearch.Root> = () => (
320
327
321
328
export const Interactions = {
322
329
render : InteractionsTemplate ,
323
-
324
- play : async ( ) => {
330
+ args : {
331
+ onSelect : jest . fn ( ) ,
332
+ } ,
333
+ play : async ( { args } ) => {
325
334
const input = await screen . findByLabelText ( "Search" ) ;
326
335
await userEvent . type ( input , "app" , {
327
336
delay : 100 ,
328
337
} ) ;
329
338
await userEvent . keyboard ( "[ArrowDown]" ) ;
330
339
await expect ( input ) . toHaveDisplayValue ( "Apple" ) ;
340
+ await userEvent . keyboard ( "[Enter]" ) ;
341
+ await expect ( args . onSelect ) . toHaveBeenCalledWith ( "Apple" ) ;
342
+ const clearButton = await screen . findByRole ( "button" , { name : "Clear" } ) ;
343
+ await userEvent . click ( clearButton ) ;
344
+ await expect ( args . onSelect ) . toHaveBeenCalledWith ( "" ) ;
331
345
} ,
332
346
} ;
333
347
334
348
export const ControlledKeyboardInteractions = {
335
349
render : ControlledTemplate ,
336
350
337
- play : async ( ) => {
351
+ play : async ( { args } ) => {
338
352
const input = await screen . findByLabelText ( "Search" ) ;
339
353
await userEvent . type ( input , "test" , {
340
354
delay : 100 ,
@@ -345,6 +359,9 @@ export const ControlledKeyboardInteractions = {
345
359
await expect ( input ) . toHaveDisplayValue ( "Orange" ) ;
346
360
await userEvent . keyboard ( "[Backspace]" ) ;
347
361
await expect ( input ) . toHaveDisplayValue ( "Orang" ) ;
362
+ await userEvent . keyboard ( "[ArrowUp]" ) ;
363
+ await userEvent . keyboard ( "[Enter]" ) ;
364
+ await expect ( args . onSelect ) . toHaveBeenCalledWith ( "Pineapple" ) ;
348
365
const clearButton = await screen . findByText ( "Clear" ) ;
349
366
await userEvent . click ( clearButton ) ;
350
367
await expect ( input ) . toHaveDisplayValue ( "" ) ;
@@ -356,6 +373,12 @@ export const ControlledKeyboardInteractions = {
356
373
const externalClearButton = await screen . findByText ( "External Clear" ) ;
357
374
await userEvent . click ( externalClearButton ) ;
358
375
await expect ( input ) . toHaveDisplayValue ( "" ) ;
376
+ await userEvent . click ( input ) ;
377
+ await expect ( input ) . toHaveFocus ( ) ;
378
+ const appleOption = await screen . findByRole ( "option" , { name : "Apple" } ) ;
379
+ await userEvent . click ( appleOption ) ;
380
+ await expect ( input ) . toHaveDisplayValue ( "Apple" ) ;
381
+ await expect ( args . onSelect ) . toHaveBeenCalledWith ( "Apple" ) ;
359
382
//
360
383
} ,
361
384
} ;
0 commit comments