|
1 | 1 | import { fetch } from 'whatwg-fetch'; |
2 | 2 |
|
3 | | -let createMock = async function(path, method, statusCode, response) { |
| 3 | +let createMock = async function({ path, method, body, statusCode, response }) { |
4 | 4 | return await fetch('/__mock-request', { |
5 | 5 | method: 'post', |
6 | 6 | headers: { |
7 | 7 | "Content-Type": "application/json", |
8 | 8 | }, |
9 | 9 | body: JSON.stringify({ |
10 | 10 | path, |
| 11 | + body, |
11 | 12 | method, |
12 | 13 | statusCode, |
13 | 14 | response |
14 | 15 | }), |
15 | 16 | }); |
16 | 17 | } |
17 | 18 |
|
18 | | -export let mockServer = { |
19 | | - async get(path, response, status = 200) { |
20 | | - return createMock(path, "GET", status, response); |
21 | | - }, |
22 | | - |
23 | | - async post(path, response, status = 200) { |
24 | | - return createMock(path, "POST", status, response); |
25 | | - }, |
26 | | - |
27 | | - async patch(path, response, status = 200) { |
28 | | - return createMock(path, "PATCH", status, response); |
29 | | - }, |
30 | | - |
31 | | - async put(path, response, status = 200) { |
32 | | - return createMock(path, "PUT", status, response); |
| 19 | +let mockServer = { |
| 20 | + get(path, response, statusCode = 200) { |
| 21 | + // when needed we'll change this to use something like MockGet |
| 22 | + // see MockPost for the idea |
| 23 | + return createMock({ |
| 24 | + path, |
| 25 | + method: "GET", |
| 26 | + statusCode, |
| 27 | + response |
| 28 | + }); |
33 | 29 | }, |
34 | 30 |
|
35 | | - async delete(path, response, status = 200) { |
36 | | - return createMock(path, "DELETE", status, response); |
| 31 | + post(path, body = {}) { |
| 32 | + return new MockPost({ path, body }); |
37 | 33 | }, |
38 | 34 |
|
39 | 35 | async cleanUp() { |
40 | 36 | return fetch('/__cleanup-mocks'); |
41 | 37 | } |
42 | 38 | }; |
| 39 | + |
| 40 | +class MockPost { |
| 41 | + constructor({ path, body }) { |
| 42 | + this.path = path; |
| 43 | + this.body = body; |
| 44 | + this.status = 200; |
| 45 | + } |
| 46 | + |
| 47 | + statusCode(status) { |
| 48 | + this.status = status; |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + reply(response) { |
| 53 | + return createMock({ |
| 54 | + path: this.path, |
| 55 | + body: this.body, |
| 56 | + method: "POST", |
| 57 | + statusCode: this.status, |
| 58 | + response |
| 59 | + }); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +export { mockServer }; |
0 commit comments