Skip to content

Commit a96f9df

Browse files
committed
more cleanup and tests
1 parent 51223f7 commit a96f9df

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
@@ -8,7 +8,6 @@ import React, {
88
import { connect } from 'react-redux';
99
import type { DataModelingState } from '../store/reducer';
1010
import {
11-
applyEdit,
1211
applyInitialLayout,
1312
moveCollection,
1413
getCurrentDiagramFromState,
@@ -21,11 +20,8 @@ import {
2120
css,
2221
spacing,
2322
Button,
24-
palette,
25-
ErrorSummary,
2623
useDarkMode,
2724
} from '@mongodb-js/compass-components';
28-
import { CodemirrorMultilineEditor } from '@mongodb-js/compass-editor';
2925
import { cancelAnalysis, retryAnalysis } from '../store/analysis-process';
3026
import {
3127
Diagram,
@@ -34,8 +30,7 @@ import {
3430
useDiagram,
3531
applyLayout,
3632
} from '@mongodb-js/diagramming';
37-
import type { Edit, StaticModel } from '../services/data-model-storage';
38-
import { UUID } from 'bson';
33+
import type { StaticModel } from '../services/data-model-storage';
3934
import DiagramEditorToolbar from './diagram-editor-toolbar';
4035
import ExportDiagramModal from './export-diagram-modal';
4136
import { useLogger } from '@mongodb-js/compass-logging/provider';
@@ -90,31 +85,6 @@ const modelPreviewStyles = css({
9085
minHeight: 0,
9186
});
9287

93-
const editorContainerStyles = css({
94-
display: 'flex',
95-
flexDirection: 'column',
96-
gap: 8,
97-
boxShadow: `0 0 0 2px ${palette.gray.light2}`,
98-
});
99-
100-
const editorContainerApplyContainerStyles = css({
101-
padding: spacing[200],
102-
justifyContent: 'flex-end',
103-
gap: spacing[200],
104-
display: 'flex',
105-
width: '100%',
106-
alignItems: 'center',
107-
});
108-
109-
const editorContainerPlaceholderButtonStyles = css({
110-
paddingLeft: 8,
111-
paddingRight: 8,
112-
alignSelf: 'flex-start',
113-
display: 'flex',
114-
gap: spacing[200],
115-
paddingTop: spacing[200],
116-
});
117-
11888
const DiagramEditor: React.FunctionComponent<{
11989
diagramLabel: string;
12090
step: DataModelingState['step'];
@@ -150,54 +120,6 @@ const DiagramEditor: React.FunctionComponent<{
150120
[diagram]
151121
);
152122

153-
const [applyInput, setApplyInput] = useState('{}');
154-
155-
const isEditValid = useMemo(() => {
156-
try {
157-
JSON.parse(applyInput);
158-
return true;
159-
} catch {
160-
return false;
161-
}
162-
}, [applyInput]);
163-
164-
const applyPlaceholder =
165-
(type: 'AddRelationship' | 'RemoveRelationship') => () => {
166-
let placeholder = {};
167-
switch (type) {
168-
case 'AddRelationship':
169-
placeholder = {
170-
type: 'AddRelationship',
171-
relationship: {
172-
id: new UUID().toString(),
173-
relationship: [
174-
{
175-
ns: 'db.sourceCollection',
176-
cardinality: 1,
177-
fields: ['field1'],
178-
},
179-
{
180-
ns: 'db.targetCollection',
181-
cardinality: 1,
182-
fields: ['field2'],
183-
},
184-
],
185-
isInferred: false,
186-
},
187-
};
188-
break;
189-
case 'RemoveRelationship':
190-
placeholder = {
191-
type: 'RemoveRelationship',
192-
relationshipId: new UUID().toString(),
193-
};
194-
break;
195-
default:
196-
throw new Error(`Unknown placeholder ${type}`);
197-
}
198-
setApplyInput(JSON.stringify(placeholder, null, 2));
199-
};
200-
201123
const edges = useMemo(() => {
202124
return (model?.relationships ?? []).map((relationship): EdgeProps => {
203125
const [source, target] = relationship.relationship;
@@ -333,29 +255,9 @@ const DiagramEditor: React.FunctionComponent<{
333255
maxZoom: 1,
334256
minZoom: 0.25,
335257
}}
336-
onSelectionChange={({ nodes }) => {
337-
console.log('SELECTION CHANGE', nodes);
338-
}}
339-
onNodeDrag={(evt, node) => {
340-
console.log('NODE DRAG', node);
341-
// onMoveCollection(node.id, [node.position.x, node.position.y]);
342-
}}
343258
onNodeDragStop={(evt, node) => {
344-
console.log('NODE DRAG STOP', node);
345259
onMoveCollection(node.id, [node.position.x, node.position.y]);
346260
}}
347-
onEdgeClick={(evt, edge) => {
348-
setApplyInput(
349-
JSON.stringify(
350-
{
351-
type: 'RemoveRelationship',
352-
relationshipId: edge.id,
353-
},
354-
null,
355-
2
356-
)
357-
);
358-
}}
359261
/>
360262
</div>
361263
</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)