@@ -3,9 +3,35 @@ import { expect, screen, userEvent, waitFor, within } from "@storybook/test";
3
3
import { useState } from "react" ;
4
4
import { Combobox } from "./Combobox" ;
5
5
6
- const options = [ "Option 1 " , "Option 2 " , "Option 3 " , "Another Option " ] ;
6
+ const simpleOptions = [ "Go " , "Gleam " , "Kotlin " , "Rust " ] ;
7
7
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" ] } ) => {
9
35
const [ value , setValue ] = useState ( "" ) ;
10
36
const [ open , setOpen ] = useState ( false ) ;
11
37
const [ inputValue , setInputValue ] = useState ( "" ) ;
@@ -34,17 +60,21 @@ const ComboboxWithHooks = () => {
34
60
const meta : Meta < typeof Combobox > = {
35
61
title : "components/Combobox" ,
36
62
component : Combobox ,
63
+ args : { options : advancedOptions } ,
37
64
} ;
38
65
39
66
export default meta ;
40
67
type Story = StoryObj < typeof Combobox > ;
41
68
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
+ } ,
44
75
} ;
45
76
46
77
export const OpenCombobox : Story = {
47
- render : ( ) => < ComboboxWithHooks /> ,
48
78
play : async ( { canvasElement } ) => {
49
79
const canvas = within ( canvasElement ) ;
50
80
await userEvent . click ( canvas . getByRole ( "button" ) ) ;
@@ -58,11 +88,7 @@ export const SelectOption: Story = {
58
88
play : async ( { canvasElement } ) => {
59
89
const canvas = within ( canvasElement ) ;
60
90
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" ) ) ;
66
92
} ,
67
93
} ;
68
94
@@ -71,19 +97,13 @@ export const SearchAndFilter: Story = {
71
97
play : async ( { canvasElement } ) => {
72
98
const canvas = within ( canvasElement ) ;
73
99
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" ) ;
79
101
await waitFor ( ( ) => {
80
102
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" } ) ,
85
104
) . not . toBeInTheDocument ( ) ;
86
105
} ) ;
106
+ await userEvent . click ( screen . getByRole ( "option" , { name : "Rust" } ) ) ;
87
107
} ,
88
108
} ;
89
109
@@ -92,16 +112,11 @@ export const EnterCustomValue: Story = {
92
112
play : async ( { canvasElement } ) => {
93
113
const canvas = within ( canvasElement ) ;
94
114
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}" ) ;
100
116
} ,
101
117
} ;
102
118
103
119
export const NoResults : Story = {
104
- render : ( ) => < ComboboxWithHooks /> ,
105
120
play : async ( { canvasElement } ) => {
106
121
const canvas = within ( canvasElement ) ;
107
122
await userEvent . click ( canvas . getByRole ( "button" ) ) ;
@@ -120,10 +135,11 @@ export const ClearSelectedOption: Story = {
120
135
const canvas = within ( canvasElement ) ;
121
136
122
137
await userEvent . click ( canvas . getByRole ( "button" ) ) ;
138
+ // const goOption = screen.getByText("Go");
123
139
// First select an option
124
- await userEvent . click ( screen . getByRole ( "option" , { name : "Option 1 " } ) ) ;
140
+ await userEvent . click ( await screen . findByRole ( "option" , { name : "Go " } ) ) ;
125
141
// 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 " } ) ) ;
127
143
128
144
await waitFor ( ( ) =>
129
145
expect ( canvas . getByRole ( "button" ) ) . toHaveTextContent ( "Select option" ) ,
0 commit comments