|
| 1 | +importtype{Meta,StoryObj}from"@storybook/react"; |
| 2 | +import{expect,screen,userEvent,waitFor,within}from"@storybook/test"; |
| 3 | +import{useState}from"react"; |
| 4 | +import{Combobox}from"./Combobox"; |
| 5 | + |
| 6 | +constoptions=["Option 1","Option 2","Option 3","Another Option"]; |
| 7 | + |
| 8 | +constComboboxWithHooks=()=>{ |
| 9 | +const[value,setValue]=useState(""); |
| 10 | +const[open,setOpen]=useState(false); |
| 11 | +const[inputValue,setInputValue]=useState(""); |
| 12 | + |
| 13 | +return( |
| 14 | +<Combobox |
| 15 | +value={value} |
| 16 | +options={options} |
| 17 | +placeholder="Select option" |
| 18 | +open={open} |
| 19 | +onOpenChange={setOpen} |
| 20 | +inputValue={inputValue} |
| 21 | +onInputChange={setInputValue} |
| 22 | +onSelect={setValue} |
| 23 | +onKeyDown={(e)=>{ |
| 24 | +if(e.key==="Enter"&&inputValue&&!options.includes(inputValue)){ |
| 25 | +setValue(inputValue); |
| 26 | +setInputValue(""); |
| 27 | +setOpen(false); |
| 28 | +} |
| 29 | +}} |
| 30 | +/> |
| 31 | +); |
| 32 | +}; |
| 33 | + |
| 34 | +constmeta:Meta<typeofCombobox>={ |
| 35 | +title:"components/Combobox", |
| 36 | +component:Combobox, |
| 37 | +}; |
| 38 | + |
| 39 | +exportdefaultmeta; |
| 40 | +typeStory=StoryObj<typeofCombobox>; |
| 41 | + |
| 42 | +exportconstDefault:Story={ |
| 43 | +render:()=><ComboboxWithHooks/>, |
| 44 | +}; |
| 45 | + |
| 46 | +exportconstOpenCombobox:Story={ |
| 47 | +render:()=><ComboboxWithHooks/>, |
| 48 | +play:async({ canvasElement})=>{ |
| 49 | +constcanvas=within(canvasElement); |
| 50 | +awaituserEvent.click(canvas.getByRole("button")); |
| 51 | + |
| 52 | +awaitwaitFor(()=>expect(screen.getByRole("dialog")).toBeInTheDocument()); |
| 53 | +}, |
| 54 | +}; |
| 55 | + |
| 56 | +exportconstSelectOption:Story={ |
| 57 | +render:()=><ComboboxWithHooks/>, |
| 58 | +play:async({ canvasElement})=>{ |
| 59 | +constcanvas=within(canvasElement); |
| 60 | +awaituserEvent.click(canvas.getByRole("button")); |
| 61 | +awaituserEvent.click(screen.getByText("Option 1")); |
| 62 | + |
| 63 | +awaitwaitFor(()=> |
| 64 | +expect(canvas.getByRole("button")).toHaveTextContent("Option 1"), |
| 65 | +); |
| 66 | +}, |
| 67 | +}; |
| 68 | + |
| 69 | +exportconstSearchAndFilter:Story={ |
| 70 | +render:()=><ComboboxWithHooks/>, |
| 71 | +play:async({ canvasElement})=>{ |
| 72 | +constcanvas=within(canvasElement); |
| 73 | +awaituserEvent.click(canvas.getByRole("button")); |
| 74 | +awaituserEvent.type(screen.getByRole("combobox"),"Another"); |
| 75 | +awaituserEvent.click( |
| 76 | +screen.getByRole("option",{name:"Another Option"}), |
| 77 | +); |
| 78 | + |
| 79 | +awaitwaitFor(()=>{ |
| 80 | +expect( |
| 81 | +screen.getByRole("option",{name:"Another Option"}), |
| 82 | +).toBeInTheDocument(); |
| 83 | +expect( |
| 84 | +screen.queryByRole("option",{name:"Option 1"}), |
| 85 | +).not.toBeInTheDocument(); |
| 86 | +}); |
| 87 | +}, |
| 88 | +}; |
| 89 | + |
| 90 | +exportconstEnterCustomValue:Story={ |
| 91 | +render:()=><ComboboxWithHooks/>, |
| 92 | +play:async({ canvasElement})=>{ |
| 93 | +constcanvas=within(canvasElement); |
| 94 | +awaituserEvent.click(canvas.getByRole("button")); |
| 95 | +awaituserEvent.type(screen.getByRole("combobox"),"Custom Value{enter}"); |
| 96 | + |
| 97 | +awaitwaitFor(()=> |
| 98 | +expect(canvas.getByRole("button")).toHaveTextContent("Custom Value"), |
| 99 | +); |
| 100 | +}, |
| 101 | +}; |
| 102 | + |
| 103 | +exportconstNoResults:Story={ |
| 104 | +render:()=><ComboboxWithHooks/>, |
| 105 | +play:async({ canvasElement})=>{ |
| 106 | +constcanvas=within(canvasElement); |
| 107 | +awaituserEvent.click(canvas.getByRole("button")); |
| 108 | +awaituserEvent.type(screen.getByRole("combobox"),"xyz"); |
| 109 | + |
| 110 | +awaitwaitFor(()=>{ |
| 111 | +expect(screen.getByText("No results found")).toBeInTheDocument(); |
| 112 | +expect(screen.getByText("Enter custom value")).toBeInTheDocument(); |
| 113 | +}); |
| 114 | +}, |
| 115 | +}; |
| 116 | + |
| 117 | +exportconstClearSelectedOption:Story={ |
| 118 | +render:()=><ComboboxWithHooks/>, |
| 119 | +play:async({ canvasElement})=>{ |
| 120 | +constcanvas=within(canvasElement); |
| 121 | + |
| 122 | +awaituserEvent.click(canvas.getByRole("button")); |
| 123 | +// First select an option |
| 124 | +awaituserEvent.click(screen.getByRole("option",{name:"Option 1"})); |
| 125 | +// Then clear it by selecting it again |
| 126 | +awaituserEvent.click(screen.getByRole("option",{name:"Option 1"})); |
| 127 | + |
| 128 | +awaitwaitFor(()=> |
| 129 | +expect(canvas.getByRole("button")).toHaveTextContent("Select option"), |
| 130 | +); |
| 131 | +}, |
| 132 | +}; |