Skip to content

Commit

Permalink
Add test for nested private paths
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Dec 7, 2020
1 parent 6cc324c commit a243caa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/models/plugins/toJSON.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* - replaces _id with id
*/


const deleteAtPath = (obj, path, index) => {
if (index === path.length - 1) {
delete obj[path[index]];
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/models/plugins/toJSON.plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ describe('toJSON plugin', () => {
expect(doc.toJSON()).toHaveProperty('public');
});

it('should remove any nested paths set as private', () => {
const schema = mongoose.Schema({
public: { type: String },
nested: {
private: { type: String, private: true },
},
});
schema.plugin(toJSON);
const Model = connection.model('Model', schema);
const doc = new Model({
public: 'some public value',
nested: {
private: 'some nested private value',
},
});
expect(doc.toJSON()).not.toHaveProperty('nested.private');
expect(doc.toJSON()).toHaveProperty('public');
});

it('should also call the schema toJSON transform function', () => {
const schema = mongoose.Schema(
{
Expand Down

0 comments on commit a243caa

Please sign in to comment.