Skip to content

Commit 8d09a20

Browse files
committed
Added test cases for #5706
1 parent 174ad6c commit 8d09a20

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

test/model.discriminator.test.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,5 +842,126 @@ describe('model', function() {
842842
}).
843843
catch(done);
844844
});
845+
describe('embedded discriminators + hooks (gh-5706)', function(){
846+
var counters = {
847+
eventPreSave: 0,
848+
eventPostSave: 0,
849+
purchasePreSave: 0,
850+
purchasePostSave: 0,
851+
eventPreValidate: 0,
852+
eventPostValidate: 0,
853+
purchasePreValidate: 0,
854+
purchasePostValidate: 0,
855+
};
856+
var eventSchema = new Schema(
857+
{ message: String },
858+
{ discriminatorKey: 'kind', _id: false }
859+
);
860+
eventSchema.pre('validate', (next) => {
861+
counters.eventPreValidate++;
862+
next();
863+
});
864+
865+
eventSchema.post('validate', (doc) => {
866+
counters.eventPostValidate++;
867+
});
868+
869+
eventSchema.pre('save', (next) => {
870+
counters.eventPreSave++;
871+
next();
872+
});
873+
874+
eventSchema.post('save', (doc) => {
875+
counters.eventPostSave++;
876+
});
877+
878+
var purchasedSchema = new Schema({
879+
product: String,
880+
}, { _id: false });
881+
882+
purchasedSchema.pre('validate', (next) => {
883+
counters.purchasePreValidate++;
884+
next();
885+
});
886+
887+
purchasedSchema.post('validate', (doc) => {
888+
counters.purchasePostValidate++;
889+
});
890+
891+
purchasedSchema.pre('save', (next) => {
892+
counters.purchasePreSave++;
893+
next();
894+
});
895+
896+
purchasedSchema.post('save', (doc) => {
897+
counters.purchasePostSave++;
898+
});
899+
900+
beforeEach(function() {
901+
Object.keys(counters).forEach(function(i){
902+
counters[i]=0;
903+
})
904+
});
905+
906+
it('should call the hooks on the embedded document defined by both the parent and discriminated schemas', function(done){
907+
var trackSchema = new Schema({
908+
event: eventSchema,
909+
});
910+
911+
var embeddedEventSchema = trackSchema.path('event');
912+
embeddedEventSchema.discriminator('Purchased', purchasedSchema)
913+
914+
var TrackModel = db.model('Track', trackSchema);
915+
var doc = new TrackModel({
916+
event: {
917+
message: 'Test',
918+
kind: 'Purchased'
919+
}
920+
});
921+
doc.save(function(err){
922+
assert.ok(!err);
923+
assert.equal(doc.event.message, 'Test')
924+
assert.equal(doc.event.kind, 'Purchased')
925+
Object.keys(counters).forEach(function(i){
926+
assert.equal(counters[i], 1);
927+
});
928+
done();
929+
})
930+
})
931+
932+
it('should call the hooks on the embedded document in an embedded array defined by both the parent and discriminated schemas', function(done){
933+
var trackSchema = new Schema({
934+
events: [eventSchema],
935+
});
936+
937+
var embeddedEventSchema = trackSchema.path('events');
938+
embeddedEventSchema.discriminator('Purchased', purchasedSchema)
939+
940+
var TrackModel = db.model('Track2', trackSchema);
941+
var doc = new TrackModel({
942+
events: [
943+
{
944+
message: 'Test',
945+
kind: 'Purchased'
946+
},
947+
{
948+
message: 'TestAgain',
949+
kind: 'Purchased'
950+
}
951+
]
952+
});
953+
doc.save(function(err){
954+
assert.ok(!err);
955+
assert.equal(doc.events[0].kind, 'Purchased');
956+
assert.equal(doc.events[0].message, 'Test');
957+
assert.equal(doc.events[1].kind, 'Purchased');
958+
assert.equal(doc.events[1].message, 'TestAgain');
959+
Object.keys(counters).forEach(function(i){
960+
assert.equal(counters[i], 2);
961+
});
962+
done();
963+
})
964+
})
965+
})
845966
});
846967
});

0 commit comments

Comments
 (0)