-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcache.spec.js
More file actions
102 lines (87 loc) · 3.42 KB
/
Copy pathcache.spec.js
File metadata and controls
102 lines (87 loc) · 3.42 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const mockExists = jest.fn();
const mockGet = jest.fn();
const mockPut = jest.fn();
const mockRemove = jest.fn();
console.warn = jest.fn();
describe('cache.js', () => {
describe('JSCache', () => {
jest.mock('@runtime', () => ({}), { virtual: true });
jest.mock('@runtime/cache', () => ({}), { virtual: true });
const { JSCache } = require('../src/cache');
const cache = new JSCache({
exists: (...args) => mockExists(...args),
get: (...args) => mockGet(...args),
put: (...args) => mockPut(...args),
remove: (...args) => mockRemove(...args)
});
const key = 'key';
const value = 'value';
describe('exist', () => {
it('returns true if key exists in cache instance.', () => {
mockGet.mockImplementation(() => 'value');
expect(cache.exists(key)).toBe(true);
expect(console.warn).not.toHaveBeenCalled();
});
it('returns false, if key does not exist in cache instance.', () => {
mockGet.mockImplementation(() => null);
expect(cache.exists(key)).toBe(false);
expect(console.warn).not.toHaveBeenCalled();
});
});
describe('get', () => {
it('delegates to cache instance.', () => {
cache.get(key);
expect(mockGet).toHaveBeenCalledWith(key);
expect(console.warn).not.toHaveBeenCalled();
});
it('returns value from cache instance.', () => {
const returnValue = 'value';
mockGet.mockImplementation(() => returnValue);
expect(cache.get(key)).toBe(returnValue);
expect(console.warn).not.toHaveBeenCalled();
});
it('returns null, if key does not exist in cache instance and no supplier is provided.', () => {
mockGet.mockImplementation(() => null);
expect(cache.get(key)).toBe(null);
});
it('returns value from supplier, if key does not exist in cache instance and supplier is provided.', () => {
const supplier = jest.fn(() => 'value');
mockGet.mockImplementation(() => null);
expect(cache.get(key, supplier)).toBe('value');
expect(supplier).toHaveBeenCalledTimes(1);
});
it('puts value from supplier into cache instance, if key does not exist in cache instance and supplier is provided.', () => {
const supplier = () => 'value';
mockGet.mockImplementation(() => null);
cache.get(key, supplier);
expect(mockPut).toHaveBeenCalledWith(key, 'value');
});
});
describe('put', () => {
it('delegates to cache instance.', () => {
cache.put(key, value);
expect(mockPut).toHaveBeenCalledWith(key, value);
expect(console.warn).not.toHaveBeenCalled();
});
it('returns value from cache instance.', () => {
const returnValue = 'value';
mockPut.mockImplementation(() => returnValue);
expect(cache.put(key, 'value')).toBe(returnValue);
expect(console.warn).not.toHaveBeenCalled();
});
});
describe('remove', () => {
it('delegates to cache instance.', () => {
cache.remove(key);
expect(mockRemove).toHaveBeenCalledWith(key);
expect(console.warn).not.toHaveBeenCalled();
});
it('returns value from cache instance.', () => {
const returnValue = 'value';
mockRemove.mockImplementation(() => returnValue);
expect(cache.remove(key)).toBe(returnValue);
expect(console.warn).not.toHaveBeenCalled();
});
});
});
});