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 @@ -4,6 +4,8 @@ const ObjectId = require('bson').ObjectId;
const Actions = require('../actions');
const HadronDocument = require('hadron-document');

// const debug = require('debug')('mongodb-compass:crud:store:open-insert-doc');

/**
* The reflux store for opening the insert document dialog.
*/
Expand All @@ -28,11 +30,13 @@ const OpenInsertDocumentDialogStore = Reflux.createStore({
openInsertDocumentDialog: function(doc, clone) {
const hadronDoc = new HadronDocument(doc, true);
if (clone) {
const firstElement = hadronDoc.elements.firstElement;
// We need to remove the _id or we will get an duplicate key error on
// insert, and we currently do not allow editing of the _id field.
if (firstElement.currentKey === '_id') {
hadronDoc.elements.remove(firstElement);
for (const element of hadronDoc.elements) {
if (element.currentKey === '_id') {
hadronDoc.elements.remove(element);
break;
}
}
}
this.trigger(hadronDoc);
Expand Down
59 changes: 59 additions & 0 deletions test/renderer/crud.open-insert-document-dialog-store.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint no-unused-expressions: 0 */

const expect = require('chai').expect;

// const debug = require('debug')('mongodb-compass:test:query-changed-store');

let OpenInsertDocumentDialogStore = require('../../src/internal-packages/crud/lib/store/open-insert-document-dialog-store');

describe('OpenInsertDocumentDialogStore', () => {
let unsubscribe = () => {};

afterEach(() => {
unsubscribe();
unsubscribe = () => {};
});

context('when inserting a new document', () => {
it('keeps the document as is without modifications', (done) => {
const doc = {
_id: 'foo',
field: 'bar'
};
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
expect(hadronDoc.generateObject()).to.be.deep.equal(doc);
done();
});
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, false);
});
});

context('when cloning a document', () => {
it('removes the _id element when it is at the first position', (done) => {
const doc = {
_id: 'foo',
field: 'bar'
};
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
expect(hadronDoc.generateObject()).to.be.deep.equal({field: 'bar'});
done();
});
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, true);
});
it('removes the _id element when it is not at the first position', (done) => {
const doc = {
_a_surprise_: 'indeed',
_id: 'foo',
field: 'bar'
};
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
expect(hadronDoc.generateObject()).to.be.deep.equal({
_a_surprise_: 'indeed',
field: 'bar'
});
done();
});
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, true);
});
});
});