Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(document): isModified should not be triggered when setting a nested boolean to the same value as previosly #12994

Merged
merged 2 commits into from
Feb 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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