lru-memoizer.sync.events.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const memoizer = require('./..');
  2. const sinon = require('sinon');
  3. describe('lru-memoizer sync (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.sync({
  12. load: function (a, b, bypass) {
  13. return a + b;
  14. },
  15. hash: function (a, b, bypass) {
  16. return a + '-' + b;
  17. },
  18. bypass: function(a, b, bypass) {
  19. return bypass;
  20. },
  21. max: 10
  22. });
  23. memoized.on('hit', onHit);
  24. memoized.on('miss', onMiss);
  25. memoized.on('queue', onQueue);
  26. });
  27. describe('when the result is not in the cache', () => {
  28. beforeEach(() => {
  29. memoized(1, 2, false);
  30. });
  31. it('should not call onHit', () => {
  32. sinon.assert.notCalled(onHit);
  33. });
  34. it('should not call onQueue', () => {
  35. sinon.assert.notCalled(onQueue);
  36. });
  37. it('should call onMiss with the load arguments', () => {
  38. sinon.assert.calledOnce(onMiss);
  39. sinon.assert.calledWith(onMiss, 1, 2, false);
  40. });
  41. });
  42. describe('when the result is in the cache', () => {
  43. beforeEach(() => {
  44. memoized(1,2, false);
  45. onHit.reset();
  46. onMiss.reset();
  47. onQueue.reset();
  48. memoized(1, 2, false);
  49. });
  50. it('should call onHit with the load arguments', () => {
  51. sinon.assert.calledOnce(onHit);
  52. sinon.assert.calledWith(onHit, 1, 2, false);
  53. });
  54. it('should not call onQueue', () => {
  55. sinon.assert.notCalled(onQueue);
  56. });
  57. it('should not call onMiss', () => {
  58. sinon.assert.notCalled(onQueue);
  59. });
  60. });
  61. describe('when the cache is by passed', () => {
  62. beforeEach(() => {
  63. memoized(1,2, false);
  64. onHit.reset();
  65. onMiss.reset();
  66. onQueue.reset();
  67. memoized(1, 2, true);
  68. });
  69. it('should not call onHit', () => {
  70. sinon.assert.notCalled(onHit);
  71. });
  72. it('should not call onQueue', () => {
  73. sinon.assert.notCalled(onQueue);
  74. });
  75. it('should call onMiss with the load arguments', () => {
  76. sinon.assert.calledOnce(onMiss);
  77. sinon.assert.calledWith(onMiss, 1, 2, true);
  78. });
  79. });
  80. });