Skip to content

Commit

Permalink
refactor example tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Aug 17, 2023
1 parent 5b74346 commit 08af253
Showing 1 changed file with 24 additions and 36 deletions.
60 changes: 24 additions & 36 deletions test/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const mongoose = require('mongoose');
const mongooseLeanVirtuals = require('../');

describe('examples', function() {
it('attaches virtuals to result of find, findOne, findById, findByIdAndUpdate, and findOneAndUpdate if lean', function() {
it('attaches virtuals to result of find, findOne, findById, findByIdAndUpdate, and findOneAndUpdate if lean', async function() {
const schema = new mongoose.Schema({
name: String
});
Expand All @@ -17,34 +17,26 @@ describe('examples', function() {
schema.plugin(mongooseLeanVirtuals);

const Model = mongoose.model('Test', schema);

return Model.create({ name: 'Val' }).
then(() => Promise.all([
// You **must** pass `virtuals: true` to `lean()`
Model.find().lean({ virtuals: true }),
Model.findOne().lean({ virtuals: true }),
Model.findOneAndUpdate({}, { name: 'VAL' }).lean({ virtuals: true })
])).
then(results => {
const findRes = results[0];
const findOneRes = results[1];
const findOneAndUpdateRes = results[2];
assert.equal(findRes[0].lowercase, 'val');
assert.equal(findOneRes.lowercase, 'val');
assert.equal(findOneAndUpdateRes.lowercase, 'val');

// Mongoose has an `id` virtual by default that gets the `_id` as a
// string.
assert.equal(findRes[0].id, findRes[0]._id.toString());
assert.equal(findOneRes.id, findOneRes._id.toString());
assert.equal(findOneAndUpdateRes.id, findOneAndUpdateRes._id.toString());
});
await Model.create({ name: 'Val' });

const findRes = await Model.find().lean({ virtuals: true });
const findOneRes = await Model.findOne().lean({ virtuals: true });
const findOneAndUpdateRes = await Model.findOneAndUpdate({}, { name: 'VAL'}).lean({ virtuals: true });
assert.equal(findRes[0].lowercase, 'val');
assert.equal(findOneRes.lowercase, 'val');
assert.equal(findOneAndUpdateRes.lowercase, 'val');

// Mongoose has an `id` virtual by default that gets the `_id` as a
// string.
assert.equal(findRes[0].id, findRes[0]._id.toString());
assert.equal(findOneRes.id, findOneRes._id.toString());
assert.equal(findOneAndUpdateRes.id, findOneAndUpdateRes._id.toString());
});

/**
* If you specify a list of virtuals in `lean()`, this plugin will only
* apply those virtuals. This lets you pick which virtuals show up. */
it('lets you choose which virtuals to apply', function() {
it('lets you choose which virtuals to apply', async function() {
const schema = new mongoose.Schema({
name: String
}, { id: false });
Expand All @@ -60,20 +52,18 @@ describe('examples', function() {

const Model = mongoose.model('Test2', schema);

return Model.create({ name: 'Val' }).
then(() => Model.findOne().lean({ virtuals: ['uppercase'] })).
then(result => {
assert.equal(result.uppercase, 'VAL');
assert.ok(!result.lowercase);
});
await Model.create({ name: 'Val' });
const result = await Model.findOne().lean({ virtuals: ['uppercase'] });
assert.equal(result.uppercase, 'VAL');
assert.ok(!result.lowercase);
});

/**
* Accessing the parent document is tricky because lean documents don't
* have a `parent()` method or any other Mongoose-specific functionality.
* To support that use case, this plugin exports a `parent()` function that
* lets you get a document's parent. */
it('lets you access a lean subdocument\'s parent', function() {
it('lets you access a lean subdocument\'s parent', async function() {
const childSchema = new mongoose.Schema({ firstName: String });
childSchema.virtual('fullName').get(function() {
if (this instanceof mongoose.Document) {
Expand All @@ -99,10 +89,8 @@ describe('examples', function() {
lastName: 'Skywalker',
child: { firstName: 'Luke' }
};
return Parent.create(doc).
then(() => Parent.findOne().lean({ virtuals: true })).
then(result => {
assert.equal(result.child.fullName, 'Luke Skywalker');
});
await Parent.create(doc);
const result = await Parent.findOne().lean({ virtuals: true });
assert.equal(result.child.fullName, 'Luke Skywalker');
});
});

0 comments on commit 08af253

Please sign in to comment.