@@ -2,6 +2,15 @@ import { expect } from 'chai';
2
2
import { describe , it } from 'mocha' ;
3
3
4
4
import { instanceOf } from '../instanceOf' ;
5
+ import { SYMBOL_TO_STRING_TAG } from '../../polyfills/symbols' ;
6
+ import {
7
+ GraphQLScalarType ,
8
+ GraphQLObjectType ,
9
+ GraphQLInterfaceType ,
10
+ GraphQLUnionType ,
11
+ GraphQLEnumType ,
12
+ GraphQLInputObjectType ,
13
+ } from '../../type/definition' ;
5
14
6
15
describe ( 'instanceOf' , ( ) => {
7
16
it ( 'fails with descriptive error message' , ( ) => {
@@ -19,4 +28,106 @@ describe('instanceOf', () => {
19
28
/ ^ C a n n o t u s e F o o " \[ o b j e c t O b j e c t \] " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
20
29
) ;
21
30
} ) ;
31
+
32
+ describe ( 'Symbol.toStringTag' , ( ) => {
33
+ function checkSameNameClasses ( getClass ) {
34
+ const Class1 = getClass ( 'FirstClass' ) ;
35
+ const Class2 = getClass ( 'SecondClass' ) ;
36
+
37
+ expect ( Class1 . name ) . to . equal ( 'Foo' ) ;
38
+ expect ( Class2 . name ) . to . equal ( 'Foo' ) ;
39
+
40
+ expect ( instanceOf ( null , Class1 ) ) . to . equal ( false ) ;
41
+ expect ( instanceOf ( null , Class2 ) ) . to . equal ( false ) ;
42
+
43
+ const c1 = new Class1 ( ) ;
44
+ const c2 = new Class2 ( ) ;
45
+
46
+ expect ( getTag ( c1 ) ) . to . equal ( 'FirstClass' ) ;
47
+ expect ( getTag ( c2 ) ) . to . equal ( 'SecondClass' ) ;
48
+
49
+ // In these Symbol.toStringTag tests, instanceOf returns the
50
+ // expected boolean value without throwing an error, because even
51
+ // though Class1.name === Class2.name, the Symbol.toStringTag
52
+ // strings of the two classes are different.
53
+ expect ( instanceOf ( c1 , Class1 ) ) . to . equal ( true ) ;
54
+ expect ( instanceOf ( c1 , Class2 ) ) . to . equal ( false ) ;
55
+ expect ( instanceOf ( c2 , Class1 ) ) . to . equal ( false ) ;
56
+ expect ( instanceOf ( c2 , Class2 ) ) . to . equal ( true ) ;
57
+ }
58
+
59
+ function getTag ( from : any ) : string {
60
+ return from [ SYMBOL_TO_STRING_TAG ] ;
61
+ }
62
+
63
+ it ( 'does not fail if dynamically-defined tags differ' , ( ) => {
64
+ checkSameNameClasses ( ( tag ) => {
65
+ class Foo { }
66
+ Object . defineProperty ( Foo . prototype , SYMBOL_TO_STRING_TAG , {
67
+ value : tag ,
68
+ } ) ;
69
+ return Foo ;
70
+ } ) ;
71
+ } ) ;
72
+
73
+ it ( 'does not fail if dynamically-defined tag getters differ' , ( ) => {
74
+ checkSameNameClasses ( ( tag ) => {
75
+ class Foo { }
76
+ Object . defineProperty ( Foo . prototype , SYMBOL_TO_STRING_TAG , {
77
+ get ( ) {
78
+ return tag ;
79
+ } ,
80
+ } ) ;
81
+ return Foo ;
82
+ } ) ;
83
+ } ) ;
84
+
85
+ it ( 'does not fail for anonymous classes' , ( ) => {
86
+ checkSameNameClasses ( ( tag ) => {
87
+ const Foo = class { } ;
88
+ Object . defineProperty ( Foo . prototype , SYMBOL_TO_STRING_TAG , {
89
+ get ( ) {
90
+ return tag ;
91
+ } ,
92
+ } ) ;
93
+ return Foo ;
94
+ } ) ;
95
+ } ) ;
96
+
97
+ it ( 'does not fail if prototype property tags differ' , ( ) => {
98
+ checkSameNameClasses ( ( tag ) => {
99
+ class Foo { }
100
+ ( Foo . prototype : any ) [ SYMBOL_TO_STRING_TAG ] = tag ;
101
+ return Foo ;
102
+ } ) ;
103
+ } ) ;
104
+
105
+ it ( 'does not fail if computed getter tags differ' , ( ) => {
106
+ checkSameNameClasses ( ( tag ) => {
107
+ class Foo {
108
+ // $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
109
+ get [ SYMBOL_TO_STRING_TAG ] ( ) {
110
+ return tag ;
111
+ }
112
+ }
113
+ return Foo ;
114
+ } ) ;
115
+ } ) ;
116
+
117
+ it ( 'is defined for various GraphQL*Type classes' , ( ) => {
118
+ function checkGraphQLType ( constructor , expectedName ) {
119
+ expect ( getTag ( constructor . prototype ) ) . to . equal ( expectedName ) ;
120
+ const instance = Object . create ( constructor . prototype ) ;
121
+ expect ( getTag ( instance ) ) . to . equal ( expectedName ) ;
122
+ expect ( instanceOf ( instance , constructor ) ) . to . equal ( true ) ;
123
+ }
124
+
125
+ checkGraphQLType ( GraphQLScalarType , 'GraphQLScalarType' ) ;
126
+ checkGraphQLType ( GraphQLObjectType , 'GraphQLObjectType' ) ;
127
+ checkGraphQLType ( GraphQLInterfaceType , 'GraphQLInterfaceType' ) ;
128
+ checkGraphQLType ( GraphQLUnionType , 'GraphQLUnionType' ) ;
129
+ checkGraphQLType ( GraphQLEnumType , 'GraphQLEnumType' ) ;
130
+ checkGraphQLType ( GraphQLInputObjectType , 'GraphQLInputObjectType' ) ;
131
+ } ) ;
132
+ } ) ;
22
133
} ) ;
0 commit comments