Skip to content

Commit e964ae2

Browse files
author
Guido Zoli
committed
fix(dropdown): add test and change story
1 parent 1b1fa05 commit e964ae2

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/components/Dropdown/Dropdown.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ export const SerchPerformedByExternalComponent: Story = {
303303
items={filteredItems}
304304
searchPlaceholder={"Type 'err' for error state"}
305305
onClick={handleClick}
306+
onOpenChange={(isOpen) => isOpen && handleSearch('')}
306307
onRetry={(search: string) => handleSearch(search, true)}
307308
onSearch={debouncedSearch}
308309
>
@@ -323,7 +324,7 @@ export const WithSearchAndBottomHeader: Story = {
323324
args: {
324325
isSearchable: true,
325326
header: {
326-
bottom: <div style={{ marginTop: '4px', display: 'flex', flexDirection: 'column', gap: '4px' }}>
327+
bottom: <div style={{ padding: '4px 8px 0 8px', display: 'flex', flexDirection: 'column', gap: '4px' }}>
327328
<div style={{ width: '100%', height: '1px', background: '#F2F2F2' }} />
328329
<div style={{ color: '#898989' }}>Custom bottom header</div>
329330
</div>,

src/components/Dropdown/Dropdown.test.tsx

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,77 @@ describe('Dropdown Component', () => {
459459
const headerBottom = await screen.findByTestId('header-bottom-id')
460460
expect(headerBottom).toBeInTheDocument()
461461
})
462+
463+
it('clears search when dropdown is closed and reopened', async() => {
464+
const customItems = [
465+
{ id: 'apple', label: 'Apple' },
466+
{ id: 'banana', label: 'Banana' },
467+
{ id: 'orange', label: 'Orange' },
468+
]
469+
renderDropdown({ props: { ...defaultProps, items: customItems, isSearchable: true } })
470+
const button = screen.getByText('test-trigger-button')
471+
await userEvent.click(button)
472+
473+
const searchInput = screen.getByRole('textbox')
474+
await userEvent.type(searchInput, 'app')
475+
476+
expect(screen.getByText('Apple')).toBeInTheDocument()
477+
expect(screen.queryByText('Banana')).not.toBeInTheDocument()
478+
479+
// Close dropdown by clicking on the button again
480+
await userEvent.click(button)
481+
await waitFor(() => expect(screen.queryByRole('textbox')).not.toBeInTheDocument())
482+
483+
// Reopen dropdown
484+
await userEvent.click(button)
485+
const newSearchInput = screen.getByRole('textbox')
486+
487+
expect(newSearchInput).toHaveValue('')
488+
expect(screen.getByText('Apple')).toBeInTheDocument()
489+
expect(screen.getByText('Banana')).toBeInTheDocument()
490+
expect(screen.getByText('Orange')).toBeInTheDocument()
491+
})
492+
493+
it('clears search when dropdown is reopened after item is selected and', async() => {
494+
const onClick = jest.fn()
495+
const customItems = [
496+
{ id: 'apple', label: 'Apple' },
497+
{ id: 'banana', label: 'Banana' },
498+
{ id: 'orange', label: 'Orange' },
499+
]
500+
renderDropdown({
501+
props: {
502+
...defaultProps,
503+
items: customItems,
504+
isSearchable: true,
505+
onClick,
506+
},
507+
})
508+
const button = screen.getByText('test-trigger-button')
509+
await userEvent.click(button)
510+
511+
const searchInput = screen.getByRole('textbox')
512+
await userEvent.type(searchInput, 'app')
513+
514+
expect(screen.getByText('Apple')).toBeInTheDocument()
515+
expect(screen.queryByText('Banana')).not.toBeInTheDocument()
516+
517+
const appleItem = screen.getByRole('menuitem', { name: 'Apple' })
518+
await userEvent.click(appleItem)
519+
await waitFor(() => expect(onClick).toHaveBeenCalled())
520+
521+
// Dropdown should close after selection
522+
await waitFor(() => expect(screen.queryByRole('textbox')).not.toBeInTheDocument())
523+
524+
// Reopen dropdown
525+
await userEvent.click(button)
526+
const newSearchInput = screen.getByRole('textbox')
527+
528+
expect(newSearchInput).toHaveValue('')
529+
expect(screen.getByText('Apple')).toBeInTheDocument()
530+
expect(screen.getByText('Banana')).toBeInTheDocument()
531+
expect(screen.getByText('Orange')).toBeInTheDocument()
532+
})
462533
})
463534

464535
describe('with footer', () => {

0 commit comments

Comments
 (0)