|
| 1 | +import { HttpCacheFixture } from './fixtures/http-cache.fixture'; |
| 2 | +import { HttpRequestFixture } from './fixtures/http-request.fixture'; |
| 3 | + |
| 4 | +describe('http-fetch', () => { |
| 5 | + afterEach(() => { |
| 6 | + jest.useRealTimers(); |
| 7 | + }); |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + jest.useFakeTimers(); |
| 11 | + jest.spyOn(global, 'setTimeout'); |
| 12 | + }); |
| 13 | + |
| 14 | + test('should return undefined if no cached entry is found for the given identifier', () => { |
| 15 | + const httpCache = HttpCacheFixture.create(); |
| 16 | + const httpRequest = HttpRequestFixture.create(); |
| 17 | + const cachedEntry = httpCache.get(httpRequest); |
| 18 | + |
| 19 | + expect(cachedEntry).toBeUndefined(); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should return the cached entry corresponding to the provided identifier', () => { |
| 23 | + const httpCache = HttpCacheFixture.create(); |
| 24 | + const httpRequest = HttpRequestFixture.create({ |
| 25 | + ...HttpRequestFixture.defaultOptions, |
| 26 | + maxAge: 6000, |
| 27 | + }); |
| 28 | + |
| 29 | + const cachedResponse = { name: 'Phelony' }; |
| 30 | + |
| 31 | + httpCache.put(httpRequest, cachedResponse); |
| 32 | + const expiredCachedResponse = httpCache.get<{ name: string }, void>(httpRequest); |
| 33 | + |
| 34 | + expect(expiredCachedResponse).toEqual(cachedResponse); |
| 35 | + expect(setTimeout).toHaveBeenCalledTimes(1); |
| 36 | + expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 6000); |
| 37 | + }); |
| 38 | + |
| 39 | + test('should remove the cached entry once expired', () => { |
| 40 | + const httpCache = HttpCacheFixture.create(); |
| 41 | + const httpRequest = HttpRequestFixture.create({ |
| 42 | + ...HttpRequestFixture.defaultOptions, |
| 43 | + maxAge: 6000, |
| 44 | + }); |
| 45 | + |
| 46 | + const httpResponse = { name: 'Phelony' }; |
| 47 | + |
| 48 | + httpCache.put(httpRequest, httpResponse); |
| 49 | + const cachedResponse = httpCache.get<{ name: string }, void>(httpRequest); |
| 50 | + |
| 51 | + expect(cachedResponse).toEqual(httpResponse); |
| 52 | + expect(setTimeout).toHaveBeenCalledTimes(1); |
| 53 | + expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 6000); |
| 54 | + |
| 55 | + jest.runOnlyPendingTimers(); |
| 56 | + const expiredCachedResponse = httpCache.get<{ name: string }, void>(httpRequest); |
| 57 | + expect(expiredCachedResponse).toBeUndefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + test('should tell if a cached response for a given request is expired', () => { |
| 61 | + const httpCache = HttpCacheFixture.create(); |
| 62 | + const httpRequest = HttpRequestFixture.create({ |
| 63 | + ...HttpRequestFixture.defaultOptions, |
| 64 | + maxAge: 6000, |
| 65 | + }); |
| 66 | + |
| 67 | + httpCache.put(httpRequest, {}); |
| 68 | + expect(httpCache.isExpired(httpRequest)).toBe(false); |
| 69 | + |
| 70 | + jest.runOnlyPendingTimers(); |
| 71 | + expect(httpCache.isExpired(httpRequest)).toBe(true); |
| 72 | + }); |
| 73 | + |
| 74 | + test('should tell if there is a cached response for the given request', () => { |
| 75 | + const httpCache = HttpCacheFixture.create(); |
| 76 | + const httpRequest = HttpRequestFixture.create({ |
| 77 | + ...HttpRequestFixture.defaultOptions, |
| 78 | + maxAge: 6000, |
| 79 | + }); |
| 80 | + |
| 81 | + httpCache.put(httpRequest, {}); |
| 82 | + expect(httpCache.has(httpRequest)).toBe(true); |
| 83 | + |
| 84 | + jest.runOnlyPendingTimers(); |
| 85 | + expect(httpCache.has(httpRequest)).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + test('should prune the cache by removing any expired entry', () => { |
| 89 | + const httpCache = HttpCacheFixture.create(); |
| 90 | + const httpRequest = HttpRequestFixture.create({ |
| 91 | + ...HttpRequestFixture.defaultOptions, |
| 92 | + maxAge: 6000, |
| 93 | + }); |
| 94 | + |
| 95 | + httpCache.put(httpRequest, {}); |
| 96 | + expect(httpCache.has(httpRequest)).toBe(true); |
| 97 | + |
| 98 | + jest.runOnlyPendingTimers(); |
| 99 | + expect(httpCache.has(httpRequest)).toBe(false); |
| 100 | + }); |
| 101 | + |
| 102 | + test('should flush the cache by removing all entries', () => { |
| 103 | + const httpCache = HttpCacheFixture.create(); |
| 104 | + const firstHttpRequest = HttpRequestFixture.create({ |
| 105 | + ...HttpRequestFixture.defaultOptions, |
| 106 | + maxAge: 6000, |
| 107 | + relativeUrl: 'posts', |
| 108 | + }); |
| 109 | + const secondHttpRequest = HttpRequestFixture.create({ |
| 110 | + ...HttpRequestFixture.defaultOptions, |
| 111 | + maxAge: 6000, |
| 112 | + relativeUrl: 'comments', |
| 113 | + }); |
| 114 | + |
| 115 | + httpCache.put(firstHttpRequest, {}); |
| 116 | + httpCache.put(secondHttpRequest, {}); |
| 117 | + |
| 118 | + expect(httpCache.has(firstHttpRequest)).toBe(true); |
| 119 | + expect(httpCache.has(secondHttpRequest)).toBe(true); |
| 120 | + |
| 121 | + httpCache.flush(); |
| 122 | + expect(httpCache.has(firstHttpRequest)).toBe(false); |
| 123 | + expect(httpCache.has(secondHttpRequest)).toBe(false); |
| 124 | + }); |
| 125 | +}); |
0 commit comments