| 
 | 1 | +import { Node, Position, useNodes } from '@xyflow/react';  | 
 | 2 | +import { ComponentProps } from 'react';  | 
 | 3 | + | 
 | 4 | +import { EMPLOYEES_NODE, ORDERS_NODE } from '@/mocks/datasets/nodes';  | 
 | 5 | +import { render, screen } from '@/mocks/testing-utils';  | 
 | 6 | +import { FieldEdge } from '@/components/edge/field-edge';  | 
 | 7 | + | 
 | 8 | +vi.mock('@xyflow/react', async () => {  | 
 | 9 | +  const actual = await vi.importActual<typeof import('@xyflow/react')>('@xyflow/react');  | 
 | 10 | +  return {  | 
 | 11 | +    ...actual,  | 
 | 12 | +    useNodes: vi.fn(),  | 
 | 13 | +  };  | 
 | 14 | +});  | 
 | 15 | + | 
 | 16 | +function mockNodes(nodes: Node[]) {  | 
 | 17 | +  const mockedNodes = vi.mocked(useNodes);  | 
 | 18 | +  mockedNodes.mockReturnValue(nodes);  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +describe('field-edge', () => {  | 
 | 22 | +  const nodes = [  | 
 | 23 | +    { ...ORDERS_NODE, data: { title: ORDERS_NODE.title, fields: ORDERS_NODE.fields } },  | 
 | 24 | +    { ...EMPLOYEES_NODE, data: { title: EMPLOYEES_NODE.title, fields: EMPLOYEES_NODE.fields } },  | 
 | 25 | +  ];  | 
 | 26 | + | 
 | 27 | +  beforeEach(() => {  | 
 | 28 | +    mockNodes(nodes);  | 
 | 29 | +  });  | 
 | 30 | + | 
 | 31 | +  afterEach(() => {  | 
 | 32 | +    vi.clearAllMocks();  | 
 | 33 | +  });  | 
 | 34 | + | 
 | 35 | +  const renderComponent = (props?: Partial<ComponentProps<typeof FieldEdge>>) => {  | 
 | 36 | +    return render(  | 
 | 37 | +      <FieldEdge  | 
 | 38 | +        sourceX={100}  | 
 | 39 | +        sourceY={100}  | 
 | 40 | +        targetX={500}  | 
 | 41 | +        targetY={500}  | 
 | 42 | +        sourcePosition={Position.Left}  | 
 | 43 | +        targetPosition={Position.Top}  | 
 | 44 | +        id={'orders-to-employees'}  | 
 | 45 | +        source={'orders'}  | 
 | 46 | +        target={'employees'}  | 
 | 47 | +        data={{ sourceFieldIndex: 0, targetFieldIndex: 1 }}  | 
 | 48 | +        {...props}  | 
 | 49 | +      />,  | 
 | 50 | +    );  | 
 | 51 | +  };  | 
 | 52 | + | 
 | 53 | +  describe('With the nodes positioned above to each other', () => {  | 
 | 54 | +    it('Should render edge', () => {  | 
 | 55 | +      mockNodes([  | 
 | 56 | +        {  | 
 | 57 | +          ...nodes[0],  | 
 | 58 | +          position: { x: 100, y: 100 },  | 
 | 59 | +        },  | 
 | 60 | +        {  | 
 | 61 | +          ...nodes[1],  | 
 | 62 | +          position: { x: 100, y: 250 },  | 
 | 63 | +        },  | 
 | 64 | +      ]);  | 
 | 65 | +      renderComponent();  | 
 | 66 | +      const path = screen.getByTestId('field-edge-orders-to-employees');  | 
 | 67 | +      expect(path).toHaveAttribute('id', 'orders-to-employees');  | 
 | 68 | +      expect(path).toHaveAttribute(  | 
 | 69 | +        'd',  | 
 | 70 | +        'M351.5 147L 366.5,147Q 371.5,147 371.5,152L 371.5,310Q 371.5,315 366.5,315L352.5 315L351.5 315',  | 
 | 71 | +      );  | 
 | 72 | +      // sense check that the line didn't become more complex  | 
 | 73 | +      const lines = path.getAttribute('d')?.split(/L\s*/);  | 
 | 74 | +      expect(lines?.length).toBeLessThanOrEqual(5);  | 
 | 75 | +    });  | 
 | 76 | +  });  | 
 | 77 | + | 
 | 78 | +  describe('With the nodes positioned above each other', () => {  | 
 | 79 | +    it('Should render edge', () => {  | 
 | 80 | +      mockNodes([  | 
 | 81 | +        {  | 
 | 82 | +          ...nodes[0],  | 
 | 83 | +          position: { x: 100, y: 100 },  | 
 | 84 | +        },  | 
 | 85 | +        {  | 
 | 86 | +          ...nodes[1],  | 
 | 87 | +          position: { x: 500, y: 100 },  | 
 | 88 | +        },  | 
 | 89 | +      ]);  | 
 | 90 | +      renderComponent();  | 
 | 91 | +      const path = screen.getByTestId('field-edge-orders-to-employees');  | 
 | 92 | +      expect(path).toHaveAttribute('id', 'orders-to-employees');  | 
 | 93 | +      expect(path).toHaveAttribute(  | 
 | 94 | +        'd',  | 
 | 95 | +        'M351.5 147L371.5 147L 417,147Q 422,147 422,152L 422,160Q 422,165 427,165L472.5 165L492.5 165',  | 
 | 96 | +      );  | 
 | 97 | +      // sense check that the line didn't become more complex  | 
 | 98 | +      const lines = path.getAttribute('d')?.split(/L\s*/);  | 
 | 99 | +      expect(lines?.length).toBeLessThanOrEqual(6);  | 
 | 100 | +    });  | 
 | 101 | +  });  | 
 | 102 | + | 
 | 103 | +  it('Should not render edge if source does not exist', () => {  | 
 | 104 | +    renderComponent({ source: 'unknown' });  | 
 | 105 | +    expect(screen.queryByTestId('field-edge-orders-to-employees')).not.toBeInTheDocument();  | 
 | 106 | +  });  | 
 | 107 | + | 
 | 108 | +  it('Should not render edge if target does not exist', () => {  | 
 | 109 | +    renderComponent({ target: 'unknown' });  | 
 | 110 | +    expect(screen.queryByTestId('field-edge-orders-to-employees')).not.toBeInTheDocument();  | 
 | 111 | +  });  | 
 | 112 | +});  | 
0 commit comments