Skip to content

Commit

Permalink
Pull in maxAge option tests from master
Browse files Browse the repository at this point in the history
Commits cd6320a
and da4e439 made
progress with regards to `maxAge` handling. This
commit takes the test changes that have been done
and backports them onto the current latest `1.x`.
  • Loading branch information
sigv committed Jul 17, 2019
1 parent cb271c8 commit 36d2861
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,52 @@ test('maxAge option', async t => {
t.is(memoized(1), 1);
});

test('maxAge option deletes old items', async t => {
let i = 0;
const f = () => i++;
const cache = new Map();
const deleted = [];
const remove = cache.delete.bind(cache);
cache.delete = item => {
deleted.push(item);
return remove(item);
};

const memoized = m(f, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.has(1), true);
await delay(50);
t.is(memoized(1), 0);
t.is(deleted.length, 0);
await delay(200);
t.is(memoized(1), 1);
t.is(deleted.length, 1);
t.is(deleted[0], 1);
});

test('maxAge items are deleted even if function throws', async t => {
let i = 0;
const f = () => {
if (i === 1) {
throw new Error('failure');
}

return i++;
};

const cache = new Map();
const memoized = m(f, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.size, 1);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.throws(() => memoized(1), 'failure');
t.is(cache.size, 0);
});

test('cacheKey option', t => {
let i = 0;
const f = () => i++;
Expand Down

0 comments on commit 36d2861

Please sign in to comment.