-
Notifications
You must be signed in to change notification settings - Fork 78
test: Improve coverage for MultiSelectFilter component #463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ParasKhandelwal1616
wants to merge
5
commits into
CCExtractor:main
Choose a base branch
from
ParasKhandelwal1616:test/increase-coverage-multi-select
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
526e868
fix(ui): auto-close date-time-picker on selection and add tests
ParasKhandelwal1616 7b1814c
move ui tests to __tests__ and revert functional changes
ParasKhandelwal1616 269ca2e
code format
ParasKhandelwal1616 b993135
Merge branch 'main' of https://github.com/CCExtractor/ccsync into tes…
ParasKhandelwal1616 7e9612d
added test on the multi-select.tsx
ParasKhandelwal1616 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
114 changes: 114 additions & 0 deletions
114
frontend/src/components/ui/__tests__/date-time-picker.test.tsx
This file contains hidden or 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,114 @@ | ||
| import { render, screen, fireEvent, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { DateTimePicker } from '../date-time-picker'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| describe('DateTimePicker', () => { | ||
| it('renders without crashing', () => { | ||
| const mockOnDateTimeChange = jest.fn(); | ||
| render( | ||
| <DateTimePicker | ||
| date={undefined} | ||
| onDateTimeChange={mockOnDateTimeChange} | ||
| /> | ||
| ); | ||
| expect( | ||
| screen.getByRole('button', { name: /calender-button/i }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('opens and closes the popover when the trigger button is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| const mockOnDateTimeChange = jest.fn(); | ||
| render( | ||
| <DateTimePicker | ||
| date={undefined} | ||
| onDateTimeChange={mockOnDateTimeChange} | ||
| /> | ||
| ); | ||
|
|
||
| const triggerButton = screen.getByRole('button', { | ||
| name: /calender-button/i, | ||
| }); | ||
|
|
||
| // Open popover | ||
| await user.click(triggerButton); | ||
| expect(screen.getByRole('dialog')).toBeInTheDocument(); // Popover content is a dialog | ||
| expect(screen.getByText(/February 2026/)).toBeInTheDocument(); // Check for specific content inside the calendar | ||
|
|
||
| // Close popover using Escape key | ||
| fireEvent.keyDown(document, { key: 'Escape' }); | ||
| await waitFor(() => { | ||
| expect(screen.queryByText(/February 2026/)).not.toBeInTheDocument(); // Check for absence of specific content | ||
| }); | ||
| }); | ||
|
|
||
| it('allows selecting a date from the calendar', async () => { | ||
| const user = userEvent.setup(); | ||
| const mockOnDateTimeChange = jest.fn(); | ||
| render( | ||
| <DateTimePicker | ||
| date={undefined} | ||
| onDateTimeChange={mockOnDateTimeChange} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: /calender-button/i })); // Open popover | ||
|
|
||
| // Find a date in the current month (e.g., the 15th) | ||
| const dateToSelect = screen.getByRole('gridcell', { name: '15' }); | ||
| await user.click(dateToSelect); | ||
|
|
||
| // Expect the popover to close after selecting a date | ||
| await waitFor(() => { | ||
| expect(screen.queryByText(/February 2026/)).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| // Check if onDateTimeChange was called with the correct date (year, month, and day) | ||
| expect(mockOnDateTimeChange).toHaveBeenCalledTimes(1); | ||
| const calledDate = mockOnDateTimeChange.mock.calls[0][0]; | ||
| expect(calledDate).toBeInstanceOf(Date); | ||
| expect(calledDate.getDate()).toBe(15); | ||
| expect(calledDate.getMonth()).toBe(new Date().getMonth()); // Assuming current month for simplicity | ||
| expect(calledDate.getFullYear()).toBe(new Date().getFullYear()); // Assuming current year for simplicity | ||
| expect(calledDate.getHours()).toBe(0); // Should reset time to 00:00:00 | ||
| expect(mockOnDateTimeChange.mock.calls[0][1]).toBe(false); // hasTime should be false | ||
| }); | ||
|
|
||
| it('allows selecting an hour, minute, and AM/PM', async () => { | ||
| const user = userEvent.setup(); | ||
| const mockOnDateTimeChange = jest.fn(); | ||
| const initialDate = new Date(2024, 0, 15, 10, 30); // Jan 15, 2024, 10:30 AM | ||
| render( | ||
| <DateTimePicker | ||
| date={initialDate} | ||
| onDateTimeChange={mockOnDateTimeChange} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: /calender-button/i })); // Open popover | ||
|
|
||
| // Verify time selection elements are present | ||
| expect(screen.getByText('AM')).toBeInTheDocument(); | ||
| expect(screen.getByText('PM')).toBeInTheDocument(); | ||
|
|
||
| // Select an hour (e.g., 2 PM) | ||
| await user.click(screen.getByRole('button', { name: '2' })); // Select hour 2 | ||
| expect(mockOnDateTimeChange).toHaveBeenCalledTimes(1); // One call for hour selection | ||
| let calledDate = mockOnDateTimeChange.mock.calls[0][0]; | ||
| expect(calledDate.getHours()).toBe(2); // Should be 2 AM initially before PM is clicked | ||
|
|
||
| await user.click(screen.getByRole('button', { name: 'PM' })); // Select PM | ||
| expect(mockOnDateTimeChange).toHaveBeenCalledTimes(2); // Second call for AM/PM selection | ||
| calledDate = mockOnDateTimeChange.mock.calls[1][0]; | ||
| expect(calledDate.getHours()).toBe(14); // 2 PM | ||
| expect(mockOnDateTimeChange.mock.calls[1][1]).toBe(true); // hasTime should be true | ||
|
|
||
| // Select a minute (e.g., 45 minutes) | ||
| await user.click(screen.getByRole('button', { name: '45' })); | ||
| expect(mockOnDateTimeChange).toHaveBeenCalledTimes(3); // Third call for minute selection | ||
| calledDate = mockOnDateTimeChange.mock.calls[2][0]; | ||
| expect(calledDate.getMinutes()).toBe(45); | ||
| expect(mockOnDateTimeChange.mock.calls[2][1]).toBe(true); // hasTime should be true | ||
| }); | ||
| }); |
229 changes: 229 additions & 0 deletions
229
frontend/src/components/ui/__tests__/multi-select-filter.test.tsx
This file contains hidden or 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,229 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { MultiSelectFilter } from '../multi-select'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| describe('MultiSelectFilter', () => { | ||
| const mockOptions = ['Option A', 'Option B', 'Option C']; | ||
| const mockOnSelectionChange = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('renders with title and placeholder when no options selected', () => { | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByText('Test Filter')).toBeInTheDocument(); | ||
| expect(screen.getByRole('combobox')).toBeInTheDocument(); // The button serves as a combobox trigger | ||
| }); | ||
|
|
||
| it('renders with selected values', () => { | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={['Option A', 'Option C']} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| // Since selected values are visually indicated by checkboxes in the popover (which is closed), | ||
| // and potentially by a badge or count on the button (depending on implementation), | ||
| // let's check if the component renders without error first. | ||
| // Inspecting the component, it doesn't seem to show selected count on the button in this version, just the title. | ||
| // So we rely on the internal logic validation in interaction tests or if we open the popover. | ||
| // For this render test, ensuring it mounts with props is the baseline. | ||
| expect(screen.getByRole('combobox')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('applies custom className', () => { | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| className="custom-class" | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByRole('combobox')).toHaveClass('custom-class'); | ||
| }); | ||
|
|
||
| it('renders icon when provided', () => { | ||
| const icon = <span data-testid="test-icon">Icon</span>; | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| icon={icon} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('test-icon')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('displays completion stats correctly', async () => { | ||
| const user = userEvent.setup(); | ||
| const completionStats = { | ||
| 'Option A': { completed: 5, total: 10, percentage: 50 }, | ||
| }; | ||
|
|
||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| completionStats={completionStats} | ||
| /> | ||
| ); | ||
|
|
||
| // Open popover to see options and stats | ||
| await user.click(screen.getByRole('combobox')); | ||
|
|
||
| expect(screen.getByText('5/10 tasks, 50%')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('opens and closes the popover', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| const button = screen.getByRole('combobox'); | ||
|
|
||
| // Open | ||
| await user.click(button); | ||
| expect(screen.getByText('All Test Filter')).toBeInTheDocument(); | ||
|
|
||
| // Close by clicking button again | ||
| await user.click(button); | ||
| expect(screen.queryByText('All Test Filter')).not.toBeInTheDocument(); | ||
|
|
||
| // Open again | ||
| await user.click(button); | ||
| expect(screen.getByText('All Test Filter')).toBeInTheDocument(); | ||
|
|
||
| // Close by Escape | ||
| await user.keyboard('{Escape}'); | ||
| expect(screen.queryByText('All Test Filter')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('selects and deselects options', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={['Option A']} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| const button = screen.getByRole('combobox'); | ||
| await user.click(button); | ||
|
|
||
| // Select unselected option | ||
| await user.click(screen.getByText('Option B')); | ||
| expect(mockOnSelectionChange).toHaveBeenCalledWith([ | ||
| 'Option A', | ||
| 'Option B', | ||
| ]); | ||
|
|
||
| // Deselect selected option | ||
| await user.click(screen.getByText('Option A')); | ||
| expect(mockOnSelectionChange).toHaveBeenCalledWith([]); | ||
| }); | ||
|
|
||
| it('clears selection when "All" option is clicked', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={['Option A']} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| const button = screen.getByRole('combobox'); | ||
| await user.click(button); | ||
|
|
||
| await user.click(screen.getByText('All Test Filter')); | ||
| expect(mockOnSelectionChange).toHaveBeenCalledWith([]); | ||
| }); | ||
|
|
||
| it('filters options based on search input', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('combobox')); | ||
| const searchInput = screen.getByPlaceholderText('Search test filter...'); | ||
|
|
||
| await user.type(searchInput, 'Option A'); | ||
|
|
||
| expect(screen.getByText('Option A')).toBeInTheDocument(); | ||
| expect(screen.queryByText('Option B')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('displays "No results found" for no matches', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('combobox')); | ||
| const searchInput = screen.getByPlaceholderText('Search test filter...'); | ||
|
|
||
| await user.type(searchInput, 'Non-existent Option'); | ||
|
|
||
| expect(screen.getByText('No results found.')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('search is case-insensitive', async () => { | ||
| const user = userEvent.setup(); | ||
| render( | ||
| <MultiSelectFilter | ||
| title="Test Filter" | ||
| options={mockOptions} | ||
| selectedValues={[]} | ||
| onSelectionChange={mockOnSelectionChange} | ||
| /> | ||
| ); | ||
|
|
||
| await user.click(screen.getByRole('combobox')); | ||
| const searchInput = screen.getByPlaceholderText('Search test filter...'); | ||
|
|
||
| await user.type(searchInput, 'option a'); | ||
|
|
||
| expect(screen.getByText('Option A')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if a global change is required for this? are there any specific requirements?