|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { MultiSelectFilter } from '../ui/multi-select'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | + |
| 6 | +describe('MultiSelectFilter', () => { |
| 7 | + const mockOptions = ['Option A', 'Option B', 'Option C']; |
| 8 | + const mockOnSelectionChange = jest.fn(); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + jest.clearAllMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + it('renders with title and placeholder when no options selected', () => { |
| 15 | + render( |
| 16 | + <MultiSelectFilter |
| 17 | + title="Test Filter" |
| 18 | + options={mockOptions} |
| 19 | + selectedValues={[]} |
| 20 | + onSelectionChange={mockOnSelectionChange} |
| 21 | + /> |
| 22 | + ); |
| 23 | + |
| 24 | + expect(screen.getByText('Test Filter')).toBeInTheDocument(); |
| 25 | + expect(screen.getByRole('combobox')).toBeInTheDocument(); // The button serves as a combobox trigger |
| 26 | + }); |
| 27 | + |
| 28 | + it('renders with selected values', () => { |
| 29 | + render( |
| 30 | + <MultiSelectFilter |
| 31 | + title="Test Filter" |
| 32 | + options={mockOptions} |
| 33 | + selectedValues={['Option A', 'Option C']} |
| 34 | + onSelectionChange={mockOnSelectionChange} |
| 35 | + /> |
| 36 | + ); |
| 37 | + |
| 38 | + // Since selected values are visually indicated by checkboxes in the popover (which is closed), |
| 39 | + // and potentially by a badge or count on the button (depending on implementation), |
| 40 | + // let's check if the component renders without error first. |
| 41 | + // Inspecting the component, it doesn't seem to show selected count on the button in this version, just the title. |
| 42 | + // So we rely on the internal logic validation in interaction tests or if we open the popover. |
| 43 | + // For this render test, ensuring it mounts with props is the baseline. |
| 44 | + expect(screen.getByRole('combobox')).toBeInTheDocument(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('applies custom className', () => { |
| 48 | + render( |
| 49 | + <MultiSelectFilter |
| 50 | + title="Test Filter" |
| 51 | + options={mockOptions} |
| 52 | + selectedValues={[]} |
| 53 | + onSelectionChange={mockOnSelectionChange} |
| 54 | + className="custom-class" |
| 55 | + /> |
| 56 | + ); |
| 57 | + |
| 58 | + expect(screen.getByRole('combobox')).toHaveClass('custom-class'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders icon when provided', () => { |
| 62 | + const icon = <span data-testid="test-icon">Icon</span>; |
| 63 | + render( |
| 64 | + <MultiSelectFilter |
| 65 | + title="Test Filter" |
| 66 | + options={mockOptions} |
| 67 | + selectedValues={[]} |
| 68 | + onSelectionChange={mockOnSelectionChange} |
| 69 | + icon={icon} |
| 70 | + /> |
| 71 | + ); |
| 72 | + |
| 73 | + expect(screen.getByTestId('test-icon')).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('displays completion stats correctly', async () => { |
| 77 | + const user = userEvent.setup(); |
| 78 | + const completionStats = { |
| 79 | + 'Option A': { completed: 5, total: 10, percentage: 50 }, |
| 80 | + }; |
| 81 | + |
| 82 | + render( |
| 83 | + <MultiSelectFilter |
| 84 | + title="Test Filter" |
| 85 | + options={mockOptions} |
| 86 | + selectedValues={[]} |
| 87 | + onSelectionChange={mockOnSelectionChange} |
| 88 | + completionStats={completionStats} |
| 89 | + /> |
| 90 | + ); |
| 91 | + |
| 92 | + // Open popover to see options and stats |
| 93 | + await user.click(screen.getByRole('combobox')); |
| 94 | + |
| 95 | + expect(screen.getByText('5/10 tasks, 50%')).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('opens and closes the popover', async () => { |
| 99 | + const user = userEvent.setup(); |
| 100 | + render( |
| 101 | + <MultiSelectFilter |
| 102 | + title="Test Filter" |
| 103 | + options={mockOptions} |
| 104 | + selectedValues={[]} |
| 105 | + onSelectionChange={mockOnSelectionChange} |
| 106 | + /> |
| 107 | + ); |
| 108 | + |
| 109 | + const button = screen.getByRole('combobox'); |
| 110 | + |
| 111 | + // Open |
| 112 | + await user.click(button); |
| 113 | + expect(screen.getByText('All Test Filter')).toBeInTheDocument(); |
| 114 | + |
| 115 | + // Close by clicking button again |
| 116 | + await user.click(button); |
| 117 | + expect(screen.queryByText('All Test Filter')).not.toBeInTheDocument(); |
| 118 | + |
| 119 | + // Open again |
| 120 | + await user.click(button); |
| 121 | + expect(screen.getByText('All Test Filter')).toBeInTheDocument(); |
| 122 | + |
| 123 | + // Close by Escape |
| 124 | + await user.keyboard('{Escape}'); |
| 125 | + expect(screen.queryByText('All Test Filter')).not.toBeInTheDocument(); |
| 126 | + }); |
| 127 | + |
| 128 | + it('selects and deselects options', async () => { |
| 129 | + const user = userEvent.setup(); |
| 130 | + render( |
| 131 | + <MultiSelectFilter |
| 132 | + title="Test Filter" |
| 133 | + options={mockOptions} |
| 134 | + selectedValues={['Option A']} |
| 135 | + onSelectionChange={mockOnSelectionChange} |
| 136 | + /> |
| 137 | + ); |
| 138 | + |
| 139 | + const button = screen.getByRole('combobox'); |
| 140 | + await user.click(button); |
| 141 | + |
| 142 | + // Select unselected option |
| 143 | + await user.click(screen.getByText('Option B')); |
| 144 | + expect(mockOnSelectionChange).toHaveBeenCalledWith([ |
| 145 | + 'Option A', |
| 146 | + 'Option B', |
| 147 | + ]); |
| 148 | + |
| 149 | + // Deselect selected option |
| 150 | + await user.click(screen.getByText('Option A')); |
| 151 | + expect(mockOnSelectionChange).toHaveBeenCalledWith([]); |
| 152 | + }); |
| 153 | + |
| 154 | + it('clears selection when "All" option is clicked', async () => { |
| 155 | + const user = userEvent.setup(); |
| 156 | + render( |
| 157 | + <MultiSelectFilter |
| 158 | + title="Test Filter" |
| 159 | + options={mockOptions} |
| 160 | + selectedValues={['Option A']} |
| 161 | + onSelectionChange={mockOnSelectionChange} |
| 162 | + /> |
| 163 | + ); |
| 164 | + |
| 165 | + const button = screen.getByRole('combobox'); |
| 166 | + await user.click(button); |
| 167 | + |
| 168 | + await user.click(screen.getByText('All Test Filter')); |
| 169 | + expect(mockOnSelectionChange).toHaveBeenCalledWith([]); |
| 170 | + }); |
| 171 | + |
| 172 | + it('filters options based on search input', async () => { |
| 173 | + const user = userEvent.setup(); |
| 174 | + render( |
| 175 | + <MultiSelectFilter |
| 176 | + title="Test Filter" |
| 177 | + options={mockOptions} |
| 178 | + selectedValues={[]} |
| 179 | + onSelectionChange={mockOnSelectionChange} |
| 180 | + /> |
| 181 | + ); |
| 182 | + |
| 183 | + await user.click(screen.getByRole('combobox')); |
| 184 | + const searchInput = screen.getByPlaceholderText('Search test filter...'); |
| 185 | + |
| 186 | + await user.type(searchInput, 'Option A'); |
| 187 | + |
| 188 | + expect(screen.getByText('Option A')).toBeInTheDocument(); |
| 189 | + expect(screen.queryByText('Option B')).not.toBeInTheDocument(); |
| 190 | + }); |
| 191 | + |
| 192 | + it('displays "No results found" for no matches', async () => { |
| 193 | + const user = userEvent.setup(); |
| 194 | + render( |
| 195 | + <MultiSelectFilter |
| 196 | + title="Test Filter" |
| 197 | + options={mockOptions} |
| 198 | + selectedValues={[]} |
| 199 | + onSelectionChange={mockOnSelectionChange} |
| 200 | + /> |
| 201 | + ); |
| 202 | + |
| 203 | + await user.click(screen.getByRole('combobox')); |
| 204 | + const searchInput = screen.getByPlaceholderText('Search test filter...'); |
| 205 | + |
| 206 | + await user.type(searchInput, 'Non-existent Option'); |
| 207 | + |
| 208 | + expect(screen.getByText('No results found.')).toBeInTheDocument(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('search is case-insensitive', async () => { |
| 212 | + const user = userEvent.setup(); |
| 213 | + render( |
| 214 | + <MultiSelectFilter |
| 215 | + title="Test Filter" |
| 216 | + options={mockOptions} |
| 217 | + selectedValues={[]} |
| 218 | + onSelectionChange={mockOnSelectionChange} |
| 219 | + /> |
| 220 | + ); |
| 221 | + |
| 222 | + await user.click(screen.getByRole('combobox')); |
| 223 | + const searchInput = screen.getByPlaceholderText('Search test filter...'); |
| 224 | + |
| 225 | + await user.type(searchInput, 'option a'); |
| 226 | + |
| 227 | + expect(screen.getByText('Option A')).toBeInTheDocument(); |
| 228 | + }); |
| 229 | +}); |
0 commit comments