Skip to content

Commit 76fa0e4

Browse files
committed
Add graphql example tests for post mocking API
1 parent 0960bf7 commit 76fa0e4

10 files changed

Lines changed: 178 additions & 3 deletions

File tree

addon-test-support/-private/mock-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let mockServer = {
2828
});
2929
},
3030

31-
post(path, body = {}) {
31+
post(path, body) {
3232
return new MockPost({ path, body });
3333
},
3434

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
app.post('/__mock-request', bodyParser.json(), (req, res) => {
4646
let mock = nock(req.headers.origin)
4747
.persist()
48-
.intercept(req.body.path, req.body.method)
48+
.intercept(req.body.path, req.body.method, req.body.body)
4949
.reply(req.body.statusCode, req.body.response);
5050

5151
res.json({ mocks: mock.pendingMocks() });

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
"ember-cli-babel": "^6.6.0",
2727
"fastboot": "^1.2.1",
2828
"jquery-param": "^1.0.1",
29-
"resolve": "^1.10.0",
3029
"nock": "^10.0.6",
30+
"resolve": "^1.10.0",
3131
"whatwg-fetch": "^3.0.0"
3232
},
3333
"devDependencies": {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Route from '@ember/routing/route';
2+
import { inject as service } from '@ember/service';
3+
4+
export default Route.extend({
5+
graphql: service(),
6+
7+
model() {
8+
let query = `{ hello }`;
9+
return this.get('graphql').request({ query });
10+
}
11+
12+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The hello response from the server:
2+
3+
<div data-test-id="hello">
4+
{{model.hello}}
5+
</div>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Route from '@ember/routing/route';
2+
import { inject as service } from '@ember/service';
3+
4+
export default Route.extend({
5+
graphql: service(),
6+
7+
model(params) {
8+
let query = `query FindHero($id: String!) {
9+
hero(id: $id) {
10+
id,
11+
name
12+
}
13+
}`;
14+
15+
return this.get('graphql').request({
16+
query,
17+
variables: {
18+
id: params.id
19+
}
20+
});
21+
}
22+
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The hero response from the server:
2+
3+
<div data-test-id="id">
4+
{{model.id}}
5+
</div>
6+
7+
<div data-test-id="name">
8+
{{model.name}}
9+
</div>

tests/dummy/app/router.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Router.map(function() {
3333
this.route('post', { path: ':post_id' });
3434
});
3535

36+
this.route('graphql', function() {
37+
this.route('simple');
38+
this.route('variables', { path: 'variables/:id' });
39+
});
40+
3641
this.route('other', function() {
3742
this.route('get-request');
3843
this.route('post-request');
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Service from '@ember/service';
2+
import fetch from 'fetch';
3+
4+
export default Service.extend({
5+
async request({ query, variables }) {
6+
let body = { query };
7+
8+
if (variables) {
9+
body.variables = variables;
10+
}
11+
12+
let response = await fetch('/graphql', {
13+
method: 'post',
14+
headers: {
15+
'Content-Type': 'application/json',
16+
'Accept': 'application/json',
17+
},
18+
body: JSON.stringify(body)
19+
});
20+
21+
let { data, errors } = await response.json();
22+
23+
if (errors) {
24+
throw errors;
25+
} else {
26+
return data;
27+
}
28+
}
29+
});
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { module, test } from 'qunit';
2+
import { setup, visit, mockServer } from 'ember-cli-fastboot-testing/test-support';
3+
4+
module('Fastboot | graphql mocking', function(hooks) {
5+
setup(hooks);
6+
7+
test('it can mock a graphql request', async function(assert) {
8+
await mockServer
9+
.post('/graphql', {
10+
query: "{ hello }"
11+
})
12+
.reply({
13+
data: {
14+
hello: "Hello world!"
15+
}
16+
});
17+
18+
await visit('/examples/network/graphql/simple');
19+
20+
assert.dom('[data-test-id="hello"]').hasText("Hello world!");
21+
});
22+
23+
test('it can mock multiple graphql requests with variables', async function(assert) {
24+
let query = `query FindHero($id: String!) {
25+
hero(id: $id) {
26+
id,
27+
name
28+
}
29+
}`;
30+
31+
let heros = [{
32+
id: "123",
33+
name: "Luke Skywalker"
34+
},{
35+
id: "456",
36+
name: "Han Solo"
37+
}];
38+
39+
for (let hero of heros) {
40+
await mockServer.post('/graphql', {
41+
query,
42+
variables: { id: hero.id }
43+
})
44+
.reply({ data: hero });
45+
}
46+
47+
await visit('/examples/network/graphql/variables/123');
48+
49+
assert.dom('[data-test-id="id"]').hasText("123");
50+
assert.dom('[data-test-id="name"]').hasText("Luke Skywalker");
51+
52+
await visit('/examples/network/graphql/variables/456');
53+
54+
assert.dom('[data-test-id="id"]').hasText("456");
55+
assert.dom('[data-test-id="name"]').hasText("Han Solo");
56+
});
57+
58+
test('it can mock a graphql request with a regex variable', async function(assert) {
59+
let query = `query FindHero($id: String!) {
60+
hero(id: $id) {
61+
id,
62+
name
63+
}
64+
}`;
65+
66+
let heros = [{
67+
id: "123",
68+
name: "Luke Skywalker"
69+
},{
70+
id: "456",
71+
name: "Han Solo"
72+
}];
73+
74+
for (let hero of heros) {
75+
await mockServer.post('/graphql', {
76+
query,
77+
variables: { id: hero.id }
78+
})
79+
.reply({ data: hero });
80+
}
81+
82+
await visit('/examples/network/graphql/variables/123');
83+
84+
assert.dom('[data-test-id="id"]').hasText("123");
85+
assert.dom('[data-test-id="name"]').hasText("Luke Skywalker");
86+
87+
await visit('/examples/network/graphql/variables/456');
88+
89+
assert.dom('[data-test-id="id"]').hasText("456");
90+
assert.dom('[data-test-id="name"]').hasText("Han Solo");
91+
});
92+
});

0 commit comments

Comments
 (0)