forked from graphql/graphql-js
/
serialization-test.js
216 lines (194 loc) · 7.23 KB
/
serialization-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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 {
GraphQLInt,
GraphQLID,
GraphQLFloat,
GraphQLString,
GraphQLBoolean,
} from '../';
import { describe, it } from 'mocha';
import { expect } from 'chai';
describe('Type System: Scalar coercion', () => {
it('serializes output as Int', () => {
expect(GraphQLInt.serialize(1)).to.equal(1);
expect(GraphQLInt.serialize('123')).to.equal(123);
expect(GraphQLInt.serialize(0)).to.equal(0);
expect(GraphQLInt.serialize(-1)).to.equal(-1);
expect(GraphQLInt.serialize(1e5)).to.equal(100000);
expect(GraphQLInt.serialize(false)).to.equal(0);
expect(GraphQLInt.serialize(true)).to.equal(1);
// The GraphQL specification does not allow serializing non-integer values
// as Int to avoid accidental data loss.
expect(() => GraphQLInt.serialize(0.1)).to.throw(
'Int cannot represent non-integer value: 0.1',
);
expect(() => GraphQLInt.serialize(1.1)).to.throw(
'Int cannot represent non-integer value: 1.1',
);
expect(() => GraphQLInt.serialize(-1.1)).to.throw(
'Int cannot represent non-integer value: -1.1',
);
expect(() => GraphQLInt.serialize('-1.1')).to.throw(
'Int cannot represent non-integer value: "-1.1"',
);
// Maybe a safe JavaScript int, but bigger than 2^32, so not
// representable as a GraphQL Int
expect(() => GraphQLInt.serialize(9876504321)).to.throw(
'Int cannot represent non 32-bit signed integer value: 9876504321',
);
expect(() => GraphQLInt.serialize(-9876504321)).to.throw(
'Int cannot represent non 32-bit signed integer value: -9876504321',
);
// Too big to represent as an Int in JavaScript or GraphQL
expect(() => GraphQLInt.serialize(1e100)).to.throw(
'Int cannot represent non 32-bit signed integer value: 1e+100',
);
expect(() => GraphQLInt.serialize(-1e100)).to.throw(
'Int cannot represent non 32-bit signed integer value: -1e+100',
);
expect(() => GraphQLInt.serialize('one')).to.throw(
'Int cannot represent non-integer value: "one"',
);
// Doesn't represent number
expect(() => GraphQLInt.serialize('')).to.throw(
'Int cannot represent non-integer value: ""',
);
expect(() => GraphQLInt.serialize(NaN)).to.throw(
'Int cannot represent non-integer value: NaN',
);
expect(() => GraphQLInt.serialize(Infinity)).to.throw(
'Int cannot represent non-integer value: Infinity',
);
expect(() => GraphQLInt.serialize([5])).to.throw(
'Int cannot represent non-integer value: [5]',
);
});
it('serializes output as Float', () => {
expect(GraphQLFloat.serialize(1)).to.equal(1.0);
expect(GraphQLFloat.serialize(0)).to.equal(0.0);
expect(GraphQLFloat.serialize('123.5')).to.equal(123.5);
expect(GraphQLFloat.serialize(-1)).to.equal(-1.0);
expect(GraphQLFloat.serialize(0.1)).to.equal(0.1);
expect(GraphQLFloat.serialize(1.1)).to.equal(1.1);
expect(GraphQLFloat.serialize(-1.1)).to.equal(-1.1);
expect(GraphQLFloat.serialize('-1.1')).to.equal(-1.1);
expect(GraphQLFloat.serialize(false)).to.equal(0.0);
expect(GraphQLFloat.serialize(true)).to.equal(1.0);
expect(() => GraphQLFloat.serialize(NaN)).to.throw(
'Float cannot represent non numeric value: NaN',
);
expect(() => GraphQLFloat.serialize(Infinity)).to.throw(
'Float cannot represent non numeric value: Infinity',
);
expect(() => GraphQLFloat.serialize('one')).to.throw(
'Float cannot represent non numeric value: "one"',
);
expect(() => GraphQLFloat.serialize('')).to.throw(
'Float cannot represent non numeric value: ""',
);
expect(() => GraphQLFloat.serialize([5])).to.throw(
'Float cannot represent non numeric value: [5]',
);
});
it('serializes output as String', () => {
expect(GraphQLString.serialize('string')).to.equal('string');
expect(GraphQLString.serialize(1)).to.equal('1');
expect(GraphQLString.serialize(-1.1)).to.equal('-1.1');
expect(GraphQLString.serialize(true)).to.equal('true');
expect(GraphQLString.serialize(false)).to.equal('false');
const stringableObjValue = {
valueOf() {
return 'valueOf string';
},
toJSON() {
return 'toJSON string';
},
};
expect(GraphQLString.serialize(stringableObjValue)).to.equal(
'valueOf string',
);
delete stringableObjValue.valueOf;
expect(GraphQLString.serialize(stringableObjValue)).to.equal(
'toJSON string',
);
expect(() => GraphQLString.serialize(NaN)).to.throw(
'String cannot represent value: NaN',
);
expect(() => GraphQLString.serialize([1])).to.throw(
'String cannot represent value: [1]',
);
const badObjValue = {};
expect(() => GraphQLString.serialize(badObjValue)).to.throw(
'String cannot represent value: {}',
);
});
it('serializes output as Boolean', () => {
expect(GraphQLBoolean.serialize(1)).to.equal(true);
expect(GraphQLBoolean.serialize(0)).to.equal(false);
expect(GraphQLBoolean.serialize(true)).to.equal(true);
expect(GraphQLBoolean.serialize(false)).to.equal(false);
expect(() => GraphQLBoolean.serialize(NaN)).to.throw(
'Boolean cannot represent a non boolean value: NaN',
);
expect(() => GraphQLBoolean.serialize('')).to.throw(
'Boolean cannot represent a non boolean value: ""',
);
expect(() => GraphQLBoolean.serialize('true')).to.throw(
'Boolean cannot represent a non boolean value: "true"',
);
expect(() => GraphQLBoolean.serialize([false])).to.throw(
'Boolean cannot represent a non boolean value: [false]',
);
expect(() => GraphQLBoolean.serialize({})).to.throw(
'Boolean cannot represent a non boolean value: {}',
);
});
it('serializes output as ID', () => {
expect(GraphQLID.serialize('string')).to.equal('string');
expect(GraphQLID.serialize('false')).to.equal('false');
expect(GraphQLID.serialize('')).to.equal('');
expect(GraphQLID.serialize(123)).to.equal('123');
expect(GraphQLID.serialize(0)).to.equal('0');
expect(GraphQLID.serialize(-1)).to.equal('-1');
const serializableObjValue = {
_id: 123,
valueOf() {
return this._id;
},
toJSON() {
return `ID:${this._id}`;
},
};
expect(GraphQLID.serialize(serializableObjValue)).to.equal('123');
delete serializableObjValue.valueOf;
expect(GraphQLID.serialize(serializableObjValue)).to.equal('ID:123');
const badObjValue = {
_id: false,
valueOf() {
return this._id;
},
};
expect(() => GraphQLID.serialize(badObjValue)).to.throw(
'ID cannot represent value: { _id: false, valueOf: [function valueOf] }',
);
expect(() => GraphQLID.serialize(true)).to.throw(
'ID cannot represent value: true',
);
expect(() => GraphQLID.serialize(3.14)).to.throw(
'ID cannot represent value: 3.14',
);
expect(() => GraphQLID.serialize({})).to.throw(
'ID cannot represent value: {}',
);
expect(() => GraphQLID.serialize(['abc'])).to.throw(
'ID cannot represent value: ["abc"]',
);
});
});