|
7 | 7 | getNextOccurrence, |
8 | 8 | toWalletAddressUrl, |
9 | 9 | setDifference, |
| 10 | + memoize, |
10 | 11 | } from '../helpers'; |
11 | 12 |
|
12 | 13 | describe('objectEquals', () => { |
@@ -165,3 +166,111 @@ describe('toWalletAddressUrl', () => { |
165 | 166 | ); |
166 | 167 | }); |
167 | 168 | }); |
| 169 | + |
| 170 | +describe('memoize', () => { |
| 171 | + jest.useFakeTimers(); |
| 172 | + |
| 173 | + type SuccessResponse = { data: string }; |
| 174 | + type MockFunction = () => Promise<SuccessResponse>; |
| 175 | + |
| 176 | + const successResponse1: SuccessResponse = { data: 'success1' }; |
| 177 | + const successResponse2: SuccessResponse = { data: 'success2' }; |
| 178 | + const errorResponse = new Error('failure'); |
| 179 | + |
| 180 | + let mockFn: jest.MockedFunction<MockFunction>; |
| 181 | + beforeEach(() => { |
| 182 | + mockFn = jest.fn(); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should cache the result of a successful promise with max-age mechanism', async () => { |
| 186 | + mockFn.mockResolvedValueOnce(successResponse1); |
| 187 | + mockFn.mockResolvedValueOnce(successResponse2); |
| 188 | + const memoizedFn = memoize(mockFn, { maxAge: 1000, mechanism: 'max-age' }); |
| 189 | + |
| 190 | + const result1 = await memoizedFn(); |
| 191 | + const result2 = await memoizedFn(); |
| 192 | + |
| 193 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 194 | + expect(result1).toBe(successResponse1); |
| 195 | + expect(result2).toBe(successResponse1); |
| 196 | + |
| 197 | + jest.advanceTimersByTime(1001); |
| 198 | + const result3 = await memoizedFn(); |
| 199 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 200 | + expect(result3).toBe(successResponse2); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should cache the result of a successful promise with stale-while-revalidate mechanism', async () => { |
| 204 | + mockFn.mockResolvedValueOnce(successResponse1); |
| 205 | + mockFn.mockResolvedValueOnce(successResponse2); |
| 206 | + const memoizedFn = memoize(mockFn, { |
| 207 | + maxAge: 1000, |
| 208 | + mechanism: 'stale-while-revalidate', |
| 209 | + }); |
| 210 | + |
| 211 | + const result1 = await memoizedFn(); |
| 212 | + const result2 = await memoizedFn(); |
| 213 | + |
| 214 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 215 | + expect(result1).toBe(successResponse1); |
| 216 | + expect(result2).toBe(successResponse1); |
| 217 | + |
| 218 | + jest.advanceTimersByTime(1001); |
| 219 | + const result3 = await memoizedFn(); |
| 220 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 221 | + expect(result3).toBe(successResponse1); |
| 222 | + |
| 223 | + jest.advanceTimersByTime(50); |
| 224 | + const result4 = await memoizedFn(); |
| 225 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 226 | + expect(result4).toBe(successResponse2); |
| 227 | + }); |
| 228 | + |
| 229 | + it('should reject if there is an error in first call with max-age mechanism', async () => { |
| 230 | + mockFn.mockRejectedValueOnce(errorResponse); |
| 231 | + mockFn.mockResolvedValueOnce(successResponse1); |
| 232 | + |
| 233 | + const memoizedFn = memoize(mockFn, { maxAge: 1000, mechanism: 'max-age' }); |
| 234 | + |
| 235 | + await expect(memoizedFn).rejects.toBe(errorResponse); |
| 236 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 237 | + |
| 238 | + const result = await memoizedFn(); |
| 239 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 240 | + expect(result).toBe(successResponse1); |
| 241 | + }); |
| 242 | + |
| 243 | + it('should not return error response from previous call when using state-while-revalidate mechanism', async () => { |
| 244 | + mockFn.mockRejectedValueOnce(errorResponse); |
| 245 | + mockFn.mockResolvedValueOnce(successResponse1); |
| 246 | + mockFn.mockRejectedValueOnce(errorResponse); |
| 247 | + mockFn.mockResolvedValueOnce(successResponse2); |
| 248 | + |
| 249 | + const memoizedFn = memoize(mockFn, { |
| 250 | + maxAge: 1000, |
| 251 | + mechanism: 'stale-while-revalidate', |
| 252 | + }); |
| 253 | + |
| 254 | + await expect(memoizedFn).rejects.toBe(errorResponse); |
| 255 | + expect(mockFn).toHaveBeenCalledTimes(1); |
| 256 | + |
| 257 | + const result1 = await memoizedFn(); |
| 258 | + expect(mockFn).toHaveBeenCalledTimes(2); |
| 259 | + expect(result1).toBe(successResponse1); |
| 260 | + |
| 261 | + jest.advanceTimersByTime(1001); |
| 262 | + |
| 263 | + // even though 3rd call results in an error, reuse successful response from |
| 264 | + // a previous call |
| 265 | + const result2 = await memoizedFn(); |
| 266 | + expect(mockFn).toHaveBeenCalledTimes(3); |
| 267 | + expect(mockFn.mock.results.at(-1)).toEqual( |
| 268 | + expect.objectContaining(errorResponse), |
| 269 | + ); |
| 270 | + expect(result2).toBe(successResponse1); |
| 271 | + |
| 272 | + const result3 = await memoizedFn(); |
| 273 | + expect(mockFn).toHaveBeenCalledTimes(4); |
| 274 | + expect(result3).toBe(successResponse2); |
| 275 | + }); |
| 276 | +}); |
0 commit comments