Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Dec 15, 2016
1 parent 655ead9 commit 86625ac
Showing 1 changed file with 151 additions and 0 deletions.
151 changes: 151 additions & 0 deletions src/execution/__tests__/abstract-promise-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,79 @@ describe('Execute: Handles execution of abstract types with promises', () => {
});
});

it('isTypeOf can be rejected', async () => {
const PetType = new GraphQLInterfaceType({
name: 'Pet',
fields: {
name: { type: GraphQLString }
}
});

const DogType = new GraphQLObjectType({
name: 'Dog',
interfaces: [ PetType ],
isTypeOf: () => Promise.reject(new Error('We are testing this error')),
fields: {
name: { type: GraphQLString },
woofs: { type: GraphQLBoolean },
}
});

const CatType = new GraphQLObjectType({
name: 'Cat',
interfaces: [ PetType ],
isTypeOf: obj => Promise.resolve(obj instanceof Cat),
fields: {
name: { type: GraphQLString },
meows: { type: GraphQLBoolean },
}
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
pets: {
type: new GraphQLList(PetType),
resolve() {
return [ new Dog('Odie', true), new Cat('Garfield', false) ];
}
}
}
}),
types: [ CatType, DogType ]
});

const query = `{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}`;

const result = await graphql(schema, query);

expect(result).to.deep.equal({
data: {
pets: [
null,
{ name: 'Garfield',
meows: false } ] },
errors: [
{
message: 'We are testing this error',
locations: [ { line: 2, column: 7 } ],
path: [ 'pets', 0 ]
}
]
});
});

it('isTypeOf used to resolve runtime type for Union', async () => {
const DogType = new GraphQLObjectType({
name: 'Dog',
Expand Down Expand Up @@ -430,4 +503,82 @@ describe('Execute: Handles execution of abstract types with promises', () => {
});
});

it('resolveType can be caught', async () => {
const PetType = new GraphQLInterfaceType({
name: 'Pet',
resolveType: () => Promise.reject('We are testing this error'),
fields: {
name: { type: GraphQLString }
}
});

const DogType = new GraphQLObjectType({
name: 'Dog',
interfaces: [ PetType ],
fields: {
name: { type: GraphQLString },
woofs: { type: GraphQLBoolean },
}
});

const CatType = new GraphQLObjectType({
name: 'Cat',
interfaces: [ PetType ],
fields: {
name: { type: GraphQLString },
meows: { type: GraphQLBoolean },
}
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
pets: {
type: new GraphQLList(PetType),
resolve() {
return [
new Dog('Odie', true),
new Cat('Garfield', false)
];
}
}
}
}),
types: [ CatType, DogType ]
});

const query = `{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}`;

const result = await graphql(schema, query);

expect(result).to.jsonEqual({
data: {
pets: [ null, null ]
},
errors: [
{
message: 'We are testing this error',
locations: [ { line: 2, column: 7 } ],
path: [ 'pets', 0 ]
},
{
message: 'We are testing this error',
locations: [ { line: 2, column: 7 } ],
path: [ 'pets', 1 ]
}
]
});
});

});

0 comments on commit 86625ac

Please sign in to comment.