-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ChatbotConversationHistoryDropdown): fix dropdown toggle behavior…
… on item selection
- Loading branch information
1 parent
dddfb24
commit 96bb1ed
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
...ages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { DropdownItem } from '@patternfly/react-core'; | ||
import { fireEvent, render, screen, waitFor } from '@testing-library/react'; | ||
import ChatbotConversationHistoryDropdown from './ChatbotConversationHistoryDropdown'; | ||
|
||
describe('ChatbotConversationHistoryDropdown', () => { | ||
const onSelect = jest.fn(); | ||
const menuItems = ( | ||
<> | ||
<DropdownItem>Rename</DropdownItem> | ||
<DropdownItem>Delete</DropdownItem> | ||
</> | ||
); | ||
|
||
it('should render the dropdown', () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} menuClassName="custom-class" />); | ||
expect(screen.queryByRole('menuitem', { name: /Conversation options/i })).toBeInTheDocument(); | ||
}); | ||
|
||
it('should display the dropdown menuItems', () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} />); | ||
|
||
const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i })!; | ||
|
||
expect(toggle).toBeInTheDocument(); | ||
fireEvent.click(toggle); | ||
|
||
waitFor(() => { | ||
expect(screen.getByText('Rename')).toBeInTheDocument(); | ||
expect(screen.getByText('Delete')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('should invoke onSelect callback when menuitem is clicked', () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} onSelect={onSelect} />); | ||
const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i })!; | ||
fireEvent.click(toggle); | ||
fireEvent.click(screen.getByText('Rename')); | ||
|
||
expect(onSelect).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should toggle the dropdown when menuitem is clicked', () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} onSelect={onSelect} />); | ||
const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i })!; | ||
fireEvent.click(toggle); | ||
fireEvent.click(screen.getByText('Delete')); | ||
|
||
expect(onSelect).toHaveBeenCalled(); | ||
|
||
expect(screen.queryByText('Delete')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should close the dropdown when user clicks outside', () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} onSelect={onSelect} />); | ||
const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i })!; | ||
fireEvent.click(toggle); | ||
|
||
expect(screen.queryByText('Delete')).toBeInTheDocument(); | ||
fireEvent.click(toggle.parentElement!); | ||
|
||
expect(screen.queryByText('Delete')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should show the tooltip when the user hovers over the toggle button', async () => { | ||
render(<ChatbotConversationHistoryDropdown menuItems={menuItems} label="Actions dropdown" />); | ||
const toggle = screen.queryByRole('menuitem', { name: /Actions dropdown/i })!; | ||
|
||
fireEvent( | ||
toggle, | ||
new MouseEvent('mouseenter', { | ||
bubbles: false, | ||
cancelable: false | ||
}) | ||
); | ||
|
||
await waitFor(() => { | ||
expect(screen.queryByText('Actions dropdown')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters