Skip to content

Commit e781885

Browse files
committed
remove jest-fetch-mock and initialProps from tests
1 parent b669d6f commit e781885

File tree

4 files changed

+21
-89
lines changed

4 files changed

+21
-89
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@
6262
}
6363
},
6464
"setupFilesAfterEnv": [
65-
"@testing-library/jest-dom/extend-expect",
66-
"<rootDir>/test/setupTests.ts"
65+
"@testing-library/jest-dom/extend-expect"
6766
]
6867
},
6968
"devDependencies": {
@@ -74,7 +73,6 @@
7473
"@types/react": "^16.9.9",
7574
"@types/react-dom": "^16.9.2",
7675
"husky": "^2.2.0",
77-
"jest-fetch-mock": "^2.1.2",
7876
"prettier": "^1.17.0",
7977
"pretty-quick": "^1.10.0",
8078
"react": "^16.10.2",

test/setupTests.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/useAsync.test.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { useAsync, UseAsyncReturn } from '../src';
2-
import { FetchMock } from 'jest-fetch-mock/types';
32
import { renderHook } from '@testing-library/react-hooks';
43

5-
const fetch: FetchMock = global.fetch;
6-
74
interface StarwarsHero {
85
name: string;
96
}
@@ -19,61 +16,34 @@ export const generateMockResponseData = (amount: number = 5): StarwarsHero[] =>
1916
}));
2017

