Skip to content

Commit c03e4de

Browse files
committed
initial test
1 parent dd77100 commit c03e4de

File tree

4 files changed

+246
-0
lines changed

4 files changed

+246
-0
lines changed

__tests__/index.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import useNetworkStatusCode from '../src';
2+
import { renderHook, act } from '@testing-library/react-hooks';
3+
import { Server, Response } from 'miragejs';
4+
5+
// mock timer using jest
6+
jest.useFakeTimers();
7+
8+
describe('useMyHook', () => {
9+
const authURL = '/example-endpoint';
10+
it('returns statuscode as undefined when no api calls have been made', () => {
11+
const { result } = renderHook(() =>
12+
useNetworkStatusCode({
13+
urls: [authURL]
14+
})
15+
);
16+
17+
expect(result.current.networkStatusCode).toStrictEqual({
18+
[authURL]: undefined
19+
});
20+
});
21+
22+
it('returns status code of the endpoints specified', () => {
23+
new Server({
24+
routes() {
25+
this.post(authURL, () => {
26+
return new Response(
27+
400,
28+
{ some: 'header' },
29+
{ errors: ['name cannot be blank'] }
30+
);
31+
});
32+
}
33+
});
34+
35+
const { result } = renderHook(() =>
36+
useNetworkStatusCode({
37+
urls: [authURL]
38+
})
39+
);
40+
41+
fetch(authURL);
42+
43+
expect(result.current.networkStatusCode[authURL]).toBe(400);
44+
});
45+
});

package-lock.json

Lines changed: 200 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@types/react": "^16.7.22",
4343
"cross-env": "^5.2.0",
4444
"gh-pages": "^2.0.1",
45+
"miragejs": "^0.1.41",
4546
"react": "^16.9.0",
4647
"react-scripts": "^3.4.0",
4748
"react-test-renderer": "^16.9.0",

src/index.test.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)