Skip to content

Commit 16d731b

Browse files
committed
updated tests
1 parent 9291c5b commit 16d731b

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ async function fetchData(url, method, data) {
99
body: !!data ? JSON.stringify(data) : null,
1010
headers: {
1111
"Content-Type": "application/json",
12-
Accept: "application/json",
13-
"Access-Control-Allow-Origin": origin
12+
Accept: "application/json"
1413
}
1514
})
1615
.then(response => {

index.test.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ let mockResponse = {};
1313
let mockJsonPromise = Promise.resolve(mockResponse);
1414
let mockFetchPromise;
1515

16-
afterEach(() => {
17-
global.fetch.mockClear();
16+
afterAll(() => {
1817
delete global.fetch;
1918
});
2019

@@ -33,10 +32,14 @@ describe("useApi", () => {
3332
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
3433
});
3534

36-
it("calls fetch with the expected parameters", done => {
35+
it("returns the expected response", () => {
3736
const { get } = useApi();
38-
get(url);
37+
expect(get(url)).resolves.toEqual(mockResponse);
38+
});
3939

40+
it("calls fetch with the expected parameters", async done => {
41+
const { get } = useApi();
42+
await get(url);
4043
expect(global.fetch).toHaveBeenCalledWith(
4144
url,
4245
expect.objectContaining({
@@ -88,10 +91,10 @@ describe("useApi", () => {
8891
});
8992

9093
xdescribe("with an onUnauthorized handler passed in", () => {
91-
it("calls the unauthorizedHandler", async () => {
94+
it("calls the unauthorizedHandler", () => {
9295
const onUnauthorized = jest.fn;
9396
const { get } = useApi(onUnauthorized);
94-
await get(url);
97+
get(url);
9598
expect(onUnauthorized).toHaveBeenCalled();
9699
});
97100
});
@@ -136,9 +139,14 @@ describe("useApi", () => {
136139
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
137140
});
138141

139-
it("calls fetch with the expected parameters", done => {
142+
it("returns the expected response", () => {
143+
const { post } = useApi();
144+
expect(post(url, data)).resolves.toEqual(mockResponse);
145+
});
146+
147+
it("calls fetch with the expected parameters", async done => {
140148
const { post } = useApi();
141-
post(url, data);
149+
await post(url, data);
142150

143151
expect(global.fetch).toHaveBeenCalledWith(
144152
url,
@@ -192,10 +200,10 @@ describe("useApi", () => {
192200
});
193201

194202
xdescribe("with an onUnauthorized handler passed in", () => {
195-
it("calls the unauthorizedHandler", async () => {
203+
it("calls the unauthorizedHandler", () => {
196204
const onUnauthorized = jest.fn;
197205
const { post } = useApi(onUnauthorized);
198-
await post(url, data);
206+
post(url, data);
199207
expect(onUnauthorized).toHaveBeenCalled();
200208
});
201209
});
@@ -240,9 +248,14 @@ describe("useApi", () => {
240248
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
241249
});
242250

243-
it("calls fetch with the expected parameters", done => {
251+
it("returns the expected response", () => {
244252
const { put } = useApi();
245-
put(url, data);
253+
expect(put(url, data)).resolves.toEqual(mockResponse);
254+
});
255+
256+
it("calls fetch with the expected parameters", async done => {
257+
const { put } = useApi();
258+
await put(url, data);
246259

247260
expect(global.fetch).toHaveBeenCalledWith(
248261
url,
@@ -296,10 +309,10 @@ describe("useApi", () => {
296309
});
297310

298311
xdescribe("with an onUnauthorized handler passed in", () => {
299-
it("calls the unauthorizedHandler", async () => {
312+
it("calls the unauthorizedHandler", () => {
300313
const onUnauthorized = jest.fn;
301314
const { put } = useApi(onUnauthorized);
302-
await put(url, data);
315+
put(url, data);
303316
expect(onUnauthorized).toHaveBeenCalled();
304317
});
305318
});
@@ -343,9 +356,14 @@ describe("useApi", () => {
343356
global.fetch = jest.fn().mockImplementation(() => mockFetchPromise);
344357
});
345358

346-
it("calls fetch with the expected parameters", done => {
359+
it("returns the expected response", () => {
360+
const { del } = useApi();
361+
expect(del(url)).resolves.toEqual(mockResponse);
362+
});
363+
364+
it("calls fetch with the expected parameters", async done => {
347365
const { del } = useApi();
348-
del(url);
366+
await del(url);
349367

350368
expect(global.fetch).toHaveBeenCalledWith(
351369
url,
@@ -398,10 +416,10 @@ describe("useApi", () => {
398416
});
399417

400418
xdescribe("with an onUnauthorized handler passed in", () => {
401-
it("calls the unauthorizedHandler", async () => {
419+
it("calls the unauthorizedHandler", () => {
402420
const onUnauthorized = jest.fn;
403421
const { del } = useApi(onUnauthorized);
404-
await del(url);
422+
del(url);
405423
expect(onUnauthorized).toHaveBeenCalled();
406424
});
407425
});

0 commit comments

Comments
 (0)