/
customHashTable.test.js
64 lines (50 loc) · 1.49 KB
/
customHashTable.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cacheEngine from '../main';
import { hashTableSymbol } from '../main';
describe('cache engine lru with custom hash table', () => {
// custom hashTable
const mockHTAdd = jest.fn();
const mockHTRemove = jest.fn();
const mockHTGet = jest.fn();
const mockHTHas = jest.fn();
const { get, add, remove, clear, size, has } = hashTableSymbol;
const hashTable = function() {
const store = {};
this[add] = (key, value) => {
mockHTAdd(key, value);
store[key] = value;
};
this[remove] = key => {
mockHTRemove(key);
delete store[key];
};
this[get] = key => {
mockHTGet(key);
return store[key];
};
this[clear] = () => {
store = {};
};
this[has] = key => {
mockHTHas(key);
return store.hasOwnProperty(key);
};
this[size] = () => {
return Object.keys(store).length;
};
};
it('should satisfy all features', () => {
const cache = cacheEngine({ HashTable: hashTable });
cache.add('key1', { msg: 'key1' });
cache.add('key2', { msg: 'key2' });
expect(mockHTAdd).toHaveBeenCalledTimes(2);
expect(cache.size()).toBe(2);
expect(cache.get('key2')).toEqual({ msg: 'key2' });
expect(cache.has('key1')).toBe(true);
expect(mockHTGet).toHaveBeenCalledTimes(1);
cache.remove('key1');
expect(mockHTRemove).toHaveBeenCalledTimes(1);
expect(cache.size()).toBe(1);
expect(cache.get('key1')).toBe(undefined);
expect(cache.has('key1')).toBe(false);
});
});