-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathErc20RepositoryCache.spec.ts
More file actions
129 lines (99 loc) · 3.69 KB
/
Erc20RepositoryCache.spec.ts
File metadata and controls
129 lines (99 loc) · 3.69 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Erc20RepositoryCache } from './Erc20RepositoryCache';
import { Erc20, Erc20Repository } from './Erc20Repository';
import { CacheRepository } from '../CacheRepository/CacheRepository';
import { SupportedChainId } from '@cowprotocol/cow-sdk';
describe('Erc20RepositoryCache', () => {
let erc20RepositoryCache: Erc20RepositoryCache;
let mockProxy: jest.Mocked<Erc20Repository>;
let mockCache: jest.Mocked<CacheRepository>;
const chainId: SupportedChainId = 1;
const tokenAddress = '0xTokenAddress';
const cacheName = 'erc20';
const cacheTimeSeconds = 60;
const cacheKey = `repos:${cacheName}:get:${chainId}:${tokenAddress.toLocaleLowerCase()}`;
const erc20Data: Erc20 = {
address: '0x1111111111111111111111111111111111111111',
name: 'Token Name',
symbol: 'TKN',
decimals: 18,
};
beforeEach(() => {
mockProxy = {
get: jest.fn(),
} as unknown as jest.Mocked<Erc20Repository>;
mockCache = {
get: jest.fn(),
set: jest.fn(),
} as unknown as jest.Mocked<CacheRepository>;
erc20RepositoryCache = new Erc20RepositoryCache(
mockProxy,
mockCache,
cacheName,
cacheTimeSeconds
);
});
it('should return cached value if available', async () => {
// GIVEN: Cached value is available
mockCache.get.mockResolvedValue(JSON.stringify(erc20Data));
// WHEN: get is called
const result = await erc20RepositoryCache.get(chainId, tokenAddress);
// THEN: The cache is called
expect(mockCache.get).toHaveBeenCalledWith(cacheKey);
// THEN: The proxy is not called
expect(mockProxy.get).not.toHaveBeenCalled();
// THEN: The cached value is returned
expect(result).toEqual(erc20Data);
});
it('should return null if cached value is NULL_VALUE', async () => {
// GIVEN: Cached value is null
mockCache.get.mockResolvedValue('null');
// WHEN: get is called
const result = await erc20RepositoryCache.get(chainId, tokenAddress);
// THEN: The cache is called
expect(mockCache.get).toHaveBeenCalledWith(cacheKey);
// THEN: The proxy is not called
expect(mockProxy.get).not.toHaveBeenCalled();
// THEN: null is returned
expect(result).toBeNull();
});
it('should fetch from proxy and cache the result if not cached', async () => {
// GIVEN: Cached value is not available
mockCache.get.mockResolvedValue(null);
// GIVEN: Proxy returns the value
mockProxy.get.mockResolvedValue(erc20Data);
// WHEN: get is called
const result = await erc20RepositoryCache.get(chainId, tokenAddress);
// THEN: The cache is called
expect(mockCache.get).toHaveBeenCalledWith(cacheKey);
// THEN: The proxy is called
expect(mockProxy.get).toHaveBeenCalledWith(chainId, tokenAddress);
// THEN: The value is cached
expect(mockCache.set).toHaveBeenCalledWith(
cacheKey,
JSON.stringify(erc20Data),
cacheTimeSeconds
);
// THEN: The value is returned
expect(result).toEqual(erc20Data);
});
it('should cache NULL_VALUE if proxy returns null', async () => {
// GIVEN: Cached value is not available
mockCache.get.mockResolvedValue(null);
// GIVEN: Proxy returns null
mockProxy.get.mockResolvedValue(null);
// WHEN: get is called
const result = await erc20RepositoryCache.get(chainId, tokenAddress);
// THEN: The cache is called
expect(mockCache.get).toHaveBeenCalledWith(cacheKey);
// THEN: The proxy is called
expect(mockProxy.get).toHaveBeenCalledWith(chainId, tokenAddress);
// THEN: The null value is cached
expect(mockCache.set).toHaveBeenCalledWith(
cacheKey,
'null',
cacheTimeSeconds
);
// THEN: The result is null
expect(result).toBeNull();
});
});