Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock interfaces #64

Merged
merged 1 commit into from
May 26, 2016
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 src/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
GraphQLObjectType,
GraphQLEnumType,
GraphQLUnionType,
GraphQLInterfaceType,
GraphQLList,
getNullableType,
getNamedType,
Expand Down Expand Up @@ -138,6 +139,10 @@ function addMockFunctionsToSchema({ schema, mocks = {}, preserveResolvers = fals
// it with a default that works with what's below.
return { typename: getRandomElement(fieldType.getTypes()) };
}
if (fieldType instanceof GraphQLInterfaceType) {
const possibleTypes = schema.getPossibleTypes(fieldType);
return { typename: getRandomElement(possibleTypes) };
}
if (fieldType instanceof GraphQLEnumType) {
return getRandomElement(fieldType.getValues()).value;
}
Expand Down
50 changes: 48 additions & 2 deletions test/testMocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ describe('Mock', () => {
const shorthand = `
scalar MissingMockType

type Bird {
interface Flying {
returnInt: Int
}

type Bird implements Flying {
returnInt: Int
returnString: String
returnStringArgument(s: String): String
}

type Bee {
type Bee implements Flying {
returnInt: Int
returnEnum: SomeEnum
}
Expand All @@ -43,6 +47,7 @@ describe('Mock', () => {
returnID: ID
returnEnum: SomeEnum
returnBirdsAndBees: [BirdsAndBees]
returnFlying: [Flying]
returnMockError: MissingMockType
returnNullableString: String
returnNonNullString: String!
Expand Down Expand Up @@ -70,6 +75,11 @@ describe('Mock', () => {
return info.schema.getType(data.typename);
},
},
Flying: {
__resolveType(data, context, info) {
return info.schema.getType(data.typename);
},
},
};

it('throws an error if you forget to pass schema', () => {
Expand Down Expand Up @@ -177,6 +187,42 @@ describe('Mock', () => {
});
});

it('can mock Interfaces', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
addResolveFunctionsToSchema(jsSchema, resolveFunctions);
const mockMap = {
Int: () => 10,
String: () => 'aha',
SomeEnum: () => 'A',
RootQuery: () => ({
returnFlying: () => new MockList(40),
}),
};
addMockFunctionsToSchema({ schema: jsSchema, mocks: mockMap });
const testQuery = `{
returnFlying {
... on Bird {
returnInt
returnString
}
... on Bee {
returnInt
returnEnum
}
}
}`;
return graphql(jsSchema, testQuery).then((res) => {
expect(res.data.returnFlying).to.include({
returnInt: 10,
returnString: 'aha',
});
return expect(res.data.returnFlying).to.include({
returnInt: 10,
returnEnum: 'A',
});
});
});

it('throws an error in resolve if mock type is not defined', () => {
const jsSchema = buildSchemaFromTypeDefinitions(shorthand);
const mockMap = {};
Expand Down