Skip to content

Commit 6732863

Browse files
author
nebarf
committed
http-cache tests
1 parent ed976d4 commit 6732863

17 files changed

+965
-768
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Contributing
3131
7. Link react peer deps to local library
3232
```sh
3333
$ cd ../react-http-fetch
34-
$ yarn link ../react-http-fetch-sandbox/node_modules/react
35-
$ yarn link ../react-http-fetch-sandbox/node_modules/react-dom
34+
$ npm link ../react-http-fetch-sandbox/node_modules/react
35+
$ npm link ../react-http-fetch-sandbox/node_modules/react-dom
3636
```
3737
8. Start library build in watch mode:
3838
```sh

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ export class HttpSessionStorageCacheStore {
580580
* @inheritdoc
581581
*/
582582
flush() {
583-
this._keys().forEach((entry) => {
584-
this.delete(entry.identifier);
583+
this._keys().forEach((itemKey) => {
584+
this.delete(itemKey);
585585
});
586586
}
587587
}

jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = {
1919

2020
// Test spec file resolution pattern
2121
// Matches parent folder `__tests__` and filename
22-
// should contain `test` or `spec`.
23-
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
22+
// should contain `spec`.
23+
testRegex: '/__tests__/.*.spec.(ts|tsx)?$',
2424

2525
// Module file extensions for importing
2626
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { HttpCacheService, HttpInMemoryCacheStore } from '../..';
2+
import { HttpCacheStorePrefixDecorator } from '../../cache/prefix-decorator';
3+
4+
export class HttpCacheFixture {
5+
static defaultStore: HttpCacheStorePrefixDecorator = new HttpCacheStorePrefixDecorator(
6+
new HttpInMemoryCacheStore()
7+
);
8+
9+
static create(store?: HttpCacheStorePrefixDecorator): HttpCacheService {
10+
const fallenbackStore =
11+
store || new HttpCacheStorePrefixDecorator(new HttpInMemoryCacheStore());
12+
return new HttpCacheService(fallenbackStore);
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { HttpMethod, HttpRequest, HttpRequestProps } from '../..';
2+
3+
export class HttpRequestFixture {
4+
static defaultOptions: HttpRequestProps = {
5+
baseUrl: 'http://rhf.com',
6+
method: HttpMethod.Get,
7+
relativeUrl: 'comments',
8+
};
9+
10+
static create(options?: HttpRequestProps): HttpRequest {
11+
const fallenbackOptions: HttpRequestProps = options || HttpRequestFixture.defaultOptions;
12+
13+
return new HttpRequest(fallenbackOptions);
14+
}
15+
}

src/__tests__/http-cache.spec.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 }>(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 }>(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 }>(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+
});

src/__tests__/use-http-client.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('', () => {
2+
test('', () => {
3+
expect(1).toBe(1);
4+
});
5+
});

src/__tests__/use-http-delete.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('', () => {
2+
test('', () => {
3+
expect(1).toBe(1);
4+
});
5+
});

src/__tests__/use-http-event.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('', () => {
2+
test('', () => {
3+
expect(1).toBe(1);
4+
});
5+
});

0 commit comments

Comments
 (0)