Skip to content

Commit

Permalink
Merge schema validation tests with other schema tests (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jan 14, 2019
1 parent 6741ac2 commit 8d7a5fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 87 deletions.
87 changes: 0 additions & 87 deletions src/type/__tests__/definition-test.js
Expand Up @@ -1198,90 +1198,3 @@ describe('Type System: NonNull must only accept non-nullable types', () => {
});
}
});

describe('Type System: A Schema must contain uniquely named types', () => {
it('rejects a Schema which redefines a built-in type', () => {
expect(() => {
const FakeString = new GraphQLScalarType({
name: 'String',
serialize: () => null,
});

const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
normal: { type: GraphQLString },
fake: { type: FakeString },
},
});

return new GraphQLSchema({ query: QueryType });
}).to.throw(
'Schema must contain unique named types but contains multiple types ' +
'named "String".',
);
});

it('rejects a Schema which defines an object type twice', () => {
expect(() => {
const A = new GraphQLObjectType({
name: 'SameName',
fields: { f: { type: GraphQLString } },
});

const B = new GraphQLObjectType({
name: 'SameName',
fields: { f: { type: GraphQLString } },
});

const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
a: { type: A },
b: { type: B },
},
});

return new GraphQLSchema({ query: QueryType });
}).to.throw(
'Schema must contain unique named types but contains multiple types ' +
'named "SameName".',
);
});

it('rejects a Schema which have same named objects implementing an interface', () => {
expect(() => {
const AnotherInterface = new GraphQLInterfaceType({
name: 'AnotherInterface',
fields: { f: { type: GraphQLString } },
});

const FirstBadObject = new GraphQLObjectType({
name: 'BadObject',
interfaces: [AnotherInterface],
fields: { f: { type: GraphQLString } },
});

const SecondBadObject = new GraphQLObjectType({
name: 'BadObject',
interfaces: [AnotherInterface],
fields: { f: { type: GraphQLString } },
});

const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
iface: { type: AnotherInterface },
},
});

return new GraphQLSchema({
query: QueryType,
types: [FirstBadObject, SecondBadObject],
});
}).to.throw(
'Schema must contain unique named types but contains multiple types ' +
'named "BadObject".',
);
});
});
48 changes: 48 additions & 0 deletions src/type/__tests__/schema-test.js
Expand Up @@ -9,6 +9,7 @@

import {
GraphQLSchema,
GraphQLScalarType,
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLString,
Expand Down Expand Up @@ -109,6 +110,53 @@ describe('Type System: Schema', () => {
});
});

describe('A Schema must contain uniquely named types', () => {
it('rejects a Schema which redefines a built-in type', () => {
const FakeString = new GraphQLScalarType({
name: 'String',
serialize: () => null,
});

const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
normal: { type: GraphQLString },
fake: { type: FakeString },
},
});

expect(() => new GraphQLSchema({ query: QueryType })).to.throw(
'Schema must contain unique named types but contains multiple types named "String".',
);
});

it('rejects a Schema which defines an object type twice', () => {
const types = [
new GraphQLObjectType({ name: 'SameName', fields: {} }),
new GraphQLObjectType({ name: 'SameName', fields: {} }),
];

expect(() => new GraphQLSchema({ types })).to.throw(
'Schema must contain unique named types but contains multiple types named "SameName".',
);
});

it('rejects a Schema which defines fields with conflicting types', () => {
const fields = {};
const QueryType = new GraphQLObjectType({
name: 'Query',
fields: {
a: { type: new GraphQLObjectType({ name: 'SameName', fields }) },
b: { type: new GraphQLObjectType({ name: 'SameName', fields }) },
},
});

expect(() => new GraphQLSchema({ query: QueryType })).to.throw(
'Schema must contain unique named types but contains multiple types named "SameName".',
);
});
});

describe('when assumed valid', () => {
it('configures the schema to have no errors', () => {
expect(
Expand Down

0 comments on commit 8d7a5fd

Please sign in to comment.