This repository has been archived by the owner on May 26, 2023. It is now read-only.
forked from graphql/graphql-js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
instanceOf-test.js
133 lines (116 loc) · 4.09 KB
/
instanceOf-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { instanceOf } from '../instanceOf';
import { SYMBOL_TO_STRING_TAG } from '../../polyfills/symbols';
import {
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
} from '../../type/definition';
describe('instanceOf', () => {
it('fails with descriptive error message', () => {
function getFoo() {
class Foo {}
return Foo;
}
const Foo1 = getFoo();
const Foo2 = getFoo();
expect(() => instanceOf(new Foo1(), Foo2)).to.throw(
/^Cannot use Foo "\[object Object\]" from another module or realm./m,
);
expect(() => instanceOf(new Foo2(), Foo1)).to.throw(
/^Cannot use Foo "\[object Object\]" from another module or realm./m,
);
});
describe('Symbol.toStringTag', () => {
function checkSameNameClasses(getClass) {
const Class1 = getClass('FirstClass');
const Class2 = getClass('SecondClass');
expect(Class1.name).to.equal('Foo');
expect(Class2.name).to.equal('Foo');
expect(instanceOf(null, Class1)).to.equal(false);
expect(instanceOf(null, Class2)).to.equal(false);
const c1 = new Class1();
const c2 = new Class2();
expect(getTag(c1)).to.equal('FirstClass');
expect(getTag(c2)).to.equal('SecondClass');
// In these Symbol.toStringTag tests, instanceOf returns the
// expected boolean value without throwing an error, because even
// though Class1.name === Class2.name, the Symbol.toStringTag
// strings of the two classes are different.
expect(instanceOf(c1, Class1)).to.equal(true);
expect(instanceOf(c1, Class2)).to.equal(false);
expect(instanceOf(c2, Class1)).to.equal(false);
expect(instanceOf(c2, Class2)).to.equal(true);
}
function getTag(from: any): string {
return from[SYMBOL_TO_STRING_TAG];
}
it('does not fail if dynamically-defined tags differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
value: tag,
});
return Foo;
});
});
it('does not fail if dynamically-defined tag getters differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
get() {
return tag;
},
});
return Foo;
});
});
it('does not fail for anonymous classes', () => {
checkSameNameClasses((tag) => {
const Foo = class {};
Object.defineProperty(Foo.prototype, SYMBOL_TO_STRING_TAG, {
get() {
return tag;
},
});
return Foo;
});
});
it('does not fail if prototype property tags differ', () => {
checkSameNameClasses((tag) => {
class Foo {}
(Foo.prototype: any)[SYMBOL_TO_STRING_TAG] = tag;
return Foo;
});
});
it('does not fail if computed getter tags differ', () => {
checkSameNameClasses((tag) => {
class Foo {
// $FlowFixMe[unsupported-syntax] Flow doesn't support computed properties yet
get [SYMBOL_TO_STRING_TAG]() {
return tag;
}
}
return Foo;
});
});
it('is defined for various GraphQL*Type classes', () => {
function checkGraphQLType(constructor, expectedName) {
expect(getTag(constructor.prototype)).to.equal(expectedName);
const instance = Object.create(constructor.prototype);
expect(getTag(instance)).to.equal(expectedName);
expect(instanceOf(instance, constructor)).to.equal(true);
}
checkGraphQLType(GraphQLScalarType, 'GraphQLScalarType');
checkGraphQLType(GraphQLObjectType, 'GraphQLObjectType');
checkGraphQLType(GraphQLInterfaceType, 'GraphQLInterfaceType');
checkGraphQLType(GraphQLUnionType, 'GraphQLUnionType');
checkGraphQLType(GraphQLEnumType, 'GraphQLEnumType');
checkGraphQLType(GraphQLInputObjectType, 'GraphQLInputObjectType');
});
});
});