diff --git a/addon-test-support/-private/mock-server.js b/addon-test-support/-private/mock-server.js
index 5ea6fda9..123a94d4 100644
--- a/addon-test-support/-private/mock-server.js
+++ b/addon-test-support/-private/mock-server.js
@@ -1,6 +1,6 @@
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: {
@@ -8,6 +8,7 @@ let createMock = async function(path, method, statusCode, response) {
},
body: JSON.stringify({
path,
+ body,
method,
statusCode,
response
@@ -15,28 +16,48 @@ let createMock = async function(path, 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 };
diff --git a/index.js b/index.js
index 128bd29b..91d638bf 100644
--- a/index.js
+++ b/index.js
@@ -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() });
diff --git a/package.json b/package.json
index 1f9e7cc2..c54ec7c2 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/tests/dummy/app/pods/examples/network/graphql/simple/route.js b/tests/dummy/app/pods/examples/network/graphql/simple/route.js
new file mode 100644
index 00000000..67b1c734
--- /dev/null
+++ b/tests/dummy/app/pods/examples/network/graphql/simple/route.js
@@ -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 });
+ }
+
+});
diff --git a/tests/dummy/app/pods/examples/network/graphql/simple/template.hbs b/tests/dummy/app/pods/examples/network/graphql/simple/template.hbs
new file mode 100644
index 00000000..99ac66c4
--- /dev/null
+++ b/tests/dummy/app/pods/examples/network/graphql/simple/template.hbs
@@ -0,0 +1,5 @@
+The hello response from the server:
+
+
+ {{model.hello}}
+
diff --git a/tests/dummy/app/pods/examples/network/graphql/variables/route.js b/tests/dummy/app/pods/examples/network/graphql/variables/route.js
new file mode 100644
index 00000000..709de958
--- /dev/null
+++ b/tests/dummy/app/pods/examples/network/graphql/variables/route.js
@@ -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
+ }
+ });
+ }
+
+});
diff --git a/tests/dummy/app/pods/examples/network/graphql/variables/template.hbs b/tests/dummy/app/pods/examples/network/graphql/variables/template.hbs
new file mode 100644
index 00000000..9061395a
--- /dev/null
+++ b/tests/dummy/app/pods/examples/network/graphql/variables/template.hbs
@@ -0,0 +1,9 @@
+The hero response from the server:
+
+
+ {{model.id}}
+
+
+
+ {{model.name}}
+
diff --git a/tests/dummy/app/pods/examples/network/other/get-request/route.js b/tests/dummy/app/pods/examples/network/other/get-request/route.js
index e073a39d..3f4fabf3 100644
--- a/tests/dummy/app/pods/examples/network/other/get-request/route.js
+++ b/tests/dummy/app/pods/examples/network/other/get-request/route.js
@@ -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();
}
diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js
index 40077fb8..7fbc8ba6 100644
--- a/tests/dummy/app/router.js
+++ b/tests/dummy/app/router.js
@@ -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');
diff --git a/tests/dummy/app/services/graphql.js b/tests/dummy/app/services/graphql.js
new file mode 100644
index 00000000..95ca27b2
--- /dev/null
+++ b/tests/dummy/app/services/graphql.js
@@ -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;
+ }
+ }
+});
diff --git a/tests/fastboot/graphql-mocking-test.js b/tests/fastboot/graphql-mocking-test.js
new file mode 100644
index 00000000..42c2d888
--- /dev/null
+++ b/tests/fastboot/graphql-mocking-test.js
@@ -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");
+ });
+});
diff --git a/tests/fastboot/network-mocking-test.js b/tests/fastboot/network-mocking-test.js
index 22a70661..8af07e89 100644
--- a/tests/fastboot/network-mocking-test.js
+++ b/tests/fastboot/network-mocking-test.js
@@ -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'},
]);
@@ -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');
diff --git a/tests/index.html b/tests/index.html
index 5209b852..6f4b1728 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -17,7 +17,7 @@
{{content-for "head-footer"}}
{{content-for "test-head-footer"}}
-
+
{{content-for "body"}}
{{content-for "test-body"}}