Skip to content

Commit 0960bf7

Browse files
committed
Add MockPost class
It exposes more options for mocking post requests.
1 parent 61065f5 commit 0960bf7

4 files changed

Lines changed: 50 additions & 24 deletions

File tree

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,63 @@
11
import { fetch } from 'whatwg-fetch';
22

3-
let createMock = async function(path, method, statusCode, response) {
3+
let createMock = async function({ path, method, body, statusCode, response }) {
44
return await fetch('/__mock-request', {
55
method: 'post',
66
headers: {
77
"Content-Type": "application/json",
88
},
99
body: JSON.stringify({
1010
path,
11+
body,
1112
method,
1213
statusCode,
1314
response
1415
}),
1516
});
1617
}
1718

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+
});
3329
},
3430

35-
async delete(path, response, status = 200) {
36-
return createMock(path, "DELETE", status, response);
31+
post(path, body = {}) {
32+
return new MockPost({ path, body });
3733
},
3834

3935
async cleanUp() {
4036
return fetch('/__cleanup-mocks');
4137
}
4238
};
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 };

tests/dummy/app/pods/examples/network/other/get-request/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import fetch from 'fetch';
44
export default Route.extend({
55

66
async model() {
7-
let response = await fetch('/api/posts', { method: 'post' });
7+
let response = await fetch('/api/posts');
88
return await response.json();
99
}
1010

tests/fastboot/network-mocking-test.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module('Fastboot | network mocking', function(hooks) {
6868
});
6969

7070
test('it can mock a get request', async function(assert) {
71-
await mockServer.post('/api/posts', [
71+
await mockServer.get('/api/posts', [
7272
{ id: 1, title: 'get post'},
7373
]);
7474

@@ -78,9 +78,14 @@ module('Fastboot | network mocking', function(hooks) {
7878
});
7979

8080
test('it can mock a post request', async function(assert) {
81-
await mockServer.post('/api/posts', [
82-
{ id: 1, title: 'post post'},
83-
]);
81+
await mockServer
82+
.post('/api/posts')
83+
.reply([
84+
{
85+
id: 1,
86+
title: 'post post'
87+
}
88+
]);
8489

8590
await visit('/examples/network/other/post-request');
8691

tests/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{{content-for "head-footer"}}
1818
{{content-for "test-head-footer"}}
1919
</head>
20-
<body>
20+
<body style="background-color: white !important;">
2121
{{content-for "body"}}
2222
{{content-for "test-body"}}
2323

0 commit comments

Comments
 (0)