Skip to content

Commit

Permalink
Merge pull request #344 from karthikjeeyar/dropdown-toggle
Browse files Browse the repository at this point in the history
fix(ChatbotConversationHistoryDropdown): fix dropdown toggle behavior on item selection
  • Loading branch information
nicolethoen authored Dec 2, 2024
2 parents dddfb24 + 96bb1ed commit f2c4b99
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
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 })!;

Check warning on line 24 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion

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 })!;

Check warning on line 37 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion
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 })!;

Check warning on line 46 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion
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 })!;

Check warning on line 57 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion
fireEvent.click(toggle);

expect(screen.queryByText('Delete')).toBeInTheDocument();
fireEvent.click(toggle.parentElement!);

Check warning on line 61 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion

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 })!;

Check warning on line 68 in packages/module/src/ChatbotConversationHistoryNav/ChatbotConversationHistoryDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Forbidden non-null assertion

fireEvent(
toggle,
new MouseEvent('mouseenter', {
bubbles: false,
cancelable: false
})
);

await waitFor(() => {
expect(screen.queryByText('Actions dropdown')).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export const ChatbotConversationHistoryDropdown: React.FunctionComponent<Chatbot
<Dropdown
className={`pf-chatbot__selections ${menuClassName ?? ''}`}
isOpen={isOpen}
onSelect={onSelect}
onSelect={(props) => {
onSelect?.(props);
setIsOpen((prev) => !prev);
}}
onOpenChange={(isOpen) => setIsOpen(isOpen)}
popperProps={{ position: 'right' }}
shouldFocusToggleOnSelect
Expand Down

0 comments on commit f2c4b99

Please sign in to comment.