lru-memoizer.sync.test.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var memoizer = require('./..');
  2. var assert = require('chai').assert;
  3. describe('lru-memoizer sync', function () {
  4. var loadTimes = 0, memoized;
  5. beforeEach(function () {
  6. loadTimes = 0;
  7. memoized = memoizer.sync({
  8. load: function (a, b) {
  9. loadTimes++;
  10. if (a === 0) {
  11. throw new Error('a cant be 0');
  12. }
  13. return a + b;
  14. },
  15. hash: function (a, b) {
  16. return a + '-' + b;
  17. },
  18. max: 10
  19. });
  20. });
  21. it('should cache the result of an async function', function () {
  22. var result = memoized(1, 2);
  23. assert.equal(result, 3);
  24. assert.equal(loadTimes, 1);
  25. var result2 = memoized(1,2);
  26. assert.equal(result2, 3);
  27. assert.equal(loadTimes, 1);
  28. });
  29. it('shuld use the hash function for keys', function () {
  30. memoized(1, 2);
  31. memoized(2, 3);
  32. assert.includeMembers(memoized.keys(), ['1-2', '2-3']);
  33. });
  34. it('should not cache errored funcs', function () {
  35. try {
  36. memoized(0, 2);
  37. } catch(err) {}
  38. assert.notInclude(memoized.keys(), ['0-2']);
  39. });
  40. });