Skip to content

Commit 5a429d5

Browse files
author
nebarf
committed
use-http-event specs
1 parent f53ab78 commit 5a429d5

File tree

1 file changed

+204
-0
lines changed

1 file changed

+204
-0
lines changed

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

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
import { cleanup } from '@testing-library/react';
2+
import { renderHook, act } from '@testing-library/react-hooks';
3+
import { FetchMock } from 'jest-fetch-mock';
4+
import {
5+
defaultHttpReqConfig,
6+
HttpContext,
7+
HttpContextToken,
8+
HttpError,
9+
HttpMethod,
10+
HttpRequest,
11+
RequestErroredEvent,
12+
RequestStartedEvent,
13+
RequestSuccededEvent,
14+
RequestSuccededEventPayload,
15+
useHttpEvent,
16+
useHttpRequest,
17+
} from '..';
18+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
19+
20+
const fetch = global.fetch as FetchMock;
21+
22+
interface FetchResponse {
23+
name: string;
24+
role: string;
25+
}
26+
27+
describe('use-http-event', () => {
28+
const fetchResponse = {
29+
name: 'Phelony',
30+
role: 'Admin',
31+
};
32+
33+
beforeEach(() => {
34+
jest.useFakeTimers();
35+
jest.spyOn(global, 'setTimeout');
36+
});
37+
38+
afterEach((): void => {
39+
cleanup();
40+
fetch.resetMocks();
41+
jest.runAllTimers();
42+
});
43+
44+
it('should run the registered handler when a request starts', (done) => {
45+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
46+
47+
const showGlobalLoader = new HttpContextToken(true);
48+
const reqContext = new HttpContext().set(showGlobalLoader, false);
49+
50+
renderHook(
51+
() =>
52+
useHttpEvent(RequestStartedEvent, (request: HttpRequest<void>) => {
53+
expect(request.url).toBe('https://phelony.com/comments');
54+
expect(request.baseUrl).toBe('https://phelony.com');
55+
expect(request.relativeUrl).toBe('comments');
56+
expect(request.body).toBeUndefined();
57+
expect(request.headers).toEqual(defaultHttpReqConfig.reqOptions.headers);
58+
expect(request.context).toBe(reqContext);
59+
expect(request.getContextValue(showGlobalLoader)).toBe(false);
60+
expect(request.credentials).toBeUndefined();
61+
expect(request.maxAge).toBe(0);
62+
expect(request.method).toBe(HttpMethod.Get);
63+
expect(request.queryParams).toBeUndefined();
64+
expect(request.serializedQueryParams).toBe('');
65+
expect(request.signal).toBeInstanceOf(AbortSignal);
66+
expect(request.urlWithParams).toBe('https://phelony.com/comments');
67+
done();
68+
}),
69+
{
70+
wrapper: HttpClientProviderConfigFixture.create(),
71+
}
72+
);
73+
74+
const { result } = renderHook(
75+
() =>
76+
useHttpRequest<{ name: string; role: string }>({
77+
baseUrlOverride: 'https://phelony.com',
78+
relativeUrl: 'comments',
79+
context: reqContext,
80+
}),
81+
{
82+
wrapper: HttpClientProviderConfigFixture.create(),
83+
}
84+
);
85+
86+
const [, performRequest] = result.current;
87+
88+
(async () => {
89+
await act(async () => {
90+
await performRequest();
91+
});
92+
})();
93+
});
94+
95+
it('should run the registered handler when a request goes in error', (done) => {
96+
const fetchError = new Error('Fetch error');
97+
fetch.mockRejectOnce(fetchError);
98+
99+
const showGlobalLoader = new HttpContextToken(true);
100+
const reqContext = new HttpContext().set(showGlobalLoader, false);
101+
102+
renderHook(
103+
() =>
104+
useHttpEvent(RequestErroredEvent, (error: HttpError<FetchResponse, void>) => {
105+
expect(error.status).toBeUndefined();
106+
expect(error.statusText).toBeUndefined();
107+
expect(error.response).toBeUndefined();
108+
expect(error.nativeError).toBe(fetchError);
109+
expect(error.request?.url).toBe('https://phelony.com/comments');
110+
expect(error.request?.baseUrl).toBe('https://phelony.com');
111+
expect(error.request?.relativeUrl).toBe('comments');
112+
expect(error.request?.body).toBeUndefined();
113+
expect(error.request?.headers).toEqual(defaultHttpReqConfig.reqOptions.headers);
114+
expect(error.request?.context).toBe(reqContext);
115+
expect(error.request?.getContextValue(showGlobalLoader)).toBe(false);
116+
expect(error.request?.credentials).toBeUndefined();
117+
expect(error.request?.maxAge).toBe(0);
118+
expect(error.request?.method).toBe(HttpMethod.Get);
119+
expect(error.request?.queryParams).toBeUndefined();
120+
expect(error.request?.serializedQueryParams).toBe('');
121+
expect(error.request?.signal).toBeInstanceOf(AbortSignal);
122+
expect(error.request?.urlWithParams).toBe('https://phelony.com/comments');
123+
done();
124+
}),
125+
{
126+
wrapper: HttpClientProviderConfigFixture.create(),
127+
}
128+
);
129+
const { result } = renderHook(
130+
() =>
131+
useHttpRequest<{ name: string; role: string }>({
132+
baseUrlOverride: 'https://phelony.com',
133+
relativeUrl: 'comments',
134+
context: reqContext,
135+
}),
136+
{
137+
wrapper: HttpClientProviderConfigFixture.create(),
138+
}
139+
);
140+
const [, performRequest] = result.current;
141+
(async () => {
142+
await act(async () => {
143+
try {
144+
await performRequest();
145+
} catch (error) {}
146+
});
147+
})();
148+
});
149+
150+
it('should run the registered handler when a request completes', (done) => {
151+
fetch.mockResponseOnce(JSON.stringify(fetchResponse));
152+
153+
const showGlobalLoader = new HttpContextToken(true);
154+
const reqContext = new HttpContext().set(showGlobalLoader, false);
155+
156+
renderHook(
157+
() =>
158+
useHttpEvent(
159+
RequestSuccededEvent,
160+
({ request, response }: RequestSuccededEventPayload<FetchResponse, void>) => {
161+
expect(request.url).toBe('https://phelony.com/comments');
162+
expect(request.baseUrl).toBe('https://phelony.com');
163+
expect(request.relativeUrl).toBe('comments');
164+
expect(request.body).toBeUndefined();
165+
expect(request.headers).toEqual(defaultHttpReqConfig.reqOptions.headers);
166+
expect(request.context).toBe(reqContext);
167+
expect(request.getContextValue(showGlobalLoader)).toBe(false);
168+
expect(request.credentials).toBeUndefined();
169+
expect(request.maxAge).toBe(0);
170+
expect(request.method).toBe(HttpMethod.Get);
171+
expect(request.queryParams).toBeUndefined();
172+
expect(request.serializedQueryParams).toBe('');
173+
expect(request.signal).toBeInstanceOf(AbortSignal);
174+
expect(request.urlWithParams).toBe('https://phelony.com/comments');
175+
expect(response).toEqual(fetchResponse);
176+
done();
177+
}
178+
),
179+
{
180+
wrapper: HttpClientProviderConfigFixture.create(),
181+
}
182+
);
183+
184+
const { result } = renderHook(
185+
() =>
186+
useHttpRequest<FetchResponse>({
187+
baseUrlOverride: 'https://phelony.com',
188+
relativeUrl: 'comments',
189+
context: reqContext,
190+
}),
191+
{
192+
wrapper: HttpClientProviderConfigFixture.create(),
193+
}
194+
);
195+
196+
const [, performRequest] = result.current;
197+
198+
(async () => {
199+
await act(async () => {
200+
await performRequest();
201+
});
202+
})();
203+
});
204+
});

0 commit comments

Comments
 (0)