Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ const DiagramContent: React.FunctionComponent<{
isNewlyCreatedDiagram?: boolean;
model: StaticModel | null;
isInRelationshipDrawingMode: boolean;
editErrors?: string[];
newCollection?: string;
onAddFieldToObjectField: (ns: string, parentPath: string[]) => void;
onAddNewFieldToCollection: (ns: string) => void;
Expand Down Expand Up @@ -526,7 +525,6 @@ export default connect(
const { diagram, step } = state;
return {
step: step,
editErrors: diagram?.editErrors,
diagramId: diagram?.id,
};
},
Expand Down
23 changes: 23 additions & 0 deletions packages/compass-data-modeling/src/store/apply-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ function renameFieldInRelationshipSide(
};
}

/**
* @param collections
* @param ns
* @throws Will throw an error if the namespace is not found in the collections.
*/
function assertCollectionExists(
collections: DataModelCollection[],
ns: string
): void {
const collection = collections.find((c) => c.ns === ns);
if (!collection) {
throw new Error(`Collection '${ns}' not found`);
}
}

export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
if (edit.type === 'SetModel') {
return edit.model;
Expand Down Expand Up @@ -81,6 +96,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'MoveCollection': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
collections: model.collections.map((collection) => {
Expand All @@ -95,6 +111,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'RemoveCollection': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
// Remove any relationships involving the collection being removed.
Expand All @@ -109,6 +126,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'RenameCollection': {
assertCollectionExists(model.collections, edit.fromNS);
return {
...model,
// Update relationships to point to the renamed namespace.
Expand Down Expand Up @@ -137,6 +155,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'UpdateCollectionNote': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
collections: model.collections.map((collection) => {
Expand All @@ -151,6 +170,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'AddField': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
collections: model.collections.map((collection) => {
Expand All @@ -169,6 +189,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'RemoveField': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
// Remove any relationships involving the field being removed.
Expand All @@ -193,6 +214,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'RenameField': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
// Update any relationships involving the field being renamed.
Expand Down Expand Up @@ -227,6 +249,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
};
}
case 'ChangeFieldType': {
assertCollectionExists(model.collections, edit.ns);
return {
...model,
collections: model.collections.map((collection) => {
Expand Down
32 changes: 22 additions & 10 deletions packages/compass-data-modeling/src/store/diagram.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
StaticModel,
} from '../services/data-model-storage';
import { UUID } from 'bson';
import Sinon from 'sinon';

const model: StaticModel = {
collections: [
Expand Down Expand Up @@ -67,9 +68,11 @@ const loadedDiagram: MongoDBDataModelDescription = {

describe('Data Modeling store', function () {
let store: DataModelingStore;
let openToastSpy: Sinon.SinonSpy;

beforeEach(function () {
store = setupStore();
openToastSpy = Sinon.spy();
store = setupStore({}, undefined, openToastSpy);
});

describe('New Diagram', function () {
Expand Down Expand Up @@ -157,7 +160,7 @@ describe('Data Modeling store', function () {

const state = store.getState();
const diagram = getCurrentDiagramFromState(state);
expect(state.diagram?.editErrors).to.be.undefined;
expect(openToastSpy).not.to.have.been.called;
expect(diagram.edits).to.have.length(2);
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
expect(diagram.edits[1]).to.deep.include(edit);
Expand Down Expand Up @@ -193,7 +196,7 @@ describe('Data Modeling store', function () {

const state = store.getState();
const diagram = getCurrentDiagramFromState(state);
expect(state.diagram?.editErrors).to.be.undefined;
expect(openToastSpy).not.to.have.been.called;
expect(diagram.edits).to.have.length(2);
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
expect(diagram.edits[1]).to.deep.include({
Expand All @@ -217,9 +220,8 @@ describe('Data Modeling store', function () {
} as unknown as Edit;
store.dispatch(applyEdit(edit));

const editErrors = store.getState().diagram?.editErrors;
expect(editErrors).to.have.length(1);
expect(editErrors && editErrors[0]).to.equal(
expect(openToastSpy).to.have.been.calledOnce;
expect(openToastSpy.firstCall.args[1].description).to.include(
"'relationship,relationship' is required"
);
const diagram = getCurrentDiagramFromState(store.getState());
Expand Down Expand Up @@ -353,7 +355,7 @@ describe('Data Modeling store', function () {

const state = store.getState();
const diagram = getCurrentDiagramFromState(state);
expect(state.diagram?.editErrors).to.be.undefined;
expect(openToastSpy).not.to.have.been.called;
expect(diagram.edits).to.have.length(2);
expect(diagram.edits[0]).to.deep.equal(loadedDiagram.edits[0]);
expect(diagram.edits[1]).to.deep.include(edit);
Expand All @@ -373,12 +375,22 @@ describe('Data Modeling store', function () {
} as unknown as Edit;
store.dispatch(applyEdit(edit));

const editErrors = store.getState().diagram?.editErrors;
expect(editErrors).to.have.length(1);
expect(editErrors && editErrors[0]).to.equal("'newPosition' is required");
expect(openToastSpy).to.have.been.calledOnce;
expect(openToastSpy.firstCall.args[1].description).to.include(
"'newPosition' is required"
);
const diagram = getCurrentDiagramFromState(store.getState());
expect(diagram.edits).to.deep.equal(loadedDiagram.edits);
});

it('should handle an invalid RenameCollection edit', function () {
store.dispatch(openDiagram(loadedDiagram));
store.dispatch(renameCollection('nonExisting', 'newName'));
expect(openToastSpy).to.have.been.calledOnce;
expect(openToastSpy.firstCall.args[1].description).to.include(
"Collection 'nonExisting' not found"
);
});
});

it('undo & redo', function () {
Expand Down
Loading
Loading