-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add graphql example tests for post mocking API
- Loading branch information
Showing
10 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/dummy/app/pods/examples/network/graphql/simple/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
|
||
}); |
5 changes: 5 additions & 0 deletions
5
tests/dummy/app/pods/examples/network/graphql/simple/template.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
tests/dummy/app/pods/examples/network/graphql/variables/route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
} | ||
|
||
}); |
9 changes: 9 additions & 0 deletions
9
tests/dummy/app/pods/examples/network/graphql/variables/template.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |