Skip to content

Commit 9da7d35

Browse files
Merge all tests for standard scalars into one file (#2695)
1 parent 0e88044 commit 9da7d35

File tree

2 files changed

+228
-227
lines changed

2 files changed

+228
-227
lines changed

src/type/__tests__/scalars-test.js

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,77 @@ describe('Type System: Specified scalar types', () => {
110110
'Int cannot represent non-integer value: $var',
111111
);
112112
});
113+
114+
it('serialize', () => {
115+
function serialize(value) {
116+
return GraphQLInt.serialize(value);
117+
}
118+
119+
expect(serialize(1)).to.equal(1);
120+
expect(serialize('123')).to.equal(123);
121+
expect(serialize(0)).to.equal(0);
122+
expect(serialize(-1)).to.equal(-1);
123+
expect(serialize(1e5)).to.equal(100000);
124+
expect(serialize(false)).to.equal(0);
125+
expect(serialize(true)).to.equal(1);
126+
127+
const customValueOfObj = {
128+
value: 5,
129+
valueOf() {
130+
return this.value;
131+
},
132+
};
133+
expect(serialize(customValueOfObj)).to.equal(5);
134+
135+
// The GraphQL specification does not allow serializing non-integer values
136+
// as Int to avoid accidental data loss.
137+
expect(() => serialize(0.1)).to.throw(
138+
'Int cannot represent non-integer value: 0.1',
139+
);
140+
expect(() => serialize(1.1)).to.throw(
141+
'Int cannot represent non-integer value: 1.1',
142+
);
143+
expect(() => serialize(-1.1)).to.throw(
144+
'Int cannot represent non-integer value: -1.1',
145+
);
146+
expect(() => serialize('-1.1')).to.throw(
147+
'Int cannot represent non-integer value: "-1.1"',
148+
);
149+
150+
// Maybe a safe JavaScript int, but bigger than 2^32, so not
151+
// representable as a GraphQL Int
152+
expect(() => serialize(9876504321)).to.throw(
153+
'Int cannot represent non 32-bit signed integer value: 9876504321',
154+
);
155+
expect(() => serialize(-9876504321)).to.throw(
156+
'Int cannot represent non 32-bit signed integer value: -9876504321',
157+
);
158+
159+
// Too big to represent as an Int in JavaScript or GraphQL
160+
expect(() => serialize(1e100)).to.throw(
161+
'Int cannot represent non 32-bit signed integer value: 1e+100',
162+
);
163+
expect(() => serialize(-1e100)).to.throw(
164+
'Int cannot represent non 32-bit signed integer value: -1e+100',
165+
);
166+
expect(() => serialize('one')).to.throw(
167+
'Int cannot represent non-integer value: "one"',
168+
);
169+
170+
// Doesn't represent number
171+
expect(() => serialize('')).to.throw(
172+
'Int cannot represent non-integer value: ""',
173+
);
174+
expect(() => serialize(NaN)).to.throw(
175+
'Int cannot represent non-integer value: NaN',
176+
);
177+
expect(() => serialize(Infinity)).to.throw(
178+
'Int cannot represent non-integer value: Infinity',
179+
);
180+
expect(() => serialize([5])).to.throw(
181+
'Int cannot represent non-integer value: [5]',
182+
);
183+
});
113184
});
114185

115186
describe('GraphQLFloat', () => {
@@ -199,6 +270,47 @@ describe('Type System: Specified scalar types', () => {
199270
'Float cannot represent non numeric value: $var',
200271
);
201272
});
273+
274+
it('serialize', () => {
275+
function serialize(value) {
276+
return GraphQLFloat.serialize(value);
277+
}
278+
279+
expect(serialize(1)).to.equal(1.0);
280+
expect(serialize(0)).to.equal(0.0);
281+
expect(serialize('123.5')).to.equal(123.5);
282+
expect(serialize(-1)).to.equal(-1.0);
283+
expect(serialize(0.1)).to.equal(0.1);
284+
expect(serialize(1.1)).to.equal(1.1);
285+
expect(serialize(-1.1)).to.equal(-1.1);
286+
expect(serialize('-1.1')).to.equal(-1.1);
287+
expect(serialize(false)).to.equal(0.0);
288+
expect(serialize(true)).to.equal(1.0);
289+
290+
const customValueOfObj = {
291+
value: 5.5,
292+
valueOf() {
293+
return this.value;
294+
},
295+
};
296+
expect(serialize(customValueOfObj)).to.equal(5.5);
297+
298+
expect(() => serialize(NaN)).to.throw(
299+
'Float cannot represent non numeric value: NaN',
300+
);
301+
expect(() => serialize(Infinity)).to.throw(
302+
'Float cannot represent non numeric value: Infinity',
303+
);
304+
expect(() => serialize('one')).to.throw(
305+
'Float cannot represent non numeric value: "one"',
306+
);
307+
expect(() => serialize('')).to.throw(
308+
'Float cannot represent non numeric value: ""',
309+
);
310+
expect(() => serialize([5])).to.throw(
311+
'Float cannot represent non numeric value: [5]',
312+
);
313+
});
202314
});
203315

