Skip to content

Commit d4ec9f4

Browse files
authored
COMPASS-840 remove _id on clone regardless of position (#813) (#814)
1 parent 9a65e63 commit d4ec9f4

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

src/internal-packages/crud/lib/store/open-insert-document-dialog-store.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const ObjectId = require('bson').ObjectId;
44
const Actions = require('../actions');
55
const HadronDocument = require('hadron-document');
66

7+
// const debug = require('debug')('mongodb-compass:crud:store:open-insert-doc');
8+
79
/**
810
* The reflux store for opening the insert document dialog.
911
*/
@@ -28,11 +30,13 @@ const OpenInsertDocumentDialogStore = Reflux.createStore({
2830
openInsertDocumentDialog: function(doc, clone) {
2931
const hadronDoc = new HadronDocument(doc, true);
3032
if (clone) {
31-
const firstElement = hadronDoc.elements.firstElement;
3233
// We need to remove the _id or we will get an duplicate key error on
3334
// insert, and we currently do not allow editing of the _id field.
34-
if (firstElement.currentKey === '_id') {
35-
hadronDoc.elements.remove(firstElement);
35+
for (const element of hadronDoc.elements) {
36+
if (element.currentKey === '_id') {
37+
hadronDoc.elements.remove(element);
38+
break;
39+
}
3640
}
3741
}
3842
this.trigger(hadronDoc);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* eslint no-unused-expressions: 0 */
2+
3+
const expect = require('chai').expect;
4+
5+
// const debug = require('debug')('mongodb-compass:test:query-changed-store');
6+
7+
let OpenInsertDocumentDialogStore = require('../../src/internal-packages/crud/lib/store/open-insert-document-dialog-store');
8+
9+
describe('OpenInsertDocumentDialogStore', () => {
10+
let unsubscribe = () => {};
11+
12+
afterEach(() => {
13+
unsubscribe();
14+
unsubscribe = () => {};
15+
});
16+
17+
context('when inserting a new document', () => {
18+
it('keeps the document as is without modifications', (done) => {
19+
const doc = {
20+
_id: 'foo',
21+
field: 'bar'
22+
};
23+
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
24+
expect(hadronDoc.generateObject()).to.be.deep.equal(doc);
25+
done();
26+
});
27+
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, false);
28+
});
29+
});
30+
31+
context('when cloning a document', () => {
32+
it('removes the _id element when it is at the first position', (done) => {
33+
const doc = {
34+
_id: 'foo',
35+
field: 'bar'
36+
};
37+
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
38+
expect(hadronDoc.generateObject()).to.be.deep.equal({field: 'bar'});
39+
done();
40+
});
41+
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, true);
42+
});
43+
it('removes the _id element when it is not at the first position', (done) => {
44+
const doc = {
45+
_a_surprise_: 'indeed',
46+
_id: 'foo',
47+
field: 'bar'
48+
};
49+
unsubscribe = OpenInsertDocumentDialogStore.listen((hadronDoc) => {
50+
expect(hadronDoc.generateObject()).to.be.deep.equal({
51+
_a_surprise_: 'indeed',
52+
field: 'bar'
53+
});
54+
done();
55+
});
56+
OpenInsertDocumentDialogStore.openInsertDocumentDialog(doc, true);
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)