Skip to content

Commit

Permalink
Merge pull request #12994 from lpizzinidev/gh-12992
Browse files Browse the repository at this point in the history
fix(document): isModified should not be triggered when setting a nested boolean to the same value as previosly
  • Loading branch information
vkarpov15 committed Feb 3, 2023
2 parents e6f6308 + a69194f commit 7c6fbb2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1621,7 +1621,7 @@ Document.prototype.$__shouldModify = function(pathToMark, path, options, constru
return false;
}

if (!deepEqual(val, priorVal || utils.getValue(path, this))) {
if (!deepEqual(val, priorVal !== undefined ? priorVal : utils.getValue(path, this))) {
return true;
}

Expand Down
35 changes: 35 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12075,6 +12075,41 @@ describe('document', function() {
assert.equal(doc.list.length, 3);
assert.deepStrictEqual(doc.list.map(el => el.a), [1, 2, 3]);
});

it('should not trigger isModified when setting a nested boolean to the same value as previously (gh-12992)', async function() {
const Test = db.model('Test', new Schema({
result: new Schema(
{
score: Number,
passed: Boolean
},
{ _id: false }
)
}));
const newTest = await Test.create({
result: {
score: 40,
passed: false
}
});

const existingTest = await Test.findById(newTest._id);
existingTest.result = {
score: 40,
passed: false
};

assert.equal(existingTest.isModified(), false);
assert.equal(existingTest.modifiedPaths().length, 0);

existingTest.result = {
score: 40,
passed: true
};

assert.equal(existingTest.isModified(), true);
assert.equal(existingTest.modifiedPaths().length, 1);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 7c6fbb2

Please sign in to comment.