Skip to content

Commit

Permalink
Added test cases for #5706
Browse files Browse the repository at this point in the history
  • Loading branch information
wlingke committed Oct 26, 2017
1 parent 174ad6c commit 8d09a20
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -842,5 +842,126 @@ describe('model', function() {
}).
catch(done);
});
describe('embedded discriminators + hooks (gh-5706)', function(){
var counters = {
eventPreSave: 0,
eventPostSave: 0,
purchasePreSave: 0,
purchasePostSave: 0,
eventPreValidate: 0,
eventPostValidate: 0,
purchasePreValidate: 0,
purchasePostValidate: 0,
};
var eventSchema = new Schema(
{ message: String },
{ discriminatorKey: 'kind', _id: false }
);
eventSchema.pre('validate', (next) => {
counters.eventPreValidate++;
next();
});

eventSchema.post('validate', (doc) => {
counters.eventPostValidate++;
});

eventSchema.pre('save', (next) => {
counters.eventPreSave++;
next();
});

eventSchema.post('save', (doc) => {
counters.eventPostSave++;
});

var purchasedSchema = new Schema({
product: String,
}, { _id: false });

purchasedSchema.pre('validate', (next) => {
counters.purchasePreValidate++;
next();
});

purchasedSchema.post('validate', (doc) => {
counters.purchasePostValidate++;
});

purchasedSchema.pre('save', (next) => {
counters.purchasePreSave++;
next();
});

purchasedSchema.post('save', (doc) => {
counters.purchasePostSave++;
});

beforeEach(function() {
Object.keys(counters).forEach(function(i){
counters[i]=0;
})
});

it('should call the hooks on the embedded document defined by both the parent and discriminated schemas', function(done){
var trackSchema = new Schema({
event: eventSchema,
});

var embeddedEventSchema = trackSchema.path('event');
embeddedEventSchema.discriminator('Purchased', purchasedSchema)

var TrackModel = db.model('Track', trackSchema);
var doc = new TrackModel({
event: {
message: 'Test',
kind: 'Purchased'
}
});
doc.save(function(err){
assert.ok(!err);
assert.equal(doc.event.message, 'Test')
assert.equal(doc.event.kind, 'Purchased')
Object.keys(counters).forEach(function(i){
assert.equal(counters[i], 1);
});
done();
})
})

it('should call the hooks on the embedded document in an embedded array defined by both the parent and discriminated schemas', function(done){
var trackSchema = new Schema({
events: [eventSchema],
});

var embeddedEventSchema = trackSchema.path('events');
embeddedEventSchema.discriminator('Purchased', purchasedSchema)

var TrackModel = db.model('Track2', trackSchema);
var doc = new TrackModel({
events: [
{
message: 'Test',
kind: 'Purchased'
},
{
message: 'TestAgain',
kind: 'Purchased'
}
]
});
doc.save(function(err){
assert.ok(!err);
assert.equal(doc.events[0].kind, 'Purchased');
assert.equal(doc.events[0].message, 'Test');
assert.equal(doc.events[1].kind, 'Purchased');
assert.equal(doc.events[1].message, 'TestAgain');
Object.keys(counters).forEach(function(i){
assert.equal(counters[i], 2);
});
done();
})
})
})
});
});

0 comments on commit 8d09a20

Please sign in to comment.