Skip to content

Commit 8a33896

Browse files
committed
feature/cache: Add basic lru-cache tests
1 parent 1d8afd9 commit 8a33896

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Lru .constructor() constructs the cache properly 1`] = `
4+
LRU {
5+
"cache": LRUCache {
6+
Symbol(max): 10,
7+
Symbol(lengthCalculator): [Function],
8+
Symbol(allowStale): false,
9+
Symbol(maxAge): 0,
10+
Symbol(dispose): undefined,
11+
Symbol(noDisposeOnSet): false,
12+
Symbol(cache): Map {},
13+
Symbol(lruList): Yallist {
14+
"head": null,
15+
"length": 0,
16+
"tail": null,
17+
},
18+
Symbol(length): 0,
19+
},
20+
}
21+
`;

src/lru-cache.spec.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const Lru = require('./lru-cache');
2+
3+
describe('Lru', () => {
4+
describe('.constructor()', () => {
5+
it('constructs the cache properly', () => {
6+
const actual = new Lru({ maxItems: 10 });
7+
expect(actual).toMatchSnapshot();
8+
});
9+
});
10+
11+
describe('.get(key)', () => {
12+
let instance;
13+
const maxItems = 10;
14+
15+
beforeEach(() => {
16+
instance = new Lru({ maxItems });
17+
});
18+
19+
it('returns a promise', () => {
20+
const actual = instance.get('key');
21+
expect(actual).toBeInstanceOf(Promise);
22+
});
23+
24+
it('returns undefined when the key is not set', () => {
25+
expect.assertions(1);
26+
return instance.get('key').then(actual => expect(actual).toBeUndefined());
27+
});
28+
29+
it('returns the correct item when the key is set', () => {
30+
const expected = 'hello world';
31+
expect.assertions(1);
32+
return instance
33+
.set('key', expected)
34+
.then(() => instance.get('key'))
35+
.then(actual => expect(actual).toEqual(expected));
36+
});
37+
38+
it('returns undefined when the key is ejected', () => {
39+
const expected = 'hello world';
40+
const range = Array.from(new Array(maxItems), (x, i) => i);
41+
42+
expect.assertions(1);
43+
44+
return instance
45+
.set('key', expected)
46+
.then(() =>
47+
Promise.all(range.map(i => instance.set(`key${i}`, 'hello universe')))
48+
)
49+
.then(() => instance.get('key'))
50+
.then(actual => expect(actual).toEqual(undefined));
51+
});
52+
});
53+
54+
describe('.set(key, value, options)', () => {
55+
let instance;
56+
const maxItems = 10;
57+
58+
beforeEach(() => {
59+
instance = new Lru({ maxItems });
60+
});
61+
62+
it('increments the item count', () => {
63+
expect.assertions(1);
64+
return instance
65+
.set('key', 'value')
66+
.then(() => expect(instance.length()).toEqual(1));
67+
});
68+
69+
it('adheres to maxItems when more items are inserted', () => {
70+
const range = Array.from(new Array(maxItems + 1), (x, i) => i);
71+
72+
expect.assertions(1);
73+
74+
return Promise.all(
75+
range.map(i => instance.set(`key${i}`, 'hello universe'))
76+
).then(() => expect(instance.length()).toEqual(maxItems));
77+
});
78+
79+
it('only stores the last items in order of last insertion', () => {
80+
const range = Array.from(new Array(maxItems), (x, i) => i);
81+
82+
expect.assertions(1);
83+
84+
return instance
85+
.set('key', 'value')
86+
.then(() =>
87+
Promise.all(
88+
range.map(i => instance.set(`key${i}`, `hello universe ${i}`))
89+
)
90+
)
91+
.then(() => instance.values())
92+
.then(values =>
93+
expect(values).toEqual(
94+
range.map(i => `hello universe ${i}`).reverse()
95+
)
96+
);
97+
});
98+
});
99+
});

0 commit comments

Comments
 (0)