Skip to content

Commit 746ce4d

Browse files
authored
fix root mocks (#6)
1 parent a74abac commit 746ce4d

4 files changed

Lines changed: 35 additions & 22 deletions

File tree

addon/mock/create.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import mockMutationFn from './mutation';
22
import mockQueryFn from './query';
3-
import { contextSet } from '../utils';
3+
import { contextSet, reduceKeys } from '../utils';
4+
5+
const addRootMock = (mocks, rootType, mockFn, db, options) =>
6+
contextSet(mocks, rootType.name, () =>
7+
mockRootType(rootType._fields, mockFn, db, options));
8+
9+
const mockRootType = (fields = {}, mockFn, db, options) =>
10+
reduceKeys(fields, (mocks, field) =>
11+
contextSet(mocks, field, mockFn(db, options)), {});
412

513
export function createMocksForSchema(schema, db, options) {
6-
let { _mutationType, _queryType } = schema;
14+
let mocks = {};
715

8-
return {
9-
RootMutationType: () =>
10-
mockRootType(_mutationType._fields, mockMutationFn, db, options),
11-
RootQueryType: () =>
12-
mockRootType(_queryType._fields, mockQueryFn, db, options)
13-
};
14-
}
16+
addRootMock(mocks, schema._queryType, mockQueryFn, db, options);
17+
addRootMock(mocks, schema._mutationType, mockMutationFn, db, options);
1518

16-
const mockRootType = (rootTypes = {}, mockFn, db, options) =>
17-
Object.keys(rootTypes).reduce((mocks, rootType) =>
18-
contextSet(mocks, rootType, mockFn(db, options)), {});
19+
return mocks;
20+
}

addon/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ export const getTypeFromMeta = (meta, isList) =>
2323
get(meta, isList ? PROP_FOR_LIST_TYPE : PROP_FOR_TYPE);
2424

2525
export const isFunction = (obj) => obj != null && typeof obj === 'function';
26+
27+
export const reduceKeys = (obj, reducerFn, defaultValue) =>
28+
Object.keys(obj).reduce(reducerFn, defaultValue);

tests/dummy/app/gql/schema.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
export default `
2-
schema {
3-
mutation: RootMutationType
4-
query: RootQueryType
5-
}
6-
72
type Address {
83
id: String!
94
line1: String!
@@ -13,6 +8,10 @@ type Address {
138
zip: String!
149
}
1510
11+
type Mutation {
12+
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person
13+
}
14+
1615
type Pet {
1716
id: String!
1817
age: Int!
@@ -36,11 +35,7 @@ input PersonAttributes {
3635
age: Int
3736
}
3837
39-
type RootMutationType {
40-
updatePerson(id: ID!, personAttributes: PersonAttributes!): Person
41-
}
42-
43-
type RootQueryType {
38+
type Query {
4439
person(id: ID!): Person
4540
4641
people(lastName: String, pageSize: Int): [Person]

tests/unit/utils-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { module, test } from 'qunit';
2+
import { reduceKeys } from 'ember-cli-mirage-graphql/utils';
3+
4+
module('Unit | utils', function() {
5+
test('it can reduce object keys by a function', function(assert) {
6+
let obj = { bar: true, baz: true };
7+
let result1 = reduceKeys(obj, (a, b) => `${a}.${b}`, 'foo');
8+
let result2 = reduceKeys(obj, (a, b) => a ? `${a}.${b}` : b);
9+
10+
assert.equal(result1, 'foo.bar.baz', 'It works with default value');
11+
assert.equal(result2, 'bar.baz', 'It works without default value');
12+
});
13+
});

0 commit comments

Comments
 (0)