From 2fcd55eb88a0a9ed560ad94b8c2f072bf9167f40 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 10 Feb 2019 21:20:00 +0200 Subject: [PATCH] Move 'Object.toString' test to the appropriate files (#1723) Move 'Object.toString' tests to the appropriate files --- src/language/__tests__/source-test.js | 20 +++++ src/type/__tests__/definition-test.js | 81 ++++++++++------- src/type/__tests__/directive-test.js | 5 +- src/type/__tests__/schema-test.js | 8 ++ src/type/__tests__/toStringTag-test.js | 118 ------------------------- 5 files changed, 81 insertions(+), 151 deletions(-) create mode 100644 src/language/__tests__/source-test.js delete mode 100644 src/type/__tests__/toStringTag-test.js diff --git a/src/language/__tests__/source-test.js b/src/language/__tests__/source-test.js new file mode 100644 index 0000000000..0cb3b057d6 --- /dev/null +++ b/src/language/__tests__/source-test.js @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + */ + +import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { Source } from '../source'; + +describe('Source', () => { + it('can be Object.toStringified', () => { + const source = new Source(''); + + expect(Object.prototype.toString.call(source)).to.equal('[object Source]'); + }); +}); diff --git a/src/type/__tests__/definition-test.js b/src/type/__tests__/definition-test.js index db0934253e..f42e1faf15 100644 --- a/src/type/__tests__/definition-test.js +++ b/src/type/__tests__/definition-test.js @@ -122,38 +122,6 @@ describe('Type System: Example', () => { }); }); - it('stringifies simple types', () => { - expect(String(ScalarType)).to.equal('Scalar'); - expect(String(ObjectType)).to.equal('Object'); - expect(String(InterfaceType)).to.equal('Interface'); - expect(String(UnionType)).to.equal('Union'); - expect(String(EnumType)).to.equal('Enum'); - expect(String(InputObjectType)).to.equal('InputObject'); - - expect(String(NonNullScalarType)).to.equal('Scalar!'); - expect(String(ListOfScalarsType)).to.equal('[Scalar]'); - expect(String(NonNullListofScalars)).to.equal('[Scalar]!'); - expect(String(ListOfNonNullScalarsType)).to.equal('[Scalar!]'); - expect(String(GraphQLList(ListOfScalarsType))).to.equal('[[Scalar]]'); - }); - - it('JSON stringifies simple types', () => { - expect(JSON.stringify(ScalarType)).to.equal('"Scalar"'); - expect(JSON.stringify(ObjectType)).to.equal('"Object"'); - expect(JSON.stringify(InterfaceType)).to.equal('"Interface"'); - expect(JSON.stringify(UnionType)).to.equal('"Union"'); - expect(JSON.stringify(EnumType)).to.equal('"Enum"'); - expect(JSON.stringify(InputObjectType)).to.equal('"InputObject"'); - - expect(JSON.stringify(NonNullScalarType)).to.equal('"Scalar!"'); - expect(JSON.stringify(ListOfScalarsType)).to.equal('"[Scalar]"'); - expect(JSON.stringify(NonNullListofScalars)).to.equal('"[Scalar]!"'); - expect(JSON.stringify(ListOfNonNullScalarsType)).to.equal('"[Scalar!]"'); - expect(JSON.stringify(GraphQLList(ListOfScalarsType))).to.equal( - '"[[Scalar]]"', - ); - }); - it('allows a thunk for Union member types', () => { const union = new GraphQLUnionType({ name: 'ThunkUnion', @@ -939,3 +907,52 @@ describe('Type System: NonNull must only accept non-nullable types', () => { ); }); }); + +describe('Type System: test utility methods', () => { + it('stringifies types', () => { + expect(String(ScalarType)).to.equal('Scalar'); + expect(String(ObjectType)).to.equal('Object'); + expect(String(InterfaceType)).to.equal('Interface'); + expect(String(UnionType)).to.equal('Union'); + expect(String(EnumType)).to.equal('Enum'); + expect(String(InputObjectType)).to.equal('InputObject'); + + expect(String(NonNullScalarType)).to.equal('Scalar!'); + expect(String(ListOfScalarsType)).to.equal('[Scalar]'); + expect(String(NonNullListofScalars)).to.equal('[Scalar]!'); + expect(String(ListOfNonNullScalarsType)).to.equal('[Scalar!]'); + expect(String(GraphQLList(ListOfScalarsType))).to.equal('[[Scalar]]'); + }); + + it('JSON.stringifies types', () => { + expect(JSON.stringify(ScalarType)).to.equal('"Scalar"'); + expect(JSON.stringify(ObjectType)).to.equal('"Object"'); + expect(JSON.stringify(InterfaceType)).to.equal('"Interface"'); + expect(JSON.stringify(UnionType)).to.equal('"Union"'); + expect(JSON.stringify(EnumType)).to.equal('"Enum"'); + expect(JSON.stringify(InputObjectType)).to.equal('"InputObject"'); + + expect(JSON.stringify(NonNullScalarType)).to.equal('"Scalar!"'); + expect(JSON.stringify(ListOfScalarsType)).to.equal('"[Scalar]"'); + expect(JSON.stringify(NonNullListofScalars)).to.equal('"[Scalar]!"'); + expect(JSON.stringify(ListOfNonNullScalarsType)).to.equal('"[Scalar!]"'); + expect(JSON.stringify(GraphQLList(ListOfScalarsType))).to.equal( + '"[[Scalar]]"', + ); + }); + + it('Object.toStringifies types', () => { + function toString(obj) { + return Object.prototype.toString.call(obj); + } + + expect(toString(ScalarType)).to.equal('[object GraphQLScalarType]'); + expect(toString(ObjectType)).to.equal('[object GraphQLObjectType]'); + expect(toString(InterfaceType)).to.equal('[object GraphQLInterfaceType]'); + expect(toString(UnionType)).to.equal('[object GraphQLUnionType]'); + expect(toString(EnumType)).to.equal('[object GraphQLEnumType]'); + expect(toString(InputObjectType)).to.equal( + '[object GraphQLInputObjectType]', + ); + }); +}); diff --git a/src/type/__tests__/directive-test.js b/src/type/__tests__/directive-test.js index 54233e2160..c93680a000 100644 --- a/src/type/__tests__/directive-test.js +++ b/src/type/__tests__/directive-test.js @@ -58,7 +58,7 @@ describe('Type System: Directive', () => { }); }); - it('can be stringified and JSON.stringified', () => { + it('can be stringified, JSON.stringified and Object.toStingified', () => { const directive = new GraphQLDirective({ name: 'Foo', locations: ['QUERY'], @@ -66,6 +66,9 @@ describe('Type System: Directive', () => { expect(String(directive)).to.equal('@Foo'); expect(JSON.stringify(directive)).to.equal('"@Foo"'); + expect(Object.prototype.toString.call(directive)).to.equal( + '[object GraphQLDirective]', + ); }); it('reject an unnamed directive', () => { diff --git a/src/type/__tests__/schema-test.js b/src/type/__tests__/schema-test.js index ebae62f433..cd5c7e960c 100644 --- a/src/type/__tests__/schema-test.js +++ b/src/type/__tests__/schema-test.js @@ -258,6 +258,14 @@ describe('Type System: Schema', () => { }); }); + it('can be Object.toStringified', () => { + const schema = new GraphQLSchema({}); + + expect(Object.prototype.toString.call(schema)).to.equal( + '[object GraphQLSchema]', + ); + }); + describe('Validity', () => { describe('when not assumed valid', () => { it('configures the schema to still needing validation', () => { diff --git a/src/type/__tests__/toStringTag-test.js b/src/type/__tests__/toStringTag-test.js deleted file mode 100644 index e98f249bfd..0000000000 --- a/src/type/__tests__/toStringTag-test.js +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Copyright (c) 2018-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict - */ - -import { describe, it } from 'mocha'; -import { expect } from 'chai'; -import { - GraphQLDirective, - GraphQLEnumType, - GraphQLInputObjectType, - GraphQLInterfaceType, - GraphQLObjectType, - GraphQLScalarType, - GraphQLSchema, - GraphQLUnionType, - Source, -} from '../../'; - -describe('Check to see if Symbol.toStringTag is defined on types', () => { - function hasSymbol(obj) { - return Object.getOwnPropertySymbols(obj).includes(Symbol.toStringTag); - } - - it('GraphQLDirective should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLDirective.prototype)).to.equal(true); - }); - - it('GraphQLEnumType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLEnumType.prototype)).to.equal(true); - }); - - it('GraphQLInputObjectType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLInputObjectType.prototype)).to.equal(true); - }); - - it('GraphQLInterfaceType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLInterfaceType.prototype)).to.equal(true); - }); - - it('GraphQLObjectType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLObjectType.prototype)).to.equal(true); - }); - - it('GraphQLScalarType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLScalarType.prototype)).to.equal(true); - }); - - it('GraphQLSchema should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLSchema.prototype)).to.equal(true); - }); - - it('GraphQLUnionType should have Symbol.toStringTag', () => { - expect(hasSymbol(GraphQLUnionType.prototype)).to.equal(true); - }); - - it('Source should have Symbol.toStringTag', () => { - expect(hasSymbol(Source.prototype)).to.equal(true); - }); -}); - -describe('Check to see if Symbol.toStringTag tests on instances', () => { - function typeOf(object) { - return Object.prototype.toString - .call(object) - .replace(/^\[object /, '') - .replace(/]$/, ''); - } - - it('should return the class name for GraphQLSchema instance', () => { - const obj = Object.create(GraphQLSchema.prototype); - expect(typeOf(obj)).to.equal(GraphQLSchema.name); - }); - - it('should return the class name for GraphQLScalarType instance', () => { - const obj = Object.create(GraphQLScalarType.prototype); - expect(typeOf(obj)).to.equal(GraphQLScalarType.name); - }); - - it('should return the class name for GraphQLObjectType instance', () => { - const obj = Object.create(GraphQLObjectType.prototype); - expect(typeOf(obj)).to.equal(GraphQLObjectType.name); - }); - - it('should return the class name for GraphQLInterfaceType instance', () => { - const obj = Object.create(GraphQLInterfaceType.prototype); - expect(typeOf(obj)).to.equal(GraphQLInterfaceType.name); - }); - - it('should return the class name for GraphQLUnionType instance', () => { - const obj = Object.create(GraphQLUnionType.prototype); - expect(typeOf(obj)).to.equal(GraphQLUnionType.name); - }); - - it('should return the class name for GraphQLEnumType instance', () => { - const obj = Object.create(GraphQLEnumType.prototype); - expect(typeOf(obj)).to.equal(GraphQLEnumType.name); - }); - - it('should return the class name for GraphQLInputObjectType instance', () => { - const obj = Object.create(GraphQLInputObjectType.prototype); - expect(typeOf(obj)).to.equal(GraphQLInputObjectType.name); - }); - - it('should return the class name for GraphQLDirective instance', () => { - const obj = Object.create(GraphQLDirective.prototype); - expect(typeOf(obj)).to.equal(GraphQLDirective.name); - }); - - it('should return the class name for Source instance', () => { - const obj = Object.create(Source.prototype); - expect(typeOf(obj)).to.equal(Source.name); - }); -});