@@ -3,9 +3,35 @@ import { expect, screen, userEvent, waitFor, within } from "@storybook/test";
33import { useState } from "react" ;
44import { Combobox } from "./Combobox" ;
55
6- const options = [ "Option 1 " , "Option 2 " , "Option 3 " , "Another Option " ] ;
6+ const simpleOptions = [ "Go " , "Gleam " , "Kotlin " , "Rust " ] ;
77
8- const ComboboxWithHooks = ( ) => {
8+ const advancedOptions = [
9+ {
10+ displayName : "Go" ,
11+ value : "go" ,
12+ icon : "/icon/go.svg" ,
13+ } ,
14+ {
15+ displayName : "Gleam" ,
16+ value : "gleam" ,
17+ icon : "https://github.com/gleam-lang.png" ,
18+ } ,
19+ {
20+ displayName : "Kotlin" ,
21+ value : "kotlin" ,
22+ description : "Kotlin 2.1, OpenJDK 24, gradle" ,
23+ icon : "/icon/kotlin.svg" ,
24+ } ,
25+ {
26+ displayName : "Rust" ,
27+ value : "rust" ,
28+ icon : "/icon/rust.svg" ,
29+ } ,
30+ ] as const ;
31+
32+ const ComboboxWithHooks = ( {
33+ options = advancedOptions ,
34+ } : { options ?: React . ComponentProps < typeof Combobox > [ "options" ] } ) => {
935 const [ value , setValue ] = useState ( "" ) ;
1036 const [ open , setOpen ] = useState ( false ) ;
1137 const [ inputValue , setInputValue ] = useState ( "" ) ;
@@ -34,17 +60,21 @@ const ComboboxWithHooks = () => {
3460const meta : Meta < typeof Combobox > = {
3561 title : "components/Combobox" ,
3662 component : Combobox ,
63+ args : { options : advancedOptions } ,
3764} ;
3865
3966export default meta ;
4067type Story = StoryObj < typeof Combobox > ;
4168
42- export const Default : Story = {
43- render : ( ) => < ComboboxWithHooks /> ,
69+ export const Default : Story = { } ;
70+
71+ export const SimpleOptions : Story = {
72+ args : {
73+ options : simpleOptions ,
74+ } ,
4475} ;
4576
4677export const OpenCombobox : Story = {
47- render : ( ) => < ComboboxWithHooks /> ,
4878 play : async ( { canvasElement } ) => {
4979 const canvas = within ( canvasElement ) ;
5080 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
@@ -58,11 +88,7 @@ export const SelectOption: Story = {
5888 play : async ( { canvasElement } ) => {
5989 const canvas = within ( canvasElement ) ;
6090 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
61- await userEvent . click ( screen . getByText ( "Option 1" ) ) ;
62-
63- await waitFor ( ( ) =>
64- expect ( canvas . getByRole ( "button" ) ) . toHaveTextContent ( "Option 1" ) ,
65- ) ;
91+ await userEvent . click ( screen . getByText ( "Go" ) ) ;
6692 } ,
6793} ;
6894
@@ -71,19 +97,13 @@ export const SearchAndFilter: Story = {
7197 play : async ( { canvasElement } ) => {
7298 const canvas = within ( canvasElement ) ;
7399 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
74- await userEvent . type ( screen . getByRole ( "combobox" ) , "Another" ) ;
75- await userEvent . click (
76- screen . getByRole ( "option" , { name : "Another Option" } ) ,
77- ) ;
78-
100+ await userEvent . type ( screen . getByRole ( "combobox" ) , "r" ) ;
79101 await waitFor ( ( ) => {
80102 expect (
81- screen . getByRole ( "option" , { name : "Another Option" } ) ,
82- ) . toBeInTheDocument ( ) ;
83- expect (
84- screen . queryByRole ( "option" , { name : "Option 1" } ) ,
103+ screen . queryByRole ( "option" , { name : "Kotlin" } ) ,
85104 ) . not . toBeInTheDocument ( ) ;
86105 } ) ;
106+ await userEvent . click ( screen . getByRole ( "option" , { name : "Rust" } ) ) ;
87107 } ,
88108} ;
89109
@@ -92,16 +112,11 @@ export const EnterCustomValue: Story = {
92112 play : async ( { canvasElement } ) => {
93113 const canvas = within ( canvasElement ) ;
94114 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
95- await userEvent . type ( screen . getByRole ( "combobox" ) , "Custom Value{enter}" ) ;
96-
97- await waitFor ( ( ) =>
98- expect ( canvas . getByRole ( "button" ) ) . toHaveTextContent ( "Custom Value" ) ,
99- ) ;
115+ await userEvent . type ( screen . getByRole ( "combobox" ) , "Swift{enter}" ) ;
100116 } ,
101117} ;
102118
103119export const NoResults : Story = {
104- render : ( ) => < ComboboxWithHooks /> ,
105120 play : async ( { canvasElement } ) => {
106121 const canvas = within ( canvasElement ) ;
107122 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
@@ -120,10 +135,11 @@ export const ClearSelectedOption: Story = {
120135 const canvas = within ( canvasElement ) ;
121136
122137 await userEvent . click ( canvas . getByRole ( "button" ) ) ;
138+ // const goOption = screen.getByText("Go");
123139 // First select an option
124- await userEvent . click ( screen . getByRole ( "option" , { name : "Option 1 " } ) ) ;
140+ await userEvent . click ( await screen . findByRole ( "option" , { name : "Go " } ) ) ;
125141 // Then clear it by selecting it again
126- await userEvent . click ( screen . getByRole ( "option" , { name : "Option 1 " } ) ) ;
142+ await userEvent . click ( await screen . findByRole ( "option" , { name : "Go " } ) ) ;
127143
128144 await waitFor ( ( ) =>
129145 expect ( canvas . getByRole ( "button" ) ) . toHaveTextContent ( "Select option" ) ,
0 commit comments