|
| 1 | +import { render, screen } from '@testing-library/vue' |
| 2 | + |
| 3 | +import Combobox from '../Combobox.vue' |
| 4 | + |
| 5 | +const defaultOptions = [ |
| 6 | + { value: 'a', label: 'Option A' }, |
| 7 | + { value: 'b', label: 'Option B' }, |
| 8 | + { value: 'c', label: 'Option C' }, |
| 9 | + { value: 'd', label: 'Option D' }, |
| 10 | + { value: 'e', label: 'Option E' } |
| 11 | +] |
| 12 | + |
| 13 | +describe('Combobox.vue - Multi-select tag display', () => { |
| 14 | + describe('maxTags prop', () => { |
| 15 | + it('displays all tags when maxTags is undefined', () => { |
| 16 | + render(Combobox, { |
| 17 | + props: { |
| 18 | + options: defaultOptions, |
| 19 | + modelValue: ['a', 'b', 'c', 'd', 'e'] |
| 20 | + } |
| 21 | + }) |
| 22 | + |
| 23 | + expect(screen.getByText('Option A')).toBeInTheDocument() |
| 24 | + expect(screen.getByText('Option B')).toBeInTheDocument() |
| 25 | + expect(screen.getByText('Option C')).toBeInTheDocument() |
| 26 | + expect(screen.getByText('Option D')).toBeInTheDocument() |
| 27 | + expect(screen.getByText('Option E')).toBeInTheDocument() |
| 28 | + }) |
| 29 | + |
| 30 | + it('limits visible tags to maxTags count', () => { |
| 31 | + render(Combobox, { |
| 32 | + props: { |
| 33 | + options: defaultOptions, |
| 34 | + modelValue: ['a', 'b', 'c', 'd', 'e'], |
| 35 | + maxTags: 2 |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + expect(screen.getByText('Option A')).toBeInTheDocument() |
| 40 | + expect(screen.getByText('Option B')).toBeInTheDocument() |
| 41 | + expect(screen.queryByText('Option C')).not.toBeInTheDocument() |
| 42 | + expect(screen.queryByText('Option D')).not.toBeInTheDocument() |
| 43 | + expect(screen.queryByText('Option E')).not.toBeInTheDocument() |
| 44 | + }) |
| 45 | + |
| 46 | + it('shows collapsed indicator when tags exceed maxTags', () => { |
| 47 | + render(Combobox, { |
| 48 | + props: { |
| 49 | + options: defaultOptions, |
| 50 | + modelValue: ['a', 'b', 'c', 'd', 'e'], |
| 51 | + maxTags: 2 |
| 52 | + } |
| 53 | + }) |
| 54 | + |
| 55 | + expect(screen.getByText('+3 more')).toBeInTheDocument() |
| 56 | + }) |
| 57 | + |
| 58 | + it('does not show indicator when maxTags >= selected count', () => { |
| 59 | + render(Combobox, { |
| 60 | + props: { |
| 61 | + options: defaultOptions, |
| 62 | + modelValue: ['a', 'b'], |
| 63 | + maxTags: 3 |
| 64 | + } |
| 65 | + }) |
| 66 | + |
| 67 | + expect(screen.queryByText(/more/)).not.toBeInTheDocument() |
| 68 | + }) |
| 69 | + |
| 70 | + it('handles maxTags: 0 showing only indicator', () => { |
| 71 | + render(Combobox, { |
| 72 | + props: { |
| 73 | + options: defaultOptions, |
| 74 | + modelValue: ['a', 'b', 'c'], |
| 75 | + maxTags: 0 |
| 76 | + } |
| 77 | + }) |
| 78 | + |
| 79 | + expect(screen.queryByText('Option A')).not.toBeInTheDocument() |
| 80 | + expect(screen.queryByText('Option B')).not.toBeInTheDocument() |
| 81 | + expect(screen.queryByText('Option C')).not.toBeInTheDocument() |
| 82 | + expect(screen.getByText('+3 more')).toBeInTheDocument() |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + describe('collapsedTagsLabel prop', () => { |
| 87 | + it('uses default label format "+N more"', () => { |
| 88 | + render(Combobox, { |
| 89 | + props: { |
| 90 | + options: defaultOptions, |
| 91 | + modelValue: ['a', 'b', 'c', 'd'], |
| 92 | + maxTags: 1 |
| 93 | + } |
| 94 | + }) |
| 95 | + |
| 96 | + expect(screen.getByText('+3 more')).toBeInTheDocument() |
| 97 | + }) |
| 98 | + |
| 99 | + it('uses custom label function when provided', () => { |
| 100 | + render(Combobox, { |
| 101 | + props: { |
| 102 | + options: defaultOptions, |
| 103 | + modelValue: ['a', 'b', 'c', 'd'], |
| 104 | + maxTags: 1, |
| 105 | + collapsedTagsLabel: (count: number) => `and ${count} others` |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + expect(screen.getByText('and 3 others')).toBeInTheDocument() |
| 110 | + }) |
| 111 | + |
| 112 | + it('passes correct hidden count to label function', () => { |
| 113 | + const labelFn = vi.fn((count: number) => `(${count} hidden)`) |
| 114 | + |
| 115 | + render(Combobox, { |
| 116 | + props: { |
| 117 | + options: defaultOptions, |
| 118 | + modelValue: ['a', 'b', 'c', 'd', 'e'], |
| 119 | + maxTags: 2, |
| 120 | + collapsedTagsLabel: labelFn |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + expect(labelFn).toHaveBeenCalledWith(3) |
| 125 | + expect(screen.getByText('(3 hidden)')).toBeInTheDocument() |
| 126 | + }) |
| 127 | + }) |
| 128 | + |
| 129 | + describe('#selected-tags slot', () => { |
| 130 | + it('renders custom content when slot is used', () => { |
| 131 | + render(Combobox, { |
| 132 | + props: { |
| 133 | + options: defaultOptions, |
| 134 | + modelValue: ['a', 'b', 'c'] |
| 135 | + }, |
| 136 | + slots: { |
| 137 | + 'selected-tags': '<span data-testid="custom-tags">Custom tags content</span>' |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + expect(screen.getByTestId('custom-tags')).toBeInTheDocument() |
| 142 | + expect(screen.getByText('Custom tags content')).toBeInTheDocument() |
| 143 | + }) |
| 144 | + |
| 145 | + it('provides selectedValues slot prop', () => { |
| 146 | + render(Combobox, { |
| 147 | + props: { |
| 148 | + options: defaultOptions, |
| 149 | + modelValue: ['a', 'b', 'c'] |
| 150 | + }, |
| 151 | + slots: { |
| 152 | + 'selected-tags': ` |
| 153 | + <template #selected-tags="{ selectedValues }"> |
| 154 | + <span data-testid="count">{{ selectedValues.length }} selected</span> |
| 155 | + </template> |
| 156 | + ` |
| 157 | + } |
| 158 | + }) |
| 159 | + |
| 160 | + expect(screen.getByTestId('count')).toHaveTextContent('3 selected') |
| 161 | + }) |
| 162 | + |
| 163 | + it('provides visibleValues slot prop respecting maxTags', () => { |
| 164 | + render(Combobox, { |
| 165 | + props: { |
| 166 | + options: defaultOptions, |
| 167 | + modelValue: ['a', 'b', 'c', 'd', 'e'], |
| 168 | + maxTags: 2 |
| 169 | + }, |
| 170 | + slots: { |
| 171 | + 'selected-tags': ` |
| 172 | + <template #selected-tags="{ visibleValues }"> |
| 173 | + <span data-testid="visible">{{ visibleValues.length }} visible</span> |
| 174 | + </template> |
| 175 | + ` |
| 176 | + } |
| 177 | + }) |
| 178 | + |
| 179 | + expect(screen.getByTestId('visible')).toHaveTextContent('2 visible') |
| 180 | + }) |
| 181 | + |
| 182 | + it('provides hiddenCount slot prop', () => { |
| 183 | + render(Combobox, { |
| 184 | + props: { |
| 185 | + options: defaultOptions, |
| 186 | + modelValue: ['a', 'b', 'c', 'd', 'e'], |
| 187 | + maxTags: 2 |
| 188 | + }, |
| 189 | + slots: { |
| 190 | + 'selected-tags': ` |
| 191 | + <template #selected-tags="{ hiddenCount }"> |
| 192 | + <span data-testid="hidden">{{ hiddenCount }} hidden</span> |
| 193 | + </template> |
| 194 | + ` |
| 195 | + } |
| 196 | + }) |
| 197 | + |
| 198 | + expect(screen.getByTestId('hidden')).toHaveTextContent('3 hidden') |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + describe('edge cases', () => { |
| 203 | + it('ignores maxTags in single-select mode', () => { |
| 204 | + render(Combobox, { |
| 205 | + props: { |
| 206 | + options: defaultOptions, |
| 207 | + modelValue: 'a', |
| 208 | + maxTags: 0 |
| 209 | + } |
| 210 | + }) |
| 211 | + |
| 212 | + // In single-select mode, the selected value is displayed as text, not tags |
| 213 | + // The maxTags prop should have no effect |
| 214 | + expect(screen.queryByText('+1 more')).not.toBeInTheDocument() |
| 215 | + }) |
| 216 | + |
| 217 | + it('does not render tags in inline mode', () => { |
| 218 | + render(Combobox, { |
| 219 | + props: { |
| 220 | + options: defaultOptions, |
| 221 | + modelValue: ['a', 'b', 'c'], |
| 222 | + inline: true |
| 223 | + } |
| 224 | + }) |
| 225 | + |
| 226 | + // In inline mode, tags are not rendered (existing behavior) |
| 227 | + // Just verify no tags are shown |
| 228 | + expect(screen.queryByRole('button', { name: /Option A/i })).not.toBeInTheDocument() |
| 229 | + }) |
| 230 | + |
| 231 | + it('handles empty selection', () => { |
| 232 | + render(Combobox, { |
| 233 | + props: { |
| 234 | + options: defaultOptions, |
| 235 | + modelValue: [], |
| 236 | + maxTags: 2 |
| 237 | + } |
| 238 | + }) |
| 239 | + |
| 240 | + expect(screen.queryByText(/more/)).not.toBeInTheDocument() |
| 241 | + }) |
| 242 | + |
| 243 | + it('handles selection with values not in options', () => { |
| 244 | + render(Combobox, { |
| 245 | + props: { |
| 246 | + options: defaultOptions, |
| 247 | + modelValue: ['unknown1', 'unknown2', 'unknown3'], |
| 248 | + maxTags: 1 |
| 249 | + } |
| 250 | + }) |
| 251 | + |
| 252 | + // Should show the raw value when option not found |
| 253 | + expect(screen.getByText('unknown1')).toBeInTheDocument() |
| 254 | + expect(screen.getByText('+2 more')).toBeInTheDocument() |
| 255 | + }) |
| 256 | + }) |
| 257 | +}) |
0 commit comments