|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { render, screen, within } from '@testing-library/react' |
| 3 | + |
| 4 | +import { DragDropCustomList, DragDropCustomItem } from './index' |
| 5 | + |
| 6 | +const renderList = (ids: string[], onDragEnd = vi.fn()) => |
| 7 | + render( |
| 8 | + <DragDropCustomList ids={ids} onDragEnd={onDragEnd}> |
| 9 | + {ids.map((id) => ( |
| 10 | + <DragDropCustomItem id={id} key={id}> |
| 11 | + {({ attributes, isDragging, listeners }) => ( |
| 12 | + <div |
| 13 | + data-dragging={isDragging} |
| 14 | + data-testid={`item-${id}`} |
| 15 | + {...attributes} |
| 16 | + {...listeners} |
| 17 | + > |
| 18 | + {id} |
| 19 | + </div> |
| 20 | + )} |
| 21 | + </DragDropCustomItem> |
| 22 | + ))} |
| 23 | + </DragDropCustomList>, |
| 24 | + ) |
| 25 | + |
| 26 | +describe('DragDropCustom', () => { |
| 27 | + it('renders the list wrapper and all items', () => { |
| 28 | + const { container } = renderList(['a', 'b', 'c']) |
| 29 | + |
| 30 | + expect(container.querySelector('.files-draggable')).toBeInTheDocument() |
| 31 | + expect(screen.getByTestId('item-a')).toHaveTextContent('a') |
| 32 | + expect(screen.getByTestId('item-b')).toHaveTextContent('b') |
| 33 | + expect(screen.getByTestId('item-c')).toHaveTextContent('c') |
| 34 | + }) |
| 35 | + |
| 36 | + it('wraps each child in a draggable item element', () => { |
| 37 | + const { container } = renderList(['a', 'b']) |
| 38 | + |
| 39 | + const items = container.querySelectorAll('.files-draggable__item') |
| 40 | + expect(items).toHaveLength(2) |
| 41 | + }) |
| 42 | + |
| 43 | + it('exposes a render-prop API with attributes, isDragging and listeners', () => { |
| 44 | + const child = vi.fn((_props: unknown) => <div data-testid="rp">x</div>) |
| 45 | + |
| 46 | + render( |
| 47 | + <DragDropCustomList ids={['only']} onDragEnd={vi.fn()}> |
| 48 | + <DragDropCustomItem id="only">{child}</DragDropCustomItem> |
| 49 | + </DragDropCustomList>, |
| 50 | + ) |
| 51 | + |
| 52 | + expect(child).toHaveBeenCalled() |
| 53 | + const args = child.mock.calls[0]![0] as { |
| 54 | + attributes: Record<string, unknown> |
| 55 | + isDragging: boolean |
| 56 | + listeners: { onPointerDown?: unknown } |
| 57 | + } |
| 58 | + expect(args).toHaveProperty('attributes') |
| 59 | + expect(args.isDragging).toBe(false) |
| 60 | + expect(typeof args.listeners.onPointerDown).toBe('function') |
| 61 | + }) |
| 62 | + |
| 63 | + it('starts each item in a non-dragging state', () => { |
| 64 | + renderList(['a', 'b']) |
| 65 | + expect(screen.getByTestId('item-a')).toHaveAttribute('data-dragging', 'false') |
| 66 | + expect(screen.getByTestId('item-b')).toHaveAttribute('data-dragging', 'false') |
| 67 | + }) |
| 68 | + |
| 69 | + it('renders a list wrapper for an empty set of ids', () => { |
| 70 | + const { container } = render( |
| 71 | + <DragDropCustomList ids={[]} onDragEnd={vi.fn()}> |
| 72 | + {null} |
| 73 | + </DragDropCustomList>, |
| 74 | + ) |
| 75 | + const list = container.querySelector('.files-draggable') |
| 76 | + expect(list).toBeInTheDocument() |
| 77 | + expect(within(list as HTMLElement).queryByTestId(/item-/)).toBeNull() |
| 78 | + }) |
| 79 | +}) |
0 commit comments