Skip to content

Commit f53ab78

Browse files
author
nebarf
committed
use-http-request specs
1 parent 18e045c commit f53ab78

15 files changed

+524
-33
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { renderHook } from '@testing-library/react-hooks';
33
import { FetchMock } from 'jest-fetch-mock';
44
import {
55
defaultHttpReqConfig,
6+
HttpContext,
7+
HttpContextToken,
68
HttpError,
79
HttpMethod,
810
HttpRequest,
@@ -123,17 +125,21 @@ describe('use-http-client', () => {
123125
wrapper: HttpClientProviderConfigFixture.create(),
124126
});
125127

128+
const showGlobalLoader = new HttpContextToken(true);
129+
const reqContext = new HttpContext().set(showGlobalLoader, false);
130+
126131
const { request } = result.current;
127132

128133
try {
129-
await request({});
134+
await request({ context: reqContext });
130135
} catch (error) {
131136
expect(error).toBeInstanceOf(HttpError);
132137
expect(error.message).toBe(fetchError.message);
133138
expect(error.status).toBeUndefined();
134139
expect(error.statusText).toBeUndefined();
135140
expect(error.response).toBeUndefined();
136141
expect(error.request.url).toBe('/');
142+
expect(error.request.context).toBe(reqContext);
137143
expect(error.nativeError).toBe(fetchError);
138144
}
139145

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { HttpMethod, useHttpDelete } from '..';
3+
import * as useHttpRequestModule from '../request/use-http-request';
4+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
5+
6+
describe('use-http-post', () => {
7+
it('should perform a post request', async () => {
8+
const useHttpRequestMock = jest.spyOn(useHttpRequestModule, 'useHttpRequest');
9+
10+
const { waitForNextUpdate } = renderHook(
11+
() =>
12+
useHttpDelete({
13+
fetchOnBootstrap: true,
14+
relativeUrl: 'posts/1',
15+
}),
16+
{
17+
wrapper: HttpClientProviderConfigFixture.create(),
18+
}
19+
);
20+
21+
await waitForNextUpdate();
22+
// It's called everytime the state is updated.
23+
expect(useHttpRequestMock).toHaveBeenCalledTimes(3);
24+
expect(useHttpRequestMock).toHaveBeenCalledWith({
25+
fetchOnBootstrap: true,
26+
relativeUrl: 'posts/1',
27+
requestOptions: { method: HttpMethod.Delete },
28+
});
29+
});
30+
});

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { HttpMethod, useHttpGet } from '..';
3+
import * as useHttpRequestModule from '../request/use-http-request';
4+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
5+
6+
describe('use-http-post', () => {
7+
it('should perform a post request', async () => {
8+
const useHttpRequestMock = jest.spyOn(useHttpRequestModule, 'useHttpRequest');
9+
10+
const { waitForNextUpdate } = renderHook(
11+
() =>
12+
useHttpGet({
13+
fetchOnBootstrap: true,
14+
relativeUrl: 'posts',
15+
}),
16+
{
17+
wrapper: HttpClientProviderConfigFixture.create(),
18+
}
19+
);
20+
21+
await waitForNextUpdate();
22+
// It's called everytime the state is updated.
23+
expect(useHttpRequestMock).toHaveBeenCalledTimes(3);
24+
expect(useHttpRequestMock).toHaveBeenCalledWith({
25+
fetchOnBootstrap: true,
26+
relativeUrl: 'posts',
27+
requestOptions: { method: HttpMethod.Get },
28+
});
29+
});
30+
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { HttpMethod, useHttpPatch } from '..';
3+
import * as useHttpRequestModule from '../request/use-http-request';
4+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
5+
6+
describe('use-http-post', () => {
7+
it('should perform a post request', async () => {
8+
const useHttpRequestMock = jest.spyOn(useHttpRequestModule, 'useHttpRequest');
9+
10+
const { waitForNextUpdate } = renderHook(
11+
() =>
12+
useHttpPatch({
13+
fetchOnBootstrap: true,
14+
requestOptions: { body: { title: 'Phelony title' } },
15+
}),
16+
{
17+
wrapper: HttpClientProviderConfigFixture.create(),
18+
}
19+
);
20+
21+
await waitForNextUpdate();
22+
// It's called everytime the state is updated.
23+
expect(useHttpRequestMock).toHaveBeenCalledTimes(3);
24+
expect(useHttpRequestMock).toHaveBeenCalledWith({
25+
fetchOnBootstrap: true,
26+
requestOptions: { body: { title: 'Phelony title' }, method: HttpMethod.Patch },
27+
});
28+
});
29+
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { HttpMethod, useHttpPost } from '..';
3+
import * as useHttpRequestModule from '../request/use-http-request';
4+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
5+
6+
describe('use-http-post', () => {
7+
it('should perform a post request', async () => {
8+
const useHttpRequestMock = jest.spyOn(useHttpRequestModule, 'useHttpRequest');
9+
10+
const { waitForNextUpdate } = renderHook(
11+
() =>
12+
useHttpPost({
13+
fetchOnBootstrap: true,
14+
requestOptions: { body: { title: 'Phelony title' } },
15+
}),
16+
{
17+
wrapper: HttpClientProviderConfigFixture.create(),
18+
}
19+
);
20+
21+
await waitForNextUpdate();
22+
// It's called everytime the state is updated.
23+
expect(useHttpRequestMock).toHaveBeenCalledTimes(3);
24+
expect(useHttpRequestMock).toHaveBeenCalledWith({
25+
fetchOnBootstrap: true,
26+
requestOptions: { body: { title: 'Phelony title' }, method: HttpMethod.Post },
27+
});
28+
});
29+
});

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { HttpMethod, useHttpPut } from '..';
3+
import * as useHttpRequestModule from '../request/use-http-request';
4+
import { HttpClientProviderConfigFixture } from './fixtures/http-client-config-provider.fixture';
5+
6+
describe('use-http-post', () => {
7+
it('should perform a post request', async () => {
8+
const useHttpRequestMock = jest.spyOn(useHttpRequestModule, 'useHttpRequest');
9+
10+
const { waitForNextUpdate } = renderHook(
11+
() =>
12+
useHttpPut({
13+
fetchOnBootstrap: true,
14+
requestOptions: { body: { title: 'Phelony title' } },
15+
}),
16+
{
17+
wrapper: HttpClientProviderConfigFixture.create(),
18+
}
19+
);
20+
21+
await waitForNextUpdate();
22+
// It's called everytime the state is updated.
23+
expect(useHttpRequestMock).toHaveBeenCalledTimes(3);
24+
expect(useHttpRequestMock).toHaveBeenCalledWith({
25+
fetchOnBootstrap: true,
26+
requestOptions: { body: { title: 'Phelony title' }, method: HttpMethod.Put },
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)