|
| 1 | +import { userEvent } from '@testing-library/user-event'; |
| 2 | + |
| 3 | +import { render, screen } from '@/mocks/testing-utils'; |
| 4 | +import { FieldList as FieldListComponent } from '@/components/field/field-list'; |
| 5 | +import { EditableDiagramInteractionsProvider } from '@/hooks/use-editable-diagram-interactions'; |
| 6 | + |
| 7 | +const FieldWithEditableInteractions = ({ |
| 8 | + onAddFieldToObjectFieldClick, |
| 9 | + onFieldNameChange, |
| 10 | + onFieldClick, |
| 11 | + ...props |
| 12 | +}: Partial<React.ComponentProps<typeof FieldListComponent>> & { |
| 13 | + onAddFieldToObjectFieldClick?: () => void; |
| 14 | + onFieldNameChange?: (newName: string) => void; |
| 15 | + onFieldClick?: () => void; |
| 16 | +}) => { |
| 17 | + return ( |
| 18 | + <EditableDiagramInteractionsProvider |
| 19 | + onFieldClick={onFieldClick} |
| 20 | + onAddFieldToObjectFieldClick={onAddFieldToObjectFieldClick} |
| 21 | + onFieldNameChange={onFieldNameChange} |
| 22 | + > |
| 23 | + <FieldListComponent nodeType="collection" nodeId="coll" fields={[]} {...props} /> |
| 24 | + </EditableDiagramInteractionsProvider> |
| 25 | + ); |
| 26 | +}; |
| 27 | + |
| 28 | +describe('field-list', () => { |
| 29 | + it('Should call field click callback with field id (and fallback to name when id missing)', async () => { |
| 30 | + const onFieldClick = vi.fn(); |
| 31 | + render( |
| 32 | + <FieldWithEditableInteractions |
| 33 | + onFieldClick={onFieldClick} |
| 34 | + fields={[ |
| 35 | + { name: 'field-with-just-name', selectable: true }, |
| 36 | + { id: ['field', 'with', 'id'], name: 'and-custom-name', selectable: true }, |
| 37 | + ]} |
| 38 | + />, |
| 39 | + ); |
| 40 | + expect(screen.getByText('field-with-just-name')).toBeInTheDocument(); |
| 41 | + expect(screen.getByText('and-custom-name')).toBeInTheDocument(); |
| 42 | + |
| 43 | + await userEvent.click(screen.getByText('field-with-just-name')); |
| 44 | + expect(onFieldClick).toHaveBeenCalledTimes(1); |
| 45 | + expect(onFieldClick.mock.lastCall?.[1]).toEqual({ |
| 46 | + id: 'field-with-just-name', |
| 47 | + nodeId: 'coll', |
| 48 | + }); |
| 49 | + |
| 50 | + await userEvent.click(screen.getByText('and-custom-name')); |
| 51 | + expect(onFieldClick).toHaveBeenCalledTimes(2); |
| 52 | + expect(onFieldClick.mock.lastCall?.[1]).toEqual({ |
| 53 | + id: ['field', 'with', 'id'], |
| 54 | + nodeId: 'coll', |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments