Skip to content

Commit 261443e

Browse files
committed
add initial schema and store test
1 parent 3eb7310 commit 261443e

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
redoEdit,
99
undoEdit,
1010
selectFieldsForCurrentModel,
11+
startCreatingCollection,
12+
addCollection,
1113
} from './diagram';
1214
import type {
1315
Edit,
@@ -223,6 +225,58 @@ describe('Data Modeling store', function () {
223225
expect(diagram.edits).to.deep.equal(loadedDiagram.edits);
224226
});
225227

228+
it('should handle the collection creation flow', function () {
229+
store.dispatch(openDiagram(loadedDiagram));
230+
231+
// start creating a new collection
232+
store.dispatch(startCreatingCollection());
233+
234+
// the new collection is not yet in the edit history
235+
const diagramAtCreation = getCurrentDiagramFromState(store.getState());
236+
expect(diagramAtCreation.edits).to.deep.equal(loadedDiagram.edits);
237+
238+
// but the selection changes accordingly
239+
const selectedItems = store.getState().diagram?.selectedItems;
240+
expect(selectedItems).to.deep.equal({
241+
type: 'collection',
242+
id: undefined,
243+
});
244+
245+
// save the new collection
246+
const newCollectionNs = 'db.newCollection';
247+
store.dispatch(addCollection(newCollectionNs));
248+
249+
// now the collection is added to the edit history
250+
const diagramAfterCreation = getCurrentDiagramFromState(store.getState());
251+
expect(diagramAfterCreation.edits).to.have.length(2);
252+
expect(diagramAfterCreation.edits[0]).to.deep.equal(
253+
loadedDiagram.edits[0]
254+
);
255+
const addCollectionEdit = diagramAfterCreation.edits[1] as Extract<
256+
Edit,
257+
{ type: 'AddCollection' }
258+
>;
259+
expect(addCollectionEdit.type).to.equal('AddCollection');
260+
expect(addCollectionEdit.ns).to.equal(newCollectionNs);
261+
expect(addCollectionEdit.initialSchema).to.deep.equal({
262+
bsonType: 'object',
263+
properties: {
264+
_id: {
265+
bsonType: 'objectId',
266+
},
267+
},
268+
required: ['_id'],
269+
});
270+
271+
// and it is selected
272+
const selectedItemsAfterCreation =
273+
store.getState().diagram?.selectedItems;
274+
expect(selectedItemsAfterCreation).to.deep.equal({
275+
type: 'collection',
276+
id: newCollectionNs,
277+
});
278+
});
279+
226280
it('should apply a valid MoveCollection edit', function () {
227281
store.dispatch(openDiagram(loadedDiagram));
228282

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ export function addCollection(
593593
).collections;
594594
position = getPositionForNewCollection(existingCollections, {
595595
ns,
596-
jsonSchema: {} as MongoDBJSONSchema, // TODO: should we use a default schema?
596+
jsonSchema: {} as MongoDBJSONSchema,
597597
indexes: [],
598598
});
599599
}
@@ -604,7 +604,15 @@ export function addCollection(
604604
> = {
605605
type: 'AddCollection',
606606
ns,
607-
initialSchema: {} as MongoDBJSONSchema, // TODO: should we use a default schema?
607+
initialSchema: {
608+
bsonType: 'object',
609+
properties: {
610+
_id: {
611+
bsonType: 'objectId',
612+
},
613+
},
614+
required: ['_id'],
615+
},
608616
position,
609617
};
610618
dispatch(applyEdit(edit));

0 commit comments

Comments
 (0)