lru-memoizer.events.test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const memoizer = require('./..');
  2. const sinon = require('sinon');
  3. describe('lru-memoizer (events)', function () {
  4. let memoized;
  5. let onMiss, onHit, onQueue;
  6. beforeEach(function () {
  7. loadTimes = 0;
  8. onMiss = sinon.stub();
  9. onHit = sinon.stub();
  10. onQueue = sinon.stub();
  11. memoized = memoizer({
  12. load: function (a, b, bypass, callback) {
  13. return setTimeout(function () {
  14. if (a === 0) {
  15. return callback(new Error('a cant be 0'));
  16. }
  17. callback(null, a+b);
  18. }, 10);
  19. },
  20. hash: function (a, b) {
  21. return a + '-' + b;
  22. },
  23. bypass: function(a, b, bypass) {
  24. return bypass;
  25. },
  26. max: 10
  27. });
  28. memoized.on('hit', onHit);
  29. memoized.on('miss', onMiss);
  30. memoized.on('queue', onQueue);
  31. });
  32. describe('when the result is not in the cache', () => {
  33. beforeEach((done) => {
  34. memoized(1, 2, false, done);
  35. });
  36. it('should not call onHit', () => {
  37. sinon.assert.notCalled(onHit);
  38. });
  39. it('should not call onQueue', () => {
  40. sinon.assert.notCalled(onQueue);
  41. });
  42. it('should call onMiss with the load arguments', () => {
  43. sinon.assert.calledOnce(onMiss);
  44. sinon.assert.calledWith(onMiss, 1, 2, false);
  45. });
  46. });
  47. describe('when the result is in the cache', () => {
  48. beforeEach((done) => {
  49. memoized(1,2, false, () => {
  50. onHit.reset();
  51. onMiss.reset();
  52. onQueue.reset();
  53. memoized(1, 2, false, done);
  54. });
  55. });
  56. it('should call onHit with the load arguments', () => {
  57. sinon.assert.calledOnce(onHit);
  58. sinon.assert.calledWith(onHit, 1, 2, false);
  59. });
  60. it('should not call onQueue', () => {
  61. sinon.assert.notCalled(onQueue);
  62. });
  63. it('should not call onMiss', () => {
  64. sinon.assert.notCalled(onQueue);
  65. });
  66. });
  67. describe('when the cache is by passed', () => {
  68. beforeEach((done) => {
  69. memoized(1,2, false, () => {
  70. onHit.reset();
  71. onMiss.reset();
  72. onQueue.reset();
  73. memoized(1, 2, true, done);
  74. });
  75. });
  76. it('should not call onHit', () => {
  77. sinon.assert.notCalled(onHit);
  78. });
  79. it('should not call onQueue', () => {
  80. sinon.assert.notCalled(onQueue);
  81. });
  82. it('should call onMiss with the load arguments', () => {
  83. sinon.assert.calledOnce(onMiss);
  84. sinon.assert.calledWith(onMiss, 1, 2, true);
  85. });
  86. });
  87. describe('when the result is pending', () => {
  88. beforeEach((done) => {
  89. let pending = 2;
  90. function onDone() {
  91. pending -= 1;
  92. if (pending === 0) {
  93. done();
  94. }
  95. }
  96. memoized(1, 2, false, onDone);
  97. onHit.reset();
  98. onMiss.reset();
  99. onQueue.reset();
  100. memoized(1, 2, false, onDone);
  101. });
  102. it('should not call onHit', () => {
  103. sinon.assert.notCalled(onHit);
  104. });
  105. it('should call onQueue with the load arguments', () => {
  106. sinon.assert.calledOnce(onQueue);
  107. sinon.assert.calledWith(onQueue, 1, 2, false);
  108. });
  109. it('should not call onMiss', () => {
  110. sinon.assert.notCalled(onMiss);
  111. });
  112. });
  113. });