From 76fa0e4df551f578de5c9bc8dba10723adf94bff Mon Sep 17 00:00:00 2001 From: Ryan Toronto Date: Sun, 19 May 2019 19:14:09 -0400 Subject: [PATCH] Add graphql example tests for post mocking API --- addon-test-support/-private/mock-server.js | 2 +- index.js | 2 +- package.json | 2 +- .../examples/network/graphql/simple/route.js | 12 +++ .../network/graphql/simple/template.hbs | 5 + .../network/graphql/variables/route.js | 23 +++++ .../network/graphql/variables/template.hbs | 9 ++ tests/dummy/app/router.js | 5 + tests/dummy/app/services/graphql.js | 29 ++++++ tests/fastboot/graphql-mocking-test.js | 92 +++++++++++++++++++ 10 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 tests/dummy/app/pods/examples/network/graphql/simple/route.js create mode 100644 tests/dummy/app/pods/examples/network/graphql/simple/template.hbs create mode 100644 tests/dummy/app/pods/examples/network/graphql/variables/route.js create mode 100644 tests/dummy/app/pods/examples/network/graphql/variables/template.hbs create mode 100644 tests/dummy/app/services/graphql.js create mode 100644 tests/fastboot/graphql-mocking-test.js diff --git a/addon-test-support/-private/mock-server.js b/addon-test-support/-private/mock-server.js index f4fb0095..123a94d4 100644 --- a/addon-test-support/-private/mock-server.js +++ b/addon-test-support/-private/mock-server.js @@ -28,7 +28,7 @@ let mockServer = { }); }, - post(path, body = {}) { + post(path, body) { return new MockPost({ path, body }); }, 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/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..3fcc85a7 --- /dev/null +++ b/tests/fastboot/graphql-mocking-test.js @@ -0,0 +1,92 @@ +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"); + }); + + test('it can mock a graphql request with a regex variable', 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"); + }); +});