11import { cleanup } from '@testing-library/react' ;
2- // import { renderHook } from '@testing-library/react-hooks';
2+ import { renderHook } from '@testing-library/react-hooks' ;
33import { 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
513const fetch = global . fetch as FetchMock ;
614
715describe ( '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} ) ;
0 commit comments