|
| 1 | +import { createMockEl } from '@test/helpers/mock-element'; |
| 2 | + |
| 3 | +import { |
| 4 | + bindSelectionChipRemove, |
| 5 | + createSelectionChip, |
| 6 | + SELECTION_CHIP_LABEL_CLASS, |
| 7 | + SELECTION_CHIP_REMOVE_CLASS, |
| 8 | + setSelectionChipLabel, |
| 9 | +} from '@/features/chat/controllers/selection-chip'; |
| 10 | + |
| 11 | +describe('selection-chip', () => { |
| 12 | + it('builds a hidden chip skeleton with icon, label and remove button', () => { |
| 13 | + const parent = createMockEl(); |
| 14 | + |
| 15 | + const chip = createSelectionChip(parent as any, 'qoderian-selection-indicator', 'text-select'); |
| 16 | + |
| 17 | + expect(chip.hasClass('qoderian-selection-indicator')).toBe(true); |
| 18 | + expect(chip.hasClass('qoderian-hidden')).toBe(true); |
| 19 | + expect(chip.querySelector('.qoderian-file-chip-icon')).not.toBeNull(); |
| 20 | + expect(chip.querySelector(`.${SELECTION_CHIP_LABEL_CLASS}`)).not.toBeNull(); |
| 21 | + const removeEl = chip.querySelector(`.${SELECTION_CHIP_REMOVE_CLASS}`); |
| 22 | + expect(removeEl).not.toBeNull(); |
| 23 | + expect(removeEl!.textContent).toBe('×'); |
| 24 | + expect(removeEl!.getAttribute('aria-label')).toBe('Remove'); |
| 25 | + }); |
| 26 | + |
| 27 | + it('writes the label into the label span, not the chip root', () => { |
| 28 | + const chip = createSelectionChip(createMockEl() as any, 'qoderian-selection-indicator', 'text-select'); |
| 29 | + |
| 30 | + setSelectionChipLabel(chip as any, '8 lines selected'); |
| 31 | + |
| 32 | + const label = chip.querySelector(`.${SELECTION_CHIP_LABEL_CLASS}`)!; |
| 33 | + expect(label.textContent).toBe('8 lines selected'); |
| 34 | + expect(chip.textContent).toBe(''); |
| 35 | + }); |
| 36 | + |
| 37 | + it('falls back to the root element when no label span exists', () => { |
| 38 | + const bare = createMockEl(); |
| 39 | + |
| 40 | + setSelectionChipLabel(bare as any, '2 lines selected'); |
| 41 | + |
| 42 | + expect(bare.textContent).toBe('2 lines selected'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('invokes onRemove when the remove button is clicked', () => { |
| 46 | + const chip = createSelectionChip(createMockEl() as any, 'qoderian-selection-indicator', 'text-select'); |
| 47 | + const onRemove = jest.fn(); |
| 48 | + bindSelectionChipRemove(chip as any, onRemove); |
| 49 | + |
| 50 | + const removeEl = chip.querySelector(`.${SELECTION_CHIP_REMOVE_CLASS}`) as any; |
| 51 | + removeEl.click(); |
| 52 | + |
| 53 | + expect(onRemove).toHaveBeenCalledTimes(1); |
| 54 | + }); |
| 55 | + |
| 56 | + it('is a no-op when the chip has no remove button', () => { |
| 57 | + const bare = createMockEl(); |
| 58 | + |
| 59 | + expect(() => bindSelectionChipRemove(bare as any, jest.fn())).not.toThrow(); |
| 60 | + }); |
| 61 | +}); |
0 commit comments