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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ You may pass in options when creating a request handler. The options take the fo
Person: { // arguments are mapped on a per-type basis
pageSize: (records, _, pageSize) => records.slice(0, pageSize)
}
},
/*
`scalarMocks` is used if you have custom scalars and you need to mock them to
return a default value
*/
scalarMocks: {
MyCustomScalar: () => {
return 'some custom value'
}
}
}
```
Expand Down
4 changes: 4 additions & 0 deletions addon/mocks/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const composeCreateMocksForSchema =

let mocks = createMocks(typesAndMockFns, db, options);

if (options && options.scalarMocks) {
mocks = Object.assign({}, mocks, options.scalarMocks)
}

return mocks;
};

Expand Down
2 changes: 2 additions & 0 deletions tests/acceptance/person-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ module('Acceptance | person', function(hooks) {
let city = element.querySelector('.person-address-city').textContent;
let state = element.querySelector('.person-address-state').textContent;
let zip = element.querySelector('.person-address-zip').textContent;
let createdAt = element.querySelector('.person-created-at').textContent;

assert.equal(line1, address.line1, 'It displays address line 1');
assert.equal(line2, address.line2, 'It displays address line 2');
assert.equal(city, address.city, 'It displays address city');
assert.equal(state, address.state, 'It displays address state');
assert.equal(zip, address.zip, 'It displays address zip');
assert.equal(createdAt, person.createdAt, 'It displays person created at');
}

test('it can display belongsTo related data 1', async function(assert) {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/gql/queries/person.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ query($id: ID!) {
name
weight
}
createdAt
}
}
3 changes: 3 additions & 0 deletions tests/dummy/app/gql/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
scalar Date

type Address {
id: ID!
line1: String!
Expand Down Expand Up @@ -81,6 +83,7 @@ type Person {
firstName: String!
lastName: String!
pets(name: String): [Pet]
createdAt: Date!
}

input PersonAttributes {
Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/templates/person/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<span class="person-address-state">{{model.human.address.state}}</span>
<span class="person-address-zip">{{model.human.address.zip}}</span>
</div>
<div class="person-created-at">{{model.human.createdAt}}</div>
{{#if model.human.pets}}
<table class="person-pets">
<thead>
Expand Down
3 changes: 2 additions & 1 deletion tests/dummy/mirage/factories/person.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const { list, name } = faker;
export default Factory.extend({
age: (i) => list.random(1, 10, 20, 30, 40, 50, 60, 70, 80, 90)(i),
firstName: name.firstName,
surname: name.lastName
surname: name.lastName,
createdAt: '2019-01-01'
});
5 changes: 5 additions & 0 deletions tests/dummy/mirage/handlers/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ const OPTIONS = {
pageSize: (people, variableName, pageSize) => people.slice(0, pageSize),
lastName: 'surname'
}
},
scalarMocks: {
Date: () => {
return '2019-01-01'
}
}
};

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/mocks/scalars-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import createMocksForSchema from
'ember-cli-mirage-graphql/mocks/create';
import { module, test } from 'qunit';

module('Unit | Mocks | scalars', function() {
module('mock', function() {

test('creates scalars from options', function(assert) {
let schema = { _mutationType: {}, _queryType: {} };
let db = {};
let fieldName = 'foo';
let value = 'custom value'
let scalarMock = () => value;
let options = {
scalarMocks: { [fieldName]: scalarMock }
};
const mocks = createMocksForSchema(schema, db, options);

assert.equal(typeof mocks[fieldName], 'function', 'mock scalar set on mocks');
assert.equal(mocks[fieldName](), value, 'custom scalar returns proper value');
});
});
});