Skip to content

Commit

Permalink
Add MockPost class
Browse files Browse the repository at this point in the history
It exposes more options for mocking post requests.
  • Loading branch information
ryanto committed May 19, 2019
1 parent 61065f5 commit 0960bf7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
57 changes: 39 additions & 18 deletions addon-test-support/-private/mock-server.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,63 @@
import { fetch } from 'whatwg-fetch';

let createMock = async function(path, method, statusCode, response) {
let createMock = async function({ path, method, body, statusCode, response }) {
return await fetch('/__mock-request', {
method: 'post',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
path,
body,
method,
statusCode,
response
}),
});
}

export let mockServer = {
async get(path, response, status = 200) {
return createMock(path, "GET", status, response);
},

async post(path, response, status = 200) {
return createMock(path, "POST", status, response);
},

async patch(path, response, status = 200) {
return createMock(path, "PATCH", status, response);
},

async put(path, response, status = 200) {
return createMock(path, "PUT", status, response);
let mockServer = {
get(path, response, statusCode = 200) {
// when needed we'll change this to use something like MockGet
// see MockPost for the idea
return createMock({
path,
method: "GET",
statusCode,
response
});
},

async delete(path, response, status = 200) {
return createMock(path, "DELETE", status, response);
post(path, body = {}) {
return new MockPost({ path, body });
},

async cleanUp() {
return fetch('/__cleanup-mocks');
}
};

class MockPost {
constructor({ path, body }) {
this.path = path;
this.body = body;
this.status = 200;
}

statusCode(status) {
this.status = status;
return this;
}

reply(response) {
return createMock({
path: this.path,
body: this.body,
method: "POST",
statusCode: this.status,
response
});
}
}

export { mockServer };
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fetch from 'fetch';
export default Route.extend({

async model() {
let response = await fetch('/api/posts', { method: 'post' });
let response = await fetch('/api/posts');
return await response.json();
}

Expand Down
13 changes: 9 additions & 4 deletions tests/fastboot/network-mocking-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module('Fastboot | network mocking', function(hooks) {
});

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

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

test('it can mock a post request', async function(assert) {
await mockServer.post('/api/posts', [
{ id: 1, title: 'post post'},
]);
await mockServer
.post('/api/posts')
.reply([
{
id: 1,
title: 'post post'
}
]);

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

Expand Down
2 changes: 1 addition & 1 deletion tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{content-for "head-footer"}}
{{content-for "test-head-footer"}}
</head>
<body>
<body style="background-color: white !important;">
{{content-for "body"}}
{{content-for "test-body"}}

Expand Down

0 comments on commit 0960bf7

Please sign in to comment.