Skip to content
Merged
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
5 changes: 5 additions & 0 deletions addon/mocks/mutation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { contextSet, isFunction, reduceKeys, unwrapNonNull } from '../utils';
import { GraphQLList } from 'graphql';
import { getRecords } from '../db';
import { resolveArgName } from '../fields/args';

Expand All @@ -13,6 +14,10 @@ export const composeMockMutation = (getRecords, mapArgs) =>
(db, options = {}, _, args, __, { fieldName, returnType }) => {
returnType = unwrapNonNull(returnType);

if (returnType instanceof GraphQLList) {
Comment thread
jneurock marked this conversation as resolved.
returnType = returnType.ofType
}

let { mutations = {}, argsMap = {} } = options;
let mutation = mutations[fieldName];
let records = getRecords(db, returnType.name);
Expand Down
32 changes: 32 additions & 0 deletions tests/acceptance/new-person-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { click, fillIn, visit } from '@ember/test-helpers';

module('Acceptance | new person', function(hooks) {
setupApplicationTest(hooks);

test('it can create a person via mutation and handles an array response', async function(assert) {
let newFirstName = 'Dave';
let newLastName = 'Jones';
let newAge = 44;

assert.ok(this.server.db.people.length === 0)

await visit(`/person-new`);


let firstNameInput = this.element.querySelector('#person-first-name');
let lastNameInput = this.element.querySelector('#person-last-name');
let ageInput = this.element.querySelector('#person-age');

await fillIn(firstNameInput, newFirstName);
await fillIn(lastNameInput, newLastName);
await fillIn(ageInput, newAge);
await click('.person-save');

let lastName = this.element.querySelector('.person-last-name');

assert.equal(lastName.textContent, newLastName, 'a person with with the correct last name was created');
assert.ok(this.server.db.people.length === 1, 'a new person was persisted to the DB')
});
})
36 changes: 36 additions & 0 deletions tests/dummy/app/controllers/person-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Controller from '@ember/controller';
import mutation from 'dummy/gql/mutations/create-person';
import { contextSet } from 'ember-cli-mirage-graphql/utils';
import { inject as service } from '@ember/service';

function getAttrsFromForm(form) {
let inputNodes = [ ...form.querySelectorAll('input') ];

return inputNodes.reduce((attrs, { name, value }) =>
contextSet(attrs, name, value), {});
}

export default Controller.extend({
apollo: service(),
lastName: '',
firstName: '',
age: null,

actions: {
async savePerson(e) {
e.preventDefault();

let attrs = getAttrsFromForm(e.target)
attrs.age = Number(attrs.age)

let { createPerson } = await this.get('apollo').mutate({
mutation,
variables: {
personAttributes: attrs
}
});

this.transitionToRoute('person', createPerson[0].id);
}
}
});
8 changes: 8 additions & 0 deletions tests/dummy/app/gql/mutations/create-person.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mutation CreatePerson($personAttributes: PersonAttributes!) {
createPerson(personAttributes: $personAttributes) {
id
firstName
lastName
age
}
}
2 changes: 2 additions & 0 deletions tests/dummy/app/gql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type LineItemEdge {

type Mutation {
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person!

createPerson(personAttributes: PersonAttributes!): [Person]
}

interface Node {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Router.map(function() {
this.route('person', { path: '/person/:person_id' }, function() {
this.route('edit');
});
this.route('person-new');
this.route('pets-and-people');
});

Expand Down
24 changes: 24 additions & 0 deletions tests/dummy/app/templates/person-new.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<form onsubmit={{action "savePerson"}}>
<div>
<label for="person-first-name">First Name:</label>
{{input id="person-first-name"
name="firstName"
value=firstName
class="person-last-name"}}
</div>
<div>
<label for="person-last-name">Last Name:</label>
{{input id="person-last-name"
name="lastName"
value=lastName
class="person-last-name"}}
</div>
<div>
<label for="person-age">Age:</label>
{{input id="person-age"
name="age"
value=age
class="person-last-name"}}
</div>
<button type="submit" class="person-save">Save</button>
</form>
4 changes: 3 additions & 1 deletion tests/dummy/mirage/handlers/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const OPTIONS = {
updatePerson: (people, { id, personAttributes }) =>
adaptPersonAttrsFrom(
people.update(id, adaptPersonAttrsTo(personAttributes))
)
),
createPerson: (people, { personAttributes }) => [people.insert(adaptPersonAttrsTo(personAttributes))]

},
argsMap: {
Person: {
Expand Down