|
| 1 | +import { useCallback } from 'react'; |
| 2 | +import { render, waitFor } from '@testing-library/react'; |
| 3 | +import { GraphProvider, Paper } from '../../components'; |
| 4 | +import { usePaper } from '../../hooks/use-paper'; |
| 5 | +import type { PaperView } from '../../mvc/paper'; |
| 6 | +import { ELEMENT_MODEL_TYPE } from '../../mvc/element-model'; |
| 7 | +import type { CellRecord } from '../../types/cell.types'; |
| 8 | + |
| 9 | +// A button inside a node must be clickable AND draggable: dragging it moves the element |
| 10 | +// (or starts a link when it sits in a magnet), while a press-and-release in place is just |
| 11 | +// a click. joint-core blocks every form control from starting an interaction, so the |
| 12 | +// preset narrows `PREVENT_INTERACTION_TAG_NAMES` to leave `BUTTON` out — and `pointerup` |
| 13 | +// withholds the native click once the gesture has moved, since a node dragged by its own |
| 14 | +// button ends the gesture back on that button and would otherwise fire `onClick`. |
| 15 | + |
| 16 | +const ELEMENT_ID = 'cell-1'; |
| 17 | + |
| 18 | +beforeAll(() => { |
| 19 | + // jsdom has no layout, so it ships no `elementFromPoint`; joint-core reaches for it |
| 20 | + // while a drag is in flight (`CellView#getEventTarget`). Nothing here depends on the |
| 21 | + // node it would return. |
| 22 | + document.elementFromPoint = () => null; |
| 23 | +}); |
| 24 | + |
| 25 | +const initialCells: readonly CellRecord[] = [ |
| 26 | + { |
| 27 | + id: ELEMENT_ID, |
| 28 | + type: ELEMENT_MODEL_TYPE, |
| 29 | + position: { x: 0, y: 0 }, |
| 30 | + size: { width: 80, height: 40 }, |
| 31 | + } as CellRecord, |
| 32 | +]; |
| 33 | + |
| 34 | +let capturedPaper: PaperView | null = null; |
| 35 | +let capturedButton: HTMLButtonElement | null = null; |
| 36 | + |
| 37 | +function Capture() { |
| 38 | + capturedPaper = usePaper().paper; |
| 39 | + return null; |
| 40 | +} |
| 41 | + |
| 42 | +const PAPER_STYLE = { width: 200, height: 200 }; |
| 43 | + |
| 44 | +/** Reassigned per test, read at render time by the button's `onClick`. */ |
| 45 | +let onButtonClick: () => void = () => {}; |
| 46 | + |
| 47 | +function NodeWithButton() { |
| 48 | + const buttonRef = useCallback((node: HTMLButtonElement | null) => { |
| 49 | + capturedButton = node; |
| 50 | + }, []); |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <rect width={80} height={40} fill="#1c2836" /> |
| 54 | + <foreignObject width={80} height={40}> |
| 55 | + <button type="button" ref={buttonRef} onClick={onButtonClick}> |
| 56 | + press |
| 57 | + </button> |
| 58 | + </foreignObject> |
| 59 | + </> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +const renderElement = () => <NodeWithButton />; |
| 64 | + |
| 65 | +/** |
| 66 | + * Mount a paper holding one element whose markup contains a `<button>`. |
| 67 | + * @param clickSpy - Spy for the button's React `onClick`. |
| 68 | + * @returns The paper and the rendered `<button>`. |
| 69 | + */ |
| 70 | +async function renderNodeWithButton( |
| 71 | + clickSpy: () => void |
| 72 | +): Promise<{ paper: PaperView; button: HTMLButtonElement }> { |
| 73 | + capturedPaper = null; |
| 74 | + capturedButton = null; |
| 75 | + onButtonClick = clickSpy; |
| 76 | + render( |
| 77 | + <GraphProvider initialCells={initialCells}> |
| 78 | + <Paper style={PAPER_STYLE} renderElement={renderElement}> |
| 79 | + <Capture /> |
| 80 | + </Paper> |
| 81 | + </GraphProvider> |
| 82 | + ); |
| 83 | + await waitFor(() => { |
| 84 | + expect(capturedPaper).not.toBeNull(); |
| 85 | + expect(capturedButton).not.toBeNull(); |
| 86 | + }); |
| 87 | + const paper = capturedPaper; |
| 88 | + const button = capturedButton; |
| 89 | + if (!paper || !button) throw new Error('paper or button not mounted'); |
| 90 | + return { paper, button }; |
| 91 | +} |
| 92 | + |
| 93 | +function dispatchAt(node: EventTarget, type: string, clientX: number, clientY: number) { |
| 94 | + node.dispatchEvent(new MouseEvent(type, { clientX, clientY, bubbles: true, cancelable: true })); |
| 95 | +} |
| 96 | + |
| 97 | +/** A real browser fires `click` on the pressed node after `mouseup` — jsdom does not. */ |
| 98 | +function dispatchClick(node: EventTarget) { |
| 99 | + dispatchAt(node, 'click', 0, 0); |
| 100 | +} |
| 101 | + |
| 102 | +describe('a button inside a node', () => { |
| 103 | + it('moves the element when dragged, and does not click', async () => { |
| 104 | + const clickSpy = jest.fn(); |
| 105 | + const { paper, button } = await renderNodeWithButton(clickSpy); |
| 106 | + const element = paper.model.getCell(ELEMENT_ID); |
| 107 | + const positionBefore = { ...element.get('position') }; |
| 108 | + |
| 109 | + dispatchAt(button, 'mousedown', 10, 10); |
| 110 | + // `clickThreshold` counts move events, not pixels, so a drag has to be more than a |
| 111 | + // single jump for joint-core to stop treating the gesture as a click. |
| 112 | + for (let step = 1; step <= 8; step += 1) { |
| 113 | + dispatchAt(document, 'pointermove', 10 + step * 10, 10 + step * 10); |
| 114 | + } |
| 115 | + dispatchAt(document, 'pointerup', 90, 90); |
| 116 | + dispatchClick(button); |
| 117 | + |
| 118 | + expect(element.get('position')).not.toEqual(positionBefore); |
| 119 | + expect(clickSpy).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('clicks when pressed without moving, and does not move the element', async () => { |
| 123 | + const clickSpy = jest.fn(); |
| 124 | + const { paper, button } = await renderNodeWithButton(clickSpy); |
| 125 | + const element = paper.model.getCell(ELEMENT_ID); |
| 126 | + const positionBefore = { ...element.get('position') }; |
| 127 | + |
| 128 | + dispatchAt(button, 'mousedown', 10, 10); |
| 129 | + dispatchAt(document, 'pointerup', 10, 10); |
| 130 | + dispatchClick(button); |
| 131 | + |
| 132 | + expect(element.get('position')).toEqual(positionBefore); |
| 133 | + expect(clickSpy).toHaveBeenCalledTimes(1); |
| 134 | + }); |
| 135 | +}); |
0 commit comments