Skip to content

Instantly share code, notes, and snippets.

@abernix
Created August 28, 2015 14:34
Show Gist options
  • Save abernix/43df56eb537c4488fac0 to your computer and use it in GitHub Desktop.
Save abernix/43df56eb537c4488fac0 to your computer and use it in GitHub Desktop.
A mock for the Meteor smart package collection-hooks. Add this file in the folder tests/jasmine/server/unit/
// publish-composite
Meteor.publishComposite = function () {};
beforeEach(function () {
Meteor.publishComposite = function () {};
});
var originalMeteorCollection = Meteor.Collection;
Meteor.Collection = function () {
var collectionHooks = {
before: {
insert: [],
update: [],
remove: []
},
after: {
insert: [],
update: [],
remove: []
}
};
this.attachSchema = function(hook) {
};
this.before = {
insert: function (hook) {
collectionHooks.before.insert.push(hook);
},
update: function (hook) {
collectionHooks.before.update.push(hook);
},
remove: function (hook) {
collectionHooks.before.remove.push(hook);
}
};
this.after = {
insert: function (hook) {
collectionHooks.after.insert.push(hook);
},
update: function (hook) {
collectionHooks.after.update.push(hook);
},
remove: function (hook) {
collectionHooks.after.remove.push(hook);
}
};
this.before.insert.run = generateHookRunner(this, collectionHooks.before.insert);
this.before.update.run = generateHookRunner(this, collectionHooks.before.update);
this.before.remove.run = generateHookRunner(this, collectionHooks.before.remove);
this.after.insert.run = generateHookRunner(this, collectionHooks.after.insert);
this.after.update.run = generateHookRunner(this, collectionHooks.after.update);
this.after.remove.run = generateHookRunner(this, collectionHooks.after.remove);
originalMeteorCollection.apply(this, arguments);
};
mockMeteorCollection();
beforeEach(function () {
mockMeteorCollection();
});
function mockMeteorCollection() {
Meteor.Collection.prototype = originalMeteorCollection.prototype;
// Remove this from the prototype (coming from meteor-stubs)
delete Meteor.Collection.after;
delete Meteor.Collection.before;
delete Meteor.Collection.attachSchema;
Mongo.Collection = Meteor.Collection;
}
function getCollectionHookContext(collection) {
return {
transform: function (document) {
collection._transform(document);
}
};
}
function generateHookRunner(collection, hooks) {
return function (userId, document) {
hooks.forEach(function (hook) {
hook.call(getCollectionHookContext(collection), userId, document);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment