|
9 | 9 | let data; |
10 | 10 | let status; |
11 | 11 | let ok; |
12 | | -let mockResponse = {}; |
| 12 | +let mockResponse = { data: "here" }; |
13 | 13 | let mockJsonPromise = Promise.resolve(mockResponse); |
14 | 14 | let mockFetchPromise; |
15 | 15 |
|
@@ -56,22 +56,47 @@ describe("useApi", () => { |
56 | 56 | }); |
57 | 57 |
|
58 | 58 | describe("when the response is successful", () => { |
59 | | - it("returns the response as json", done => { |
60 | | - const { get } = useApi(); |
61 | | - const response = get(url); |
62 | | - expect(response).toEqual(mockJsonPromise); |
| 59 | + describe("when the status code is 204 No Content", () => { |
| 60 | + beforeEach(() => { |
| 61 | + status = 204; |
| 62 | + mockFetchPromise = Promise.resolve({ |
| 63 | + status: status, |
| 64 | + ok: ok, |
| 65 | + json: () => Promise.resolve({}) |
| 66 | + }); |
| 67 | + |
| 68 | + global.fetch = jest.fn().mockImplementation(() => mockFetchPromise); |
| 69 | + }); |
63 | 70 |
|
64 | | - process.nextTick(() => { |
65 | | - global.fetch.mockClear(); |
66 | | - done(); |
| 71 | + it("returns an empty json object", done => { |
| 72 | + const { get } = useApi(); |
| 73 | + const response = get(url); |
| 74 | + expect(response).toEqual(Promise.resolve({})); |
| 75 | + |
| 76 | + process.nextTick(() => { |
| 77 | + global.fetch.mockClear(); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe("when the status code is any other successful status", () => { |
| 84 | + it("returns the response as json", done => { |
| 85 | + const { get } = useApi(); |
| 86 | + const response = get(url); |
| 87 | + expect(response).toEqual(mockJsonPromise); |
| 88 | + |
| 89 | + process.nextTick(() => { |
| 90 | + global.fetch.mockClear(); |
| 91 | + done(); |
| 92 | + }); |
67 | 93 | }); |
68 | 94 | }); |
69 | 95 | }); |
70 | 96 |
|
71 | 97 | describe("when the response is not successful", () => { |
72 | 98 | describe("when the status code is 401 Unauthorized", () => { |
73 | 99 | beforeEach(() => { |
74 | | - url = "https://jsonplaceholder.typicode.com/todos/1"; |
75 | 100 | status = 401; |
76 | 101 | ok = false; |
77 | 102 | mockFetchPromise = Promise.resolve({ |
@@ -102,7 +127,6 @@ describe("useApi", () => { |
102 | 127 |
|
103 | 128 | describe("when the status code is any other error ", () => { |
104 | 129 | beforeEach(() => { |
105 | | - url = "https://jsonplaceholder.typicode.com/todos/1"; |
106 | 130 | status = 500; |
107 | 131 | ok = false; |
108 | 132 | mockFetchPromise = Promise.resolve({ |
@@ -164,22 +188,47 @@ describe("useApi", () => { |
164 | 188 | }); |
165 | 189 |
|
166 | 190 | describe("when the response is successful", () => { |
167 | | - it("returns the response as json", done => { |
168 | | - const { post } = useApi(); |
169 | | - const response = post(url, data); |
170 | | - expect(response).toEqual(mockJsonPromise); |
| 191 | + describe("when the status code is 204 No Content", () => { |
| 192 | + beforeEach(() => { |
| 193 | + status = 204; |
| 194 | + mockFetchPromise = Promise.resolve({ |
| 195 | + status: status, |
| 196 | + ok: ok, |
| 197 | + json: () => Promise.resolve({}) |
| 198 | + }); |
| 199 | + |
| 200 | + global.fetch = jest.fn().mockImplementation(() => mockFetchPromise); |
| 201 | + }); |
171 | 202 |
|
172 | | - process.nextTick(() => { |
173 | | - global.fetch.mockClear(); |
174 | | - done(); |
| 203 | + it("returns an empty json object", done => { |
| 204 | + const { post } = useApi(); |
| 205 | + const response = post(url, data); |
| 206 | + expect(response).toEqual(Promise.resolve({})); |
| 207 | + |
| 208 | + process.nextTick(() => { |
| 209 | + global.fetch.mockClear(); |
| 210 | + done(); |
| 211 | + }); |
| 212 | + }); |
| 213 | + }); |
| 214 | + |
| 215 | + describe("when the status code is any other successful status", () => { |
| 216 | + it("returns the response as json", done => { |
| 217 | + const { post } = useApi(); |
| 218 | + const response = post(url, data); |
| 219 | + expect(response).toEqual(mockJsonPromise); |
| 220 | + |
| 221 | + process.nextTick(() => { |
| 222 | + global.fetch.mockClear(); |
| 223 | + done(); |
| 224 | + }); |
175 | 225 | }); |
176 | 226 | }); |
177 | 227 | }); |
178 | 228 |
|
179 | 229 | describe("when the response is not successful", () => { |
180 | 230 | describe("when the status code is 401 Unauthorized", () => { |
181 | 231 | beforeEach(() => { |
182 | | - url = "https://jsonplaceholder.typicode.com/todos"; |
183 | 232 | data = { title: "Gotta do all the things!", completed: false }; |
184 | 233 | status = 401; |
185 | 234 | ok = false; |
@@ -211,7 +260,6 @@ describe("useApi", () => { |
211 | 260 |
|
212 | 261 | describe("when the status code is any other error ", () => { |
213 | 262 | beforeEach(() => { |
214 | | - url = "https://jsonplaceholder.typicode.com/todos"; |
215 | 263 | status = 500; |
216 | 264 | ok = false; |
217 | 265 | mockFetchPromise = Promise.resolve({ |
@@ -273,22 +321,47 @@ describe("useApi", () => { |
273 | 321 | }); |
274 | 322 |
|
275 | 323 | describe("when the response is successful", () => { |
276 | | - it("returns the response as json", done => { |
277 | | - const { put } = useApi(); |
278 | | - const response = put(url, data); |
279 | | - expect(response).toEqual(mockJsonPromise); |
| 324 | + describe("when the status code is 204 No Content", () => { |
| 325 | + beforeEach(() => { |
| 326 | + status = 204; |
| 327 | + mockFetchPromise = Promise.resolve({ |
| 328 | + status: status, |
| 329 | + ok: ok, |
| 330 | + json: () => Promise.resolve({}) |
| 331 | + }); |
| 332 | + |
| 333 | + global.fetch = jest.fn().mockImplementation(() => mockFetchPromise); |
| 334 | + }); |
280 | 335 |
|
281 | | - process.nextTick(() => { |
282 | | - global.fetch.mockClear(); |
283 | | - done(); |
| 336 | + it("returns an empty json object", done => { |
| 337 | + const { put } = useApi(); |
| 338 | + const response = put(url, data); |
| 339 | + expect(response).toEqual(Promise.resolve({})); |
| 340 | + |
| 341 | + process.nextTick(() => { |
| 342 | + global.fetch.mockClear(); |
| 343 | + done(); |
| 344 | + }); |
| 345 | + }); |
| 346 | + }); |
| 347 | + |
| 348 | + describe("when the status code is any other successful status", () => { |
| 349 | + it("returns the response as json", done => { |
| 350 | + const { put } = useApi(); |
| 351 | + const response = put(url, data); |
| 352 | + expect(response).toEqual(mockJsonPromise); |
| 353 | + |
| 354 | + process.nextTick(() => { |
| 355 | + global.fetch.mockClear(); |
| 356 | + done(); |
| 357 | + }); |
284 | 358 | }); |
285 | 359 | }); |
286 | 360 | }); |
287 | 361 |
|
288 | 362 | describe("when the response is not successful", () => { |
289 | 363 | describe("when the status code is 401 Unauthorized", () => { |
290 | 364 | beforeEach(() => { |
291 | | - url = "https://jsonplaceholder.typicode.com/todos"; |
292 | 365 | data = { title: "Gotta do all the things!", completed: false }; |
293 | 366 | status = 401; |
294 | 367 | ok = false; |
@@ -320,7 +393,6 @@ describe("useApi", () => { |
320 | 393 |
|
321 | 394 | describe("when the status code is any other error ", () => { |
322 | 395 | beforeEach(() => { |
323 | | - url = "https://jsonplaceholder.typicode.com/todos"; |
324 | 396 | status = 500; |
325 | 397 | ok = false; |
326 | 398 | mockFetchPromise = Promise.resolve({ |
@@ -381,22 +453,47 @@ describe("useApi", () => { |
381 | 453 | }); |
382 | 454 |
|
383 | 455 | describe("when the response is successful", () => { |
384 | | - it("returns the response as json", done => { |
385 | | - const { del } = useApi(); |
386 | | - const response = del(url); |
387 | | - expect(response).toEqual(mockJsonPromise); |
| 456 | + describe("when the status code is 204 No Content", () => { |
| 457 | + beforeEach(() => { |
| 458 | + status = 204; |
| 459 | + mockFetchPromise = Promise.resolve({ |
| 460 | + status: status, |
| 461 | + ok: ok, |
| 462 | + json: () => Promise.resolve({}) |
| 463 | + }); |
| 464 | + |
| 465 | + global.fetch = jest.fn().mockImplementation(() => mockFetchPromise); |
| 466 | + }); |
388 | 467 |
|
389 | | - process.nextTick(() => { |
390 | | - global.fetch.mockClear(); |
391 | | - done(); |
| 468 | + it("returns an empty json object", done => { |
| 469 | + const { del } = useApi(); |
| 470 | + const response = del(url); |
| 471 | + expect(response).toEqual(Promise.resolve({})); |
| 472 | + |
| 473 | + process.nextTick(() => { |
| 474 | + global.fetch.mockClear(); |
| 475 | + done(); |
| 476 | + }); |
| 477 | + }); |
| 478 | + }); |
| 479 | + |
| 480 | + describe("when the status code is any other successful status", () => { |
| 481 | + it("returns the response as json", done => { |
| 482 | + const { del } = useApi(); |
| 483 | + const response = del(url); |
| 484 | + expect(response).toEqual(mockJsonPromise); |
| 485 | + |
| 486 | + process.nextTick(() => { |
| 487 | + global.fetch.mockClear(); |
| 488 | + done(); |
| 489 | + }); |
392 | 490 | }); |
393 | 491 | }); |
394 | 492 | }); |
395 | 493 |
|
396 | 494 | describe("when the response is not successful", () => { |
397 | 495 | describe("when the status code is 401 Unauthorized", () => { |
398 | 496 | beforeEach(() => { |
399 | | - url = "https://jsonplaceholder.typicode.com/todos/1"; |
400 | 497 | status = 401; |
401 | 498 | ok = false; |
402 | 499 | mockFetchPromise = Promise.resolve({ |
@@ -427,7 +524,6 @@ describe("useApi", () => { |
427 | 524 |
|
428 | 525 | describe("when the status code is any other error ", () => { |
429 | 526 | beforeEach(() => { |
430 | | - url = "https://jsonplaceholder.typicode.com/todos/1"; |
431 | 527 | status = 500; |
432 | 528 | ok = false; |
433 | 529 | mockFetchPromise = Promise.resolve({ |
|
0 commit comments