Skip to content

Commit a5695fd

Browse files
committed
wait for fitView and update tests
1 parent 128437b commit a5695fd

File tree

2 files changed

+52
-60
lines changed

2 files changed

+52
-60
lines changed

packages/compass-data-modeling/src/components/diagram-editor.tsx

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,6 @@ const DiagramEditor: React.FunctionComponent<{
133133
});
134134
}, [model?.relationships]);
135135

136-
const applyInitialLayout = useCallback(async () => {
137-
try {
138-
const { nodes: positionedNodes } = await applyLayout(
139-
nodes,
140-
edges,
141-
'LEFT_RIGHT'
142-
);
143-
onApplyInitialLayout(
144-
Object.fromEntries(
145-
positionedNodes.map((node) => [
146-
node.id,
147-
[node.position.x, node.position.y],
148-
])
149-
)
150-
);
151-
} catch (err) {
152-
log.error(
153-
mongoLogId(1_001_000_361),
154-
'DiagramEditor',
155-
'Error applying layout:',
156-
err
157-
);
158-
}
159-
}, [edges, log, mongoLogId, onApplyInitialLayout]);
160-
161136
const nodes = useMemo<NodeProps[]>(() => {
162137
return (model?.collections ?? []).map(
163138
(coll): NodeProps => ({
@@ -188,6 +163,31 @@ const DiagramEditor: React.FunctionComponent<{
188163
);
189164
}, [model?.collections]);
190165

166+
const applyInitialLayout = useCallback(async () => {
167+
try {
168+
const { nodes: positionedNodes } = await applyLayout(
169+
nodes,
170+
edges,
171+
'LEFT_RIGHT'
172+
);
173+
onApplyInitialLayout(
174+
Object.fromEntries(
175+
positionedNodes.map((node) => [
176+
node.id,
177+
[node.position.x, node.position.y],
178+
])
179+
)
180+
);
181+
} catch (err) {
182+
log.error(
183+
mongoLogId(1_001_000_361),
184+
'DiagramEditor',
185+
'Error applying layout:',
186+
err
187+
);
188+
}
189+
}, [edges, log, nodes, mongoLogId, onApplyInitialLayout]);
190+
191191
useEffect(() => {
192192
if (nodes.length === 0) return;
193193
const isInitialState = nodes.some(
@@ -198,8 +198,10 @@ const DiagramEditor: React.FunctionComponent<{
198198
return;
199199
}
200200
if (!areNodesReady) {
201-
void diagram.fitView();
202201
setAreNodesReady(true);
202+
setTimeout(() => {
203+
void diagram.fitView();
204+
});
203205
}
204206
}, [areNodesReady, nodes, diagram, applyInitialLayout]);
205207

@@ -256,6 +258,10 @@ const DiagramEditor: React.FunctionComponent<{
256258
minZoom: 0.25,
257259
}}
258260
onNodeDragStop={(evt, node) => {
261+
console.log('Node drag stopped:', node.id, [
262+
node.position.x,
263+
node.position.y,
264+
]);
259265
onMoveCollection(node.id, [node.position.x, node.position.y]);
260266
}}
261267
/>

packages/compass-e2e-tests/tests/data-modeling-tab.test.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -132,78 +132,64 @@ describe('Data Modeling tab', function () {
132132
const dataModelEditor = browser.$(Selectors.DataModelEditor);
133133
await dataModelEditor.waitForDisplayed();
134134

135-
let nodes = await getDiagramNodes(browser);
135+
const nodes = await getDiagramNodes(browser);
136136
expect(nodes).to.have.lengthOf(2);
137137
expect(nodes[0].id).to.equal('test.testCollection1');
138138
expect(nodes[1].id).to.equal('test.testCollection2');
139139

140140
// Apply change to the model
141-
const originalPosition = {
142-
x: Math.round(nodes[0].position.x),
143-
y: Math.round(nodes[0].position.y),
144-
};
145141

146142
// react flow uses its own coordinate system,
147143
// so we get the node element location for the pointer action
148-
const startPosition = await browser
149-
.$(Selectors.DataModelPreviewCollection('test.testCollection1'))
150-
.getLocation();
144+
const testCollection1 = browser.$(
145+
Selectors.DataModelPreviewCollection('test.testCollection1')
146+
);
147+
const startPosition = await testCollection1.getLocation();
148+
const nodeSize = await testCollection1.getSize();
151149

152150
// This should move the node 20px to the right but it just moves it "somewhere else"
153151
// That's the best I could do
154152
await browser
155153
.action('pointer')
156154
.move({
157-
x: Math.round(startPosition.x) + 10,
158-
y: Math.round(startPosition.y) + 10,
155+
x: Math.round(startPosition.x) + nodeSize.width / 2,
156+
y: Math.round(startPosition.y) + nodeSize.height / 2,
159157
})
160158
.down({ button: 0 }) // Left mouse button
161-
.move({ x: 10, y: 0, duration: 100 })
162159
.pause(1000)
163-
.move({ x: 10, y: 0, duration: 100 })
160+
.move({ x: 100, y: 0, duration: 1000, origin: 'pointer' })
161+
.pause(1000)
162+
.move({ x: 100, y: 0, duration: 1000, origin: 'pointer' })
164163
.up({ button: 0 }) // Release the left mouse button
165164
.perform();
166165
await browser.waitForAnimations(dataModelEditor);
167166

168167
// Check that the first node has moved and mark the new position
169-
nodes = await getDiagramNodes(browser);
170-
expect(nodes).to.have.lengthOf(2);
171-
expect(Math.round(nodes[0].position.x)).not.to.equal(originalPosition.x);
172-
expect(Math.round(nodes[0].position.y)).not.to.equal(originalPosition.y);
173-
const newPosition = {
174-
x: Math.round(nodes[0].position.x),
175-
y: Math.round(nodes[0].position.y),
176-
};
168+
const newPosition = await testCollection1.getLocation();
169+
expect(newPosition).not.to.deep.equal(startPosition);
177170

178171
// Undo the change
179172
await browser.clickVisible(Selectors.DataModelUndoButton);
180173
await browser.waitForAnimations(dataModelEditor);
181-
nodes = await getDiagramNodes(browser);
182-
expect(nodes).to.have.lengthOf(2);
183-
expect(Math.round(nodes[0].position.x)).to.equal(originalPosition.x);
184-
expect(Math.round(nodes[0].position.y)).to.equal(originalPosition.y);
174+
const positionAfterUndone = await testCollection1.getLocation();
175+
expect(positionAfterUndone).to.deep.equal(startPosition);
185176

186177
// Redo the change
187178
await browser.waitForAriaDisabled(Selectors.DataModelRedoButton, false);
188179
await browser.clickVisible(Selectors.DataModelRedoButton);
189180
await browser.waitForAnimations(dataModelEditor);
190-
nodes = await getDiagramNodes(browser);
191-
expect(nodes).to.have.lengthOf(2);
192-
expect(Math.round(nodes[0].position.x)).to.equal(newPosition.x);
193-
expect(Math.round(nodes[0].position.y)).to.equal(newPosition.y);
194-
181+
const positionAfterRedo = await testCollection1.getLocation();
182+
expect(positionAfterRedo).to.deep.equal(newPosition);
195183
// Open a new tab
196184
await browser.openNewTab();
197185

198186
// Open the saved diagram
199187
await browser.clickVisible(Selectors.DataModelsListItem(dataModelName));
200188
await browser.$(Selectors.DataModelEditor).waitForDisplayed();
201189

202-
// Verify that the diagram has the latest changes
203-
nodes = await getDiagramNodes(browser);
204-
expect(nodes).to.have.lengthOf(2);
205-
expect(Math.round(nodes[0].position.x)).to.equal(newPosition.x);
206-
expect(Math.round(nodes[0].position.y)).to.equal(newPosition.y);
190+
// TODO: Verify that the diagram has the latest changes COMPASS-9479
191+
const savedNodes = await getDiagramNodes(browser);
192+
expect(savedNodes).to.have.lengthOf(2);
207193

208194
// Open a new tab
209195
await browser.openNewTab();

0 commit comments

Comments
 (0)