Skip to content

Commit c187490

Browse files
committed
more cleanup and tests
1 parent 942549c commit c187490

File tree

2 files changed

+48
-99
lines changed

2 files changed

+48
-99
lines changed

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

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { connect } from 'react-redux';
99
import type { MongoDBJSONSchema } from 'mongodb-schema';
1010
import type { DataModelingState } from '../store/reducer';
1111
import {
12-
applyEdit,
1312
applyInitialLayout,
1413
moveCollection,
1514
getCurrentDiagramFromState,
@@ -24,11 +23,8 @@ import {
2423
css,
2524
spacing,
2625
Button,
27-
palette,
28-
ErrorSummary,
2926
useDarkMode,
3027
} from '@mongodb-js/compass-components';
31-
import { CodemirrorMultilineEditor } from '@mongodb-js/compass-editor';
3228
import { cancelAnalysis, retryAnalysis } from '../store/analysis-process';
3329
import {
3430
Diagram,
@@ -37,8 +33,7 @@ import {
3733
useDiagram,
3834
applyLayout,
3935
} from '@mongodb-js/diagramming';
40-
import type { Edit, StaticModel } from '../services/data-model-storage';
41-
import { UUID } from 'bson';
36+
import type { StaticModel } from '../services/data-model-storage';
4237
import DiagramEditorToolbar from './diagram-editor-toolbar';
4338
import ExportDiagramModal from './export-diagram-modal';
4439
import { useLogger } from '@mongodb-js/compass-logging/provider';
@@ -120,31 +115,6 @@ const modelPreviewStyles = css({
120115
minHeight: 0,
121116
});
122117

123-
const editorContainerStyles = css({
124-
display: 'flex',
125-
flexDirection: 'column',
126-
gap: 8,
127-
boxShadow: `0 0 0 2px ${palette.gray.light2}`,
128-
});
129-
130-
const editorContainerApplyContainerStyles = css({
131-
padding: spacing[200],
132-
justifyContent: 'flex-end',
133-
gap: spacing[200],
134-
display: 'flex',
135-
width: '100%',
136-
alignItems: 'center',
137-
});
138-
139-
const editorContainerPlaceholderButtonStyles = css({
140-
paddingLeft: 8,
141-
paddingRight: 8,
142-
alignSelf: 'flex-start',
143-
display: 'flex',
144-
gap: spacing[200],
145-
paddingTop: spacing[200],
146-
});
147-
148118
const DiagramEditor: React.FunctionComponent<{
149119
diagramLabel: string;
150120
step: DataModelingState['step'];
@@ -180,54 +150,6 @@ const DiagramEditor: React.FunctionComponent<{
180150
[diagram]
181151
);
182152

183-
const [applyInput, setApplyInput] = useState('{}');
184-
185-
const isEditValid = useMemo(() => {
186-
try {
187-
JSON.parse(applyInput);
188-
return true;
189-
} catch {
190-
return false;
191-
}
192-
}, [applyInput]);
193-
194-
const applyPlaceholder =
195-
(type: 'AddRelationship' | 'RemoveRelationship') => () => {
196-
let placeholder = {};
197-
switch (type) {
198-
case 'AddRelationship':
199-
placeholder = {
200-
type: 'AddRelationship',
201-
relationship: {
202-
id: new UUID().toString(),
203-
relationship: [
204-
{
205-
ns: 'db.sourceCollection',
206-
cardinality: 1,
207-
fields: ['field1'],
208-
},
209-
{
210-
ns: 'db.targetCollection',
211-
cardinality: 1,
212-
fields: ['field2'],
213-
},
214-
],
215-
isInferred: false,
216-
},
217-
};
218-
break;
219-
case 'RemoveRelationship':
220-
placeholder = {
221-
type: 'RemoveRelationship',
222-
relationshipId: new UUID().toString(),
223-
};
224-
break;
225-
default:
226-
throw new Error(`Unknown placeholder ${type}`);
227-
}
228-
setApplyInput(JSON.stringify(placeholder, null, 2));
229-
};
230-
231153
const edges = useMemo(() => {
232154
return (model?.relationships ?? []).map((relationship): EdgeProps => {
233155
const [source, target] = relationship.relationship;
@@ -357,29 +279,9 @@ const DiagramEditor: React.FunctionComponent<{
357279
maxZoom: 1,
358280
minZoom: 0.25,
359281
}}
360-
onSelectionChange={({ nodes }) => {
361-
console.log('SELECTION CHANGE', nodes);
362-
}}
363-
onNodeDrag={(evt, node) => {
364-
console.log('NODE DRAG', node);
365-
// onMoveCollection(node.id, [node.position.x, node.position.y]);
366-
}}
367282
onNodeDragStop={(evt, node) => {
368-
console.log('NODE DRAG STOP', node);
369283
onMoveCollection(node.id, [node.position.x, node.position.y]);
370284
}}
371-
onEdgeClick={(evt, edge) => {
372-
setApplyInput(
373-
JSON.stringify(
374-
{
375-
type: 'RemoveRelationship',
376-
relationshipId: edge.id,
377-
},
378-
null,
379-
2
380-
)
381-
);
382-
}}
383285
/>
384286
</div>
385287
</div>

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
applyEdit,
55
applyInitialLayout,
66
getCurrentDiagramFromState,
7+
getCurrentModel,
78
openDiagram,
89
redoEdit,
910
undoEdit,
@@ -201,6 +202,7 @@ describe('Data Modeling store', function () {
201202
],
202203
isInferred: false,
203204
};
205+
204206
store.dispatch(
205207
applyEdit({
206208
type: 'AddRelationship',
@@ -217,6 +219,9 @@ describe('Data Modeling store', function () {
217219
type: 'AddRelationship',
218220
relationship: newRelationship,
219221
});
222+
223+
const currentModel = getCurrentModel(diagram);
224+
expect(currentModel.relationships).to.have.length(2);
220225
});
221226

222227
it('should not apply invalid AddRelationship edit', function () {
@@ -239,6 +244,48 @@ describe('Data Modeling store', function () {
239244
const diagram = getCurrentDiagramFromState(store.getState());
240245
expect(diagram.edits).to.deep.equal(loadedDiagram.edits);
241246
});
247+
248+
it('should apply a valid MoveCollection edit', function () {
249+
store.dispatch(openDiagram(loadedDiagram));
250+
251+
const edit: Omit<
252+
Extract<Edit, { type: 'MoveCollection' }>,
253+
'id' | 'timestamp'
254+
> = {
255+
type: 'MoveCollection',
256+
ns: model.collections[0].ns,
257+
newPosition: [100, 100],
258+
};
259+
store.dispatch(applyEdit(edit));
260+
261+
const state = store.getState();
262+
const diagram = getCurrentDiagramFromState(state);
263+
expect(state.diagram?.editErrors).to.be.undefined;
264+
expect(diagram.edits).to.have.length(2);
265+
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
266+
expect(diagram.edits[1]).to.deep.include(edit);
267+
268+
const currentModel = getCurrentModel(diagram);
269+
expect(currentModel.collections[0].displayPosition).to.deep.equal([
270+
100, 100,
271+
]);
272+
});
273+
274+
it('should not apply invalid MoveCollection edit', function () {
275+
store.dispatch(openDiagram(loadedDiagram));
276+
277+
const edit = {
278+
type: 'MoveCollection',
279+
ns: 'nonexistent.collection',
280+
} as unknown as Edit;
281+
store.dispatch(applyEdit(edit));
282+
283+
const editErrors = store.getState().diagram?.editErrors;
284+
expect(editErrors).to.have.length(1);
285+
expect(editErrors && editErrors[0]).to.equal("'newPosition' is required");
286+
const diagram = getCurrentDiagramFromState(store.getState());
287+
expect(diagram.edits).to.deep.equal(loadedDiagram.edits);
288+
});
242289
});
243290

244291
it('undo & redo', function () {

0 commit comments

Comments
 (0)