204316
describe('GraphQLString', () => {
@@ -265,6 +377,45 @@ describe('Type System: Specified scalar types', () => {
265377
'String cannot represent a non string value: $var',
266378
);
267379
});
380+
381+
it('serialize', () => {
382+
function serialize(value) {
383+
return GraphQLString.serialize(value);
384+
}
385+
386+
expect(serialize('string')).to.equal('string');
387+
expect(serialize(1)).to.equal('1');
388+
expect(serialize(-1.1)).to.equal('-1.1');
389+
expect(serialize(true)).to.equal('true');
390+
expect(serialize(false)).to.equal('false');
391+
392+
const valueOf = () => 'valueOf string';
393+
const toJSON = () => 'toJSON string';
394+
395+
const valueOfAndToJSONValue = { valueOf, toJSON };
396+
expect(serialize(valueOfAndToJSONValue)).to.equal('valueOf string');
397+
398+
const onlyToJSONValue = { toJSON };
399+
expect(serialize(onlyToJSONValue)).to.equal('toJSON string');
400+
401+
expect(() => serialize(NaN)).to.throw(
402+
'String cannot represent value: NaN',
403+
);
404+
405+
expect(() => serialize([1])).to.throw(
406+
'String cannot represent value: [1]',
407+
);
408+
409+
const badObjValue = {};
410+
expect(() => serialize(badObjValue)).to.throw(
411+
'String cannot represent value: {}',
412+
);
413+
414+
const badValueOfObjValue = { valueOf: 'valueOf string' };
415+
expect(() => serialize(badValueOfObjValue)).to.throw(
416+
'String cannot represent value: { valueOf: "valueOf string" }',
417+
);
418+
});
268419
});
269420

270421
describe('GraphQLBoolean', () => {
@@ -344,6 +495,41 @@ describe('Type System: Specified scalar types', () => {
344495
'Boolean cannot represent a non boolean value: $var',
345496
);
346497
});
498+
499+
it('serialize', () => {
500+
function serialize(value) {
501+
return GraphQLBoolean.serialize(value);
502+
}
503+
504+
expect(serialize(1)).to.equal(true);
505+
expect(serialize(0)).to.equal(false);
506+
expect(serialize(true)).to.equal(true);
507+
expect(serialize(false)).to.equal(false);
508+
expect(
509+
serialize({
510+
value: true,
511+
valueOf() {
512+
return this.value;
513+
},
514+
}),
515+
).to.equal(true);
516+
517+
expect(() => serialize(NaN)).to.throw(
518+
'Boolean cannot represent a non boolean value: NaN',
519+
);
520+
expect(() => serialize('')).to.throw(
521+
'Boolean cannot represent a non boolean value: ""',
522+
);
523+
expect(() => serialize('true')).to.throw(
524+
'Boolean cannot represent a non boolean value: "true"',
525+
);
526+
expect(() => serialize([false])).to.throw(
527+
'Boolean cannot represent a non boolean value: [false]',
528+
);
529+
expect(() => serialize({})).to.throw(
530+
'Boolean cannot represent a non boolean value: {}',
531+
);
532+
});
347533
});
348534

349535
describe('GraphQLID', () => {
@@ -424,5 +610,47 @@ describe('Type System: Specified scalar types', () => {
424610
'ID cannot represent a non-string and non-integer value: $var',
425611
);
426612
});
613+
614+
it('serialize', () => {
615+
function serialize(value) {
616+
return GraphQLID.serialize(value);
617+
}
618+
619+
expect(serialize('string')).to.equal('string');
620+
expect(serialize('false')).to.equal('false');
621+
expect(serialize('')).to.equal('');
622+
expect(serialize(123)).to.equal('123');
623+
expect(serialize(0)).to.equal('0');
624+
expect(serialize(-1)).to.equal('-1');
625+
626+
const valueOf = () => 'valueOf ID';
627+
const toJSON = () => 'toJSON ID';
628+
629+
const valueOfAndToJSONValue = { valueOf, toJSON };
630+
expect(serialize(valueOfAndToJSONValue)).to.equal('valueOf ID');
631+
632+
const onlyToJSONValue = { toJSON };
633+
expect(serialize(onlyToJSONValue)).to.equal('toJSON ID');
634+
635+
const badObjValue = {
636+
_id: false,
637+
valueOf() {
638+
return this._id;
639+
},
640+
};
641+
expect(() => serialize(badObjValue)).to.throw(
642+
'ID cannot represent value: { _id: false, valueOf: [function valueOf] }',
643+
);
644+
645+
expect(() => serialize(true)).to.throw('ID cannot represent value: true');
646+
647+
expect(() => serialize(3.14)).to.throw('ID cannot represent value: 3.14');
648+
649+
expect(() => serialize({})).to.throw('ID cannot represent value: {}');
650+
651+
expect(() => serialize(['abc'])).to.throw(
652+
'ID cannot represent value: ["abc"]',
653+
);
654+
});
427655
});
428656
});

0 commit comments

Comments
 (0)