Advertisement
Guest User

Untitled

a guest
Aug 28th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // publish-composite
  2. Meteor.publishComposite = function () {};
  3.  
  4. beforeEach(function () {
  5. Meteor.publishComposite = function () {};
  6. });
  7.  
  8. var originalMeteorCollection = Meteor.Collection;
  9.  
  10. Meteor.Collection = function () {
  11. var collectionHooks = {
  12. before: {
  13. insert: [],
  14. update: [],
  15. remove: []
  16. },
  17. after: {
  18. insert: [],
  19. update: [],
  20. remove: []
  21. }
  22. };
  23.  
  24. this.attachSchema = function(hook) {
  25. };
  26.  
  27. this.before = {
  28. insert: function (hook) {
  29. collectionHooks.before.insert.push(hook);
  30. },
  31. update: function (hook) {
  32. collectionHooks.before.update.push(hook);
  33. },
  34. remove: function (hook) {
  35. collectionHooks.before.remove.push(hook);
  36. }
  37. };
  38.  
  39. this.after = {
  40. insert: function (hook) {
  41. collectionHooks.after.insert.push(hook);
  42. },
  43. update: function (hook) {
  44. collectionHooks.after.update.push(hook);
  45. },
  46. remove: function (hook) {
  47. collectionHooks.after.remove.push(hook);
  48. }
  49. };
  50.  
  51. this.before.insert.run = generateHookRunner(this, collectionHooks.before.insert);
  52. this.before.update.run = generateHookRunner(this, collectionHooks.before.update);
  53. this.before.remove.run = generateHookRunner(this, collectionHooks.before.remove);
  54.  
  55. this.after.insert.run = generateHookRunner(this, collectionHooks.after.insert);
  56. this.after.update.run = generateHookRunner(this, collectionHooks.after.update);
  57. this.after.remove.run = generateHookRunner(this, collectionHooks.after.remove);
  58.  
  59. originalMeteorCollection.apply(this, arguments);
  60. };
  61.  
  62. mockMeteorCollection();
  63. beforeEach(function () {
  64. mockMeteorCollection();
  65. });
  66.  
  67. function mockMeteorCollection() {
  68. Meteor.Collection.prototype = originalMeteorCollection.prototype;
  69. // Remove this from the prototype (coming from meteor-stubs)
  70. delete Meteor.Collection.after;
  71. delete Meteor.Collection.before;
  72. delete Meteor.Collection.attachSchema;
  73. Mongo.Collection = Meteor.Collection;
  74. }
  75.  
  76. function getCollectionHookContext(collection) {
  77. return {
  78. transform: function (document) {
  79. collection._transform(document);
  80. }
  81. };
  82. }
  83.  
  84. function generateHookRunner(collection, hooks) {
  85. return function (userId, document) {
  86. hooks.forEach(function (hook) {
  87. hook.call(getCollectionHookContext(collection), userId, document);
  88. });
  89. };
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement