misc.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const assert = require('assert');
  3. const Kareem = require('../');
  4. const { describe, it } = require('mocha');
  5. describe('hasHooks', function() {
  6. it('returns false for toString (Automattic/mongoose#6538)', function() {
  7. const k = new Kareem();
  8. assert.ok(!k.hasHooks('toString'));
  9. });
  10. });
  11. describe('merge', function() {
  12. it('handles async pres if source doesnt have them', function() {
  13. const k1 = new Kareem();
  14. k1.pre('cook', true, function(next, done) {
  15. setTimeout(
  16. function() {
  17. done('error!');
  18. },
  19. 5);
  20. next();
  21. });
  22. assert.equal(k1._pres.get('cook').numAsync, 1);
  23. const k2 = new Kareem();
  24. const k3 = k2.merge(k1);
  25. assert.equal(k3._pres.get('cook').numAsync, 1);
  26. });
  27. });
  28. describe('filter', function() {
  29. it('returns clone with only hooks that match `fn()`', function() {
  30. const k1 = new Kareem();
  31. k1.pre('update', { document: true }, f1);
  32. k1.pre('update', { query: true }, f2);
  33. k1.pre('remove', { document: true }, f3);
  34. k1.post('update', { document: true }, f1);
  35. k1.post('update', { query: true }, f2);
  36. k1.post('remove', { document: true }, f3);
  37. const k2 = k1.filter(hook => hook.document);
  38. assert.equal(k2._pres.get('update').length, 1);
  39. assert.equal(k2._pres.get('update')[0].fn, f1);
  40. assert.equal(k2._pres.get('remove').length, 1);
  41. assert.equal(k2._pres.get('remove')[0].fn, f3);
  42. assert.equal(k2._posts.get('update').length, 1);
  43. assert.equal(k2._posts.get('update')[0].fn, f1);
  44. assert.equal(k2._posts.get('remove').length, 1);
  45. assert.equal(k2._posts.get('remove')[0].fn, f3);
  46. const k3 = k1.filter(hook => hook.query);
  47. assert.equal(k3._pres.get('update').length, 1);
  48. assert.equal(k3._pres.get('update')[0].fn, f2);
  49. assert.ok(!k3._pres.has('remove'));
  50. assert.equal(k3._posts.get('update').length, 1);
  51. assert.equal(k3._posts.get('update')[0].fn, f2);
  52. assert.ok(!k3._posts.has('remove'));
  53. function f1() {}
  54. function f2() {}
  55. function f3() {}
  56. });
  57. });