|
| 1 | +import { Component, type ReactNode } from 'react'; |
| 2 | +import { act, render, waitFor } from '@testing-library/react'; |
| 3 | +import { dia } from '@joint/core'; |
| 4 | +import { GraphProvider, Paper } from '../..'; |
| 5 | +import { useCell } from '../../../hooks/use-cell'; |
| 6 | +import { ELEMENT_MODEL_TYPE } from '../../../mvc/element-model'; |
| 7 | +import { DEFAULT_CELL_NAMESPACE } from '../../../store/graph-store'; |
| 8 | +import type { Computed, ElementRecord } from '../../../types/cell.types'; |
| 9 | + |
| 10 | +interface NodeData { |
| 11 | + readonly label: string; |
| 12 | +} |
| 13 | + |
| 14 | +// Surfaces an error thrown anywhere in its subtree so the test can assert on |
| 15 | +// it instead of the throw tearing down the whole test render. |
| 16 | +class CatchErrorBoundary extends Component< |
| 17 | + Readonly<{ onCatch: (error: Error) => void; children: ReactNode }>, |
| 18 | + { hasError: boolean } |
| 19 | +> { |
| 20 | + state = { hasError: false }; |
| 21 | + static getDerivedStateFromError() { |
| 22 | + return { hasError: true }; |
| 23 | + } |
| 24 | + componentDidCatch(error: Error) { |
| 25 | + this.props.onCatch(error); |
| 26 | + } |
| 27 | + render() { |
| 28 | + return this.state.hasError ? null : this.props.children; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +const caughtState: { error: Error | null } = { error: null }; |
| 33 | +function captureError(error: Error) { |
| 34 | + caughtState.error = error; |
| 35 | +} |
| 36 | + |
| 37 | +// Subscribes to its own cell — the same shape a content-sized node gets from |
| 38 | +// useMeasureElement, so the scenario needs no explicit useCell in app code. |
| 39 | +function SubscribingNode() { |
| 40 | + const label = useCell((cell: Computed<ElementRecord<NodeData>>) => cell.data.label); |
| 41 | + return <text>{label}</text>; |
| 42 | +} |
| 43 | +const renderSubscribing = () => <SubscribingNode />; |
| 44 | + |
| 45 | +const PAPER_STYLE = { width: 400, height: 400 } as const; |
| 46 | + |
| 47 | +function elementJSON(id: string, index = 0): dia.Cell.JSON { |
| 48 | + return { |
| 49 | + id, |
| 50 | + type: ELEMENT_MODEL_TYPE, |
| 51 | + position: { x: (index % 10) * 20, y: Math.floor(index / 10) * 20 }, |
| 52 | + size: { width: 10, height: 10 }, |
| 53 | + data: { label: `label-${id}` }, |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +function createExternalGraph(): dia.Graph { |
| 58 | + return new dia.Graph({}, { cellNamespace: DEFAULT_CELL_NAMESPACE }); |
| 59 | +} |
| 60 | + |
| 61 | +// Customer scenario: the app owns an external dia.Graph and mutates it |
| 62 | +// imperatively (fromJSON load, stencil drop add→remove→re-add churn, |
| 63 | +// CommandManager undo/redo). React content must follow every membership |
| 64 | +// change of the graph — even when a coalesced commit keeps the cell COUNT |
| 65 | +// unchanged while swapping ids. |
| 66 | +describe('GraphProvider — imperative external-graph churn', () => { |
| 67 | + afterEach(() => { |
| 68 | + jest.restoreAllMocks(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('paints a cell swapped in during a same-tick remove+add (stencil churn)', async () => { |
| 72 | + const graph = createExternalGraph(); |
| 73 | + graph.addCell(elementJSON('a')); |
| 74 | + |
| 75 | + const { container } = render( |
| 76 | + <GraphProvider graph={graph}> |
| 77 | + <Paper style={PAPER_STYLE} id="churn-swap-paper" renderElement={renderSubscribing} /> |
| 78 | + </GraphProvider> |
| 79 | + ); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(container.textContent).toContain('label-a'); |
| 83 | + }); |
| 84 | + |
| 85 | + // Same tick: remove 'a', add 'b'. Both mutations coalesce into a single |
| 86 | + // container commit whose net size is unchanged. |
| 87 | + await act(async () => { |
| 88 | + graph.removeCells([graph.getCell('a')]); |
| 89 | + graph.addCell(elementJSON('b', 1)); |
| 90 | + }); |
| 91 | + |
| 92 | + await waitFor(() => { |
| 93 | + expect(container.textContent).toContain('label-b'); |
| 94 | + expect(container.textContent).not.toContain('label-a'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('paints new cells after graph.fromJSON reload with the same cell count', async () => { |
| 99 | + const graph = createExternalGraph(); |
| 100 | + graph.addCell(elementJSON('a')); |
| 101 | + graph.addCell(elementJSON('b', 1)); |
| 102 | + |
| 103 | + const { container } = render( |
| 104 | + <GraphProvider graph={graph}> |
| 105 | + <Paper style={PAPER_STYLE} id="churn-fromjson-paper" renderElement={renderSubscribing} /> |
| 106 | + </GraphProvider> |
| 107 | + ); |
| 108 | + |
| 109 | + await waitFor(() => { |
| 110 | + expect(container.textContent).toContain('label-a'); |
| 111 | + expect(container.textContent).toContain('label-b'); |
| 112 | + }); |
| 113 | + |
| 114 | + // Imperative reload: same count, entirely different ids. |
| 115 | + await act(async () => { |
| 116 | + graph.fromJSON({ cells: [elementJSON('c'), elementJSON('d', 1)] }); |
| 117 | + }); |
| 118 | + |
| 119 | + await waitFor(() => { |
| 120 | + expect(container.textContent).toContain('label-c'); |
| 121 | + expect(container.textContent).toContain('label-d'); |
| 122 | + expect(container.textContent).not.toContain('label-a'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('survives delete + undo-style re-add at scale (120 cells)', async () => { |
| 127 | + jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 128 | + caughtState.error = null; |
| 129 | + |
| 130 | + const graph = createExternalGraph(); |
| 131 | + const cellCount = 120; |
| 132 | + for (let index = 0; index < cellCount; index++) { |
| 133 | + graph.addCell(elementJSON(`el-${index}`, index)); |
| 134 | + } |
| 135 | + |
| 136 | + const { container } = render( |
| 137 | + <CatchErrorBoundary onCatch={captureError}> |
| 138 | + <GraphProvider graph={graph}> |
| 139 | + <Paper style={PAPER_STYLE} id="churn-undo-paper" renderElement={renderSubscribing} /> |
| 140 | + </GraphProvider> |
| 141 | + </CatchErrorBoundary> |
| 142 | + ); |
| 143 | + |
| 144 | + await waitFor(() => { |
| 145 | + expect(container.textContent).toContain('label-el-119'); |
| 146 | + }); |
| 147 | + |
| 148 | + const removedJSON = graph.getCell('el-115').toJSON(); |
| 149 | + |
| 150 | + // Delete at scale — regression for the historical `useCell(): no cell with |
| 151 | + // id` crash, where a stale render pass hit the already-removed cell. |
| 152 | + await act(async () => { |
| 153 | + graph.removeCells([graph.getCell('el-115')]); |
| 154 | + }); |
| 155 | + |
| 156 | + await waitFor(() => { |
| 157 | + expect(container.textContent).not.toContain('label-el-115'); |
| 158 | + }); |
| 159 | + expect(caughtState.error).toBeNull(); |
| 160 | + |
| 161 | + // Undo — CommandManager restores the cell by re-adding the same JSON. |
| 162 | + await act(async () => { |
| 163 | + graph.addCell(removedJSON); |
| 164 | + }); |
| 165 | + |
| 166 | + await waitFor(() => { |
| 167 | + expect(container.textContent).toContain('label-el-115'); |
| 168 | + }); |
| 169 | + expect(caughtState.error).toBeNull(); |
| 170 | + }); |
| 171 | +}); |
0 commit comments