HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-10-0-8-47 6.8.0-1021-aws #23~22.04.1-Ubuntu SMP Tue Dec 10 16:31:58 UTC 2024 aarch64
User: ubuntu (1000)
PHP: 8.1.2-1ubuntu2.22
Disabled: NONE
Upload Files
File: /var/www/javago-api-updates/node_modules/lru-memoizer/test/lru-memoizer.sync.test.js
var memoizer = require('./..');
var assert = require('chai').assert;

describe('lru-memoizer sync', function () {
  var loadTimes = 0, memoized;

  beforeEach(function () {
    loadTimes = 0;

    memoized = memoizer.sync({
      load: function (a, b) {
        loadTimes++;
        if (a === 0) {
          throw new Error('a cant be 0');
        }
        return a + b;
      },
      hash: function (a, b) {
        return a + '-' + b;
      },
      max: 10
    });
  });

  it('should cache the result of an async function', function () {
    var result = memoized(1, 2);
    assert.equal(result, 3);
    assert.equal(loadTimes, 1);

    var result2 = memoized(1,2);
    assert.equal(result2, 3);
    assert.equal(loadTimes, 1);
  });

  it('shuld use the hash function for keys', function () {
    memoized(1, 2);
    memoized(2, 3);
    assert.includeMembers(memoized.keys(), ['1-2', '2-3']);
  });

  it('should not cache errored funcs', function () {
    try {
      memoized(0, 2);
    } catch(err) {}
    assert.notInclude(memoized.keys(), ['0-2']);
  });
});