Skip to content

Commit 18e045c

Browse files
author
nebarf
committed
Finalize http-client-config-provider specs
1 parent d6cd714 commit 18e045c

File tree

5 files changed

+193
-11
lines changed

5 files changed

+193
-11
lines changed

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ module.exports = {
2121

2222
// Module file extensions for importing
2323
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
24+
25+
testEnvironment: 'jsdom',
2426
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"husky": "^7.0.0",
3636
"jest": "^27.2.4",
3737
"jest-fetch-mock": "^3.0.3",
38+
"jest-mock-extended": "^2.0.4",
3839
"lint-staged": "^11.2.0",
3940
"prettier": "^2.4.1",
4041
"react": "^17.0.2",
Lines changed: 177 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { cleanup } from '@testing-library/react';
2-
// import { renderHook } from '@testing-library/react-hooks';
2+
import { renderHook } from '@testing-library/react-hooks';
33
import { FetchMock } from 'jest-fetch-mock';
4+
import { mock } from 'jest-mock-extended';
5+
import { defaultHttpReqConfig, HttpCacheStore, HttpMethod, useHttpClient } from '..';
6+
import { HttpInMemoryCacheStore } from '../cache/http-in-memory-cache-store';
7+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
8+
9+
jest.mock('../cache/http-in-memory-cache-store');
10+
const MockedHttpInMemoryCacheStore = <jest.Mock<HttpInMemoryCacheStore>>HttpInMemoryCacheStore;
11+
const [inMemoryCacheMockInstance] = MockedHttpInMemoryCacheStore.mock.instances;
412

513
const fetch = global.fetch as FetchMock;
614

715
describe('http-client-config-provider', () => {
8-
// const fetchResponse = {
9-
// name: 'Phelony',
10-
// role: 'Admin',
11-
// };
16+
const fetchResponse = {
17+
name: 'Phelony',
18+
role: 'Admin',
19+
};
1220

1321
beforeEach(() => {
1422
jest.useFakeTimers();
1523
jest.spyOn(global, 'setTimeout');
24+
(inMemoryCacheMockInstance.get as jest.Mock).mockClear();
25+
(inMemoryCacheMockInstance.put as jest.Mock).mockClear();
1626
});
1727

1828
afterEach((): void => {
@@ -21,13 +31,170 @@ describe('http-client-config-provider', () => {
2131
jest.runAllTimers();
2232
});
2333

24-
// test('should override global req options', async () => {});
34+
test('should override global req options', async () => {
35+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
36+
37+
const customReqOpts = {
38+
headers: {
39+
'Content-Type': 'text/html; charset=UTF-8',
40+
},
41+
};
42+
43+
const { result } = renderHook(() => useHttpClient(), {
44+
wrapper: HttpClientProviderConfigFixture.create({
45+
reqOptions: customReqOpts,
46+
}),
47+
});
48+
49+
const { get } = result.current;
50+
51+
const res1 = await get({
52+
relativeUrl: 'posts/1',
53+
});
54+
55+
expect(res1).toEqual(fetchResponse);
56+
57+
const [[fetchUrl, fetchParams]] = fetch.mock.calls;
58+
const {
59+
method: fetchMethod,
60+
headers: fetchHeaders,
61+
credentials: fetchCredentials,
62+
body: fetchBody,
63+
signal: fetchSignal,
64+
} = fetchParams || {};
65+
66+
expect(fetchUrl).toBe('/posts/1');
67+
expect(fetchMethod).toBe(HttpMethod.Get);
68+
expect(fetchHeaders).toEqual(customReqOpts.headers);
69+
expect(fetchCredentials).toBeUndefined();
70+
expect(fetchBody).toBeNull();
71+
expect(fetchSignal).toBeUndefined();
72+
});
73+
74+
test('should override global base url', async () => {
75+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
76+
const customBaseUrl = 'http://phelony.com';
77+
78+
const { result } = renderHook(() => useHttpClient(), {
79+
wrapper: HttpClientProviderConfigFixture.create({
80+
baseUrl: 'http://phelony.com',
81+
}),
82+
});
83+
84+
const { get } = result.current;
85+
86+
const res1 = await get({
87+
relativeUrl: 'posts/1',
88+
});
89+
90+
expect(res1).toEqual(fetchResponse);
91+
92+
const [[fetchUrl, fetchParams]] = fetch.mock.calls;
93+
const {
94+
method: fetchMethod,
95+
headers: fetchHeaders,
96+
credentials: fetchCredentials,
97+
body: fetchBody,
98+
signal: fetchSignal,
99+
} = fetchParams || {};
100+
101+
expect(fetchUrl).toBe(`${customBaseUrl}/posts/1`);
102+
expect(fetchMethod).toBe(HttpMethod.Get);
103+
expect(fetchHeaders).toEqual(defaultHttpReqConfig.reqOptions.headers);
104+
expect(fetchCredentials).toBeUndefined();
105+
expect(fetchBody).toBeNull();
106+
expect(fetchSignal).toBeUndefined();
107+
});
108+
109+
test('should override cache store', async () => {
110+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
111+
const customBaseUrl = 'http://phelony.com';
112+
113+
const httpCacheStoreMock = mock<HttpCacheStore>();
114+
115+
const { result } = renderHook(() => useHttpClient(), {
116+
wrapper: HttpClientProviderConfigFixture.create({
117+
baseUrl: 'http://phelony.com',
118+
cacheStore: httpCacheStoreMock,
119+
}),
120+
});
121+
122+
const { get } = result.current;
123+
124+
const res1 = await get({
125+
relativeUrl: 'posts/1',
126+
requestOptions: {
127+
queryParams: { orderBy: 'title' },
128+
maxAge: 1000,
129+
},
130+
});
25131

26-
// test('should override global base url', async () => {});
132+
expect(res1).toEqual(fetchResponse);
27133

28-
// test('should override cache store', async () => {});
134+
const [[fetchUrl, fetchParams]] = fetch.mock.calls;
135+
const {
136+
method: fetchMethod,
137+
headers: fetchHeaders,
138+
credentials: fetchCredentials,
139+
body: fetchBody,
140+
signal: fetchSignal,
141+
} = fetchParams || {};
142+
143+
expect(fetchUrl).toBe(`${customBaseUrl}/posts/1?orderBy=title`);
144+
expect(fetchMethod).toBe(HttpMethod.Get);
145+
expect(fetchHeaders).toEqual(defaultHttpReqConfig.reqOptions.headers);
146+
expect(fetchCredentials).toBeUndefined();
147+
expect(fetchBody).toBeNull();
148+
expect(fetchSignal).toBeUndefined();
149+
150+
expect(httpCacheStoreMock.get).toHaveBeenCalledTimes(1);
151+
expect(httpCacheStoreMock.get).toHaveBeenCalledWith(
152+
'rhf__http://phelony.com/posts/1?orderBy=title'
153+
);
154+
155+
// expect(httpCacheStoreMock.put).toHaveBeenCalledTimes(1);
156+
});
29157

30-
// test('should override cache store prefix', async () => {});
158+
test('should override cache store prefix and separator', async () => {
159+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
160+
const customBaseUrl = 'http://phelony.com';
31161

32-
// test('should override cache store separator', async () => {});
162+
const { result } = renderHook(() => useHttpClient(), {
163+
wrapper: HttpClientProviderConfigFixture.create({
164+
baseUrl: 'http://phelony.com',
165+
cacheStorePrefix: 'phy',
166+
cacheStoreSeparator: '--',
167+
}),
168+
});
169+
170+
const { get } = result.current;
171+
172+
const res1 = await get({
173+
relativeUrl: 'posts/1',
174+
requestOptions: { maxAge: 1000 },
175+
});
176+
177+
expect(res1).toEqual(fetchResponse);
178+
179+
const [[fetchUrl, fetchParams]] = fetch.mock.calls;
180+
const {
181+
method: fetchMethod,
182+
headers: fetchHeaders,
183+
credentials: fetchCredentials,
184+
body: fetchBody,
185+
signal: fetchSignal,
186+
} = fetchParams || {};
187+
188+
expect(fetchUrl).toBe(`${customBaseUrl}/posts/1`);
189+
expect(fetchMethod).toBe(HttpMethod.Get);
190+
expect(fetchHeaders).toEqual(defaultHttpReqConfig.reqOptions.headers);
191+
expect(fetchCredentials).toBeUndefined();
192+
expect(fetchBody).toBeNull();
193+
expect(fetchSignal).toBeUndefined();
194+
195+
expect(inMemoryCacheMockInstance.get).toHaveBeenCalledTimes(1);
196+
expect(inMemoryCacheMockInstance.get).toHaveBeenCalledWith('phy--http://phelony.com/posts/1');
197+
198+
expect(inMemoryCacheMockInstance.put).toHaveBeenCalledTimes(1);
199+
});
33200
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ describe('use-http-client', () => {
494494

495495
const { get } = result.current;
496496

497-
const res1 = await get<string, unknown>({
497+
const res1 = await get({
498498
relativeUrl: 'posts/1',
499499
});
500500

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3286,6 +3286,13 @@ jest-message-util@^27.3.1:
32863286
slash "^3.0.0"
32873287
stack-utils "^2.0.3"
32883288

3289+
jest-mock-extended@^2.0.4:
3290+
version "2.0.4"
3291+
resolved "https://registry.yarnpkg.com/jest-mock-extended/-/jest-mock-extended-2.0.4.tgz#2bb430ba0adb9e10ea6a68d08731f2129330c8fe"
3292+
integrity sha512-MgL3B3GjURQFjjPGqbCANydA5BFNPygv0mYp4Tjfxohh9MWwxxX8Eq2p6ncCt/Vt+RAnaLlDaI7gwrDRD7Pt9A==
3293+
dependencies:
3294+
ts-essentials "^7.0.3"
3295+
32893296
jest-mock@^27.3.0:
32903297
version "27.3.0"
32913298
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-27.3.0.tgz#ddf0ec3cc3e68c8ccd489bef4d1f525571a1b867"
@@ -5040,6 +5047,11 @@ tr46@~0.0.3:
50405047
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
50415048
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
50425049

5050+
ts-essentials@^7.0.3:
5051+
version "7.0.3"
5052+
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-7.0.3.tgz#686fd155a02133eedcc5362dc8b5056cde3e5a38"
5053+
integrity sha512-8+gr5+lqO3G84KdiTSMRLtuyJ+nTBVRKuCrK4lidMPdVeEp0uqC875uE5NMcaA7YYMN7XsNiFQuMvasF8HT/xQ==
5054+
50435055
ts-jest@^27.0.5:
50445056
version "27.0.7"
50455057
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-27.0.7.tgz#fb7c8c8cb5526ab371bc1b23d06e745652cca2d0"

0 commit comments

Comments
 (0)