Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking post requests #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
app.post('/__mock-request', bodyParser.json(), (req, res) => {
let mock = nock(req.headers.origin)
.persist()
.intercept(req.body.path, req.body.method)
.intercept(req.body.path, req.body.method, req.body.body)
.reply(req.body.statusCode, req.body.response);

res.json({ mocks: mock.pendingMocks() });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"ember-cli-babel": "^6.6.0",
"fastboot": "^1.2.1",
"jquery-param": "^1.0.1",
"resolve": "^1.10.0",
"nock": "^10.0.6",
"resolve": "^1.10.0",
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
Expand Down
12 changes: 12 additions & 0 deletions tests/dummy/app/pods/examples/network/graphql/simple/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
graphql: service(),

model() {
let query = `{ hello }`;
return this.get('graphql').request({ query });
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The hello response from the server:

<div data-test-id="hello">
{{model.hello}}
</div>
23 changes: 23 additions & 0 deletions tests/dummy/app/pods/examples/network/graphql/variables/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
graphql: service(),

model(params) {
let query = `query FindHero($id: String!) {
hero(id: $id) {
id,
name
}
}`;

return this.get('graphql').request({
query,
variables: {
id: params.id
}
});
}

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The hero response from the server:

<div data-test-id="id">
{{model.id}}
</div>

<div data-test-id="name">
{{model.name}}
</div>
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
5 changes: 5 additions & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ Router.map(function() {
this.route('post', { path: ':post_id' });
});

this.route('graphql', function() {
this.route('simple');
this.route('variables', { path: 'variables/:id' });
});

this.route('other', function() {
this.route('get-request');
this.route('post-request');
Expand Down
29 changes: 29 additions & 0 deletions tests/dummy/app/services/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Service from '@ember/service';
import fetch from 'fetch';

export default Service.extend({
async request({ query, variables }) {
let body = { query };

if (variables) {
body.variables = variables;
}

let response = await fetch('/graphql', {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify(body)
});

let { data, errors } = await response.json();

if (errors) {
throw errors;
} else {
return data;
}
}
});
57 changes: 57 additions & 0 deletions tests/fastboot/graphql-mocking-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { module, test } from 'qunit';
import { setup, visit, mockServer } from 'ember-cli-fastboot-testing/test-support';

module('Fastboot | graphql mocking', function(hooks) {
setup(hooks);

test('it can mock a graphql request', async function(assert) {
await mockServer
.post('/graphql', {
query: "{ hello }"
})
.reply({
data: {
hello: "Hello world!"
}
});

await visit('/examples/network/graphql/simple');

assert.dom('[data-test-id="hello"]').hasText("Hello world!");
});

test('it can mock multiple graphql requests with variables', async function(assert) {
let query = `query FindHero($id: String!) {
hero(id: $id) {
id,
name
}
}`;

let heros = [{
id: "123",
name: "Luke Skywalker"
},{
id: "456",
name: "Han Solo"
}];

for (let hero of heros) {
await mockServer.post('/graphql', {
query,
variables: { id: hero.id }
})
.reply({ data: hero });
}

await visit('/examples/network/graphql/variables/123');

assert.dom('[data-test-id="id"]').hasText("123");
assert.dom('[data-test-id="name"]').hasText("Luke Skywalker");

await visit('/examples/network/graphql/variables/456');

assert.dom('[data-test-id="id"]').hasText("456");
assert.dom('[data-test-id="name"]').hasText("Han Solo");
});
});
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