2118
describe('useAync', () => {
22-
const fetchStarwarsHeroes = async (): Promise<StarwarsHero[]> => {
23-
const result = await fetch(`https://swapi.co/api/people/`);
24-
25-
return await result.json();
26-
};
27-
28-
const props = {
29-
asyncFunction: fetchStarwarsHeroes,
30-
};
31-
3219
const fakeResults = generateMockResponseData();
3320

34-
beforeEach(() => {
35-
fetch.resetMocks();
36-
});
37-
3821
it('should have a useAsync hook', () => {
3922
expect(useAsync).toBeDefined();
4023
});
4124

42-
it('should set loading flag when request is initially made', () => {
43-
fetch.mockResponseOnce(JSON.stringify(fakeResults));
44-
45-
const { result } = renderHook<
46-
StarwarsHeroArgs,
47-
UseAsyncReturn<StarwarsHero[]>
48-
>(p => useAsync<StarwarsHero[], any[]>(p.asyncFunction, []), {
49-
initialProps: { ...props },
50-
});
51-
52-
expect(result.current.loading).toBe(true);
53-
expect(result.current.error).toBeUndefined();
54-
expect(result.current.result).toBeUndefined();
55-
});
56-
5725
it('should resolve a successful request', async () => {
58-
fetch.mockResponseOnce(JSON.stringify(fakeResults));
59-
6026
const onSuccess = jest.fn();
6127
const onError = jest.fn();
6228

6329
const { result, waitForNextUpdate } = renderHook<
6430
StarwarsHeroArgs,
6531
UseAsyncReturn<StarwarsHero[]>
66-
>(
67-
p =>
68-
useAsync<StarwarsHero[], any>(p.asyncFunction, [], {
32+
>(() =>
33+
useAsync<StarwarsHero[], any>(
34+
async () => {
35+
return Promise.resolve(fakeResults);
36+
},
37+
[],
38+
{
6939
onSuccess: () => onSuccess(),
7040
onError: () => onError(),
71-
}),
72-
{
73-
initialProps: { ...props },
74-
}
41+
}
42+
)
7543
);
7644

45+
expect(result.current.loading).toBe(true);
46+
7747
await waitForNextUpdate();
7848

7949
expect(result.current.result).toEqual(fakeResults);
@@ -84,23 +54,23 @@ describe('useAync', () => {
8454
});
8555

8656
it('should set error detail for unsuccessful request', async () => {
87-
fetch.mockReject(new Error('something went wrong'));
88-
8957
const onSuccess = jest.fn();
9058
const onError = jest.fn();
9159

9260
const { result, waitForNextUpdate } = renderHook<
9361
StarwarsHeroArgs,
9462
UseAsyncReturn<StarwarsHero[]>
95-
>(
96-
p =>
97-
useAsync<StarwarsHero[], any>(p.asyncFunction, [], {
63+
>(() =>
64+
useAsync<StarwarsHero[], any>(
65+
async () => {
66+
return Promise.reject(new Error('something went wrong'));
67+
},
68+
[],
69+
{
9870
onSuccess: () => onSuccess(),
9971
onError: () => onError(),
100-
}),
101-
{
102-
initialProps: { ...props },
103-
}
72+
}
73+
)
10474
);
10575

10676
await waitForNextUpdate();

yarn.lock

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,14 +2182,6 @@ [email protected]:
21822182
cross-spawn "^6.0.5"
21832183
is-windows "^1.0.0"
21842184

2185-
cross-fetch@^2.2.2:
2186-
version "2.2.3"
2187-
resolved "https://registry.npmjs.org/cross-fetch/-/cross-fetch-2.2.3.tgz#e8a0b3c54598136e037f8650f8e823ccdfac198e"
2188-
integrity sha512-PrWWNH3yL2NYIb/7WF/5vFG3DCQiXDOVf8k3ijatbrtnwNuhMWLC7YF7uqf53tbTFDzHIUD8oITw4Bxt8ST3Nw==
2189-
dependencies:
2190-
node-fetch "2.1.2"
2191-
whatwg-fetch "2.0.4"
2192-
21932185
cross-spawn@^5.0.1:
21942186
version "5.1.0"
21952187
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
@@ -3595,14 +3587,6 @@ jest-environment-node@^24.8.0:
35953587
jest-mock "^24.8.0"
35963588
jest-util "^24.8.0"
35973589

3598-
jest-fetch-mock@^2.1.2:
3599-
version "2.1.2"
3600-
resolved "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-2.1.2.tgz#1260b347918e3931c4ec743ceaf60433da661bd0"
3601-
integrity sha512-tcSR4Lh2bWLe1+0w/IwvNxeDocMI/6yIA2bijZ0fyWxC4kQ18lckQ1n7Yd40NKuisGmcGBRFPandRXrW/ti/Bw==
3602-
dependencies:
3603-
cross-fetch "^2.2.2"
3604-
promise-polyfill "^7.1.1"
3605-
36063590
jest-get-type@^22.1.0:
36073591
version "22.4.3"
36083592
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-22.4.3.tgz#e3a8504d8479342dd4420236b322869f18900ce4"
@@ -4452,11 +4436,6 @@ nice-try@^1.0.4:
44524436
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
44534437
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==
44544438

4455-
4456-
version "2.1.2"
4457-
resolved "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz#ab884e8e7e57e38a944753cec706f788d1768bb5"
4458-
integrity sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=
4459-
44604439
node-int64@^0.4.0:
44614440
version "0.4.0"
44624441
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
@@ -5024,11 +5003,6 @@ promise-inflight@^1.0.1:
50245003
resolved "https://registry.yarnpkg.com/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3"
50255004
integrity sha1-mEcocL8igTL8vdhoEputEsPAKeM=
50265005

5027-
promise-polyfill@^7.1.1:
5028-
version "7.1.2"
5029-
resolved "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-7.1.2.tgz#ab05301d8c28536301622d69227632269a70ca3b"
5030-
integrity sha512-FuEc12/eKqqoRYIGBrUptCBRhobL19PS2U31vMNTfyck1FxPyMfgsXyW4Mav85y/ZN1hop3hOwRlUDok23oYfQ==
5031-
50325006
prompts@^2.0.1:
50335007
version "2.0.4"
50345008
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.0.4.tgz#179f9d4db3128b9933aa35f93a800d8fce76a682"
@@ -6510,11 +6484,6 @@ whatwg-encoding@^1.0.1, whatwg-encoding@^1.0.3:
65106484
dependencies:
65116485
iconv-lite "0.4.24"
65126486

6513-
6514-
version "2.0.4"
6515-
resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f"
6516-
integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng==
6517-
65186487
whatwg-mimetype@^2.1.0, whatwg-mimetype@^2.2.0:
65196488
version "2.3.0"
65206489
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"

0 commit comments

Comments
 (0)