Skip to content

Commit 2a66440

Browse files
Add tests for parseValue & parseLiteral of std scalars (#2042)
1 parent adb5292 commit 2a66440

File tree

1 file changed

+337
-0
lines changed

1 file changed

+337
-0
lines changed

src/type/__tests__/scalars-test.js

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
// @flow strict
2+
3+
import { describe, it } from 'mocha';
4+
import { expect } from 'chai';
5+
6+
import {
7+
GraphQLID,
8+
GraphQLInt,
9+
GraphQLFloat,
10+
GraphQLString,
11+
GraphQLBoolean,
12+
} from '../scalars';
13+
import { parseValue as parseValueToAST } from '../../language/parser';
14+
15+
describe('Type System: Specified scalar types', () => {
16+
describe('GraphQLInt', () => {
17+
it('parseValue', () => {
18+
function parseValue(value) {
19+
return GraphQLInt.parseValue(value);
20+
}
21+
22+
expect(parseValue(1)).to.equal(1);
23+
expect(parseValue(0)).to.equal(0);
24+
expect(parseValue(-1)).to.equal(-1);
25+
26+
expect(() => parseValue(9876504321)).to.throw(
27+
'Int cannot represent non 32-bit signed integer value: 9876504321',
28+
);
29+
expect(() => parseValue(-9876504321)).to.throw(
30+
'Int cannot represent non 32-bit signed integer value: -9876504321',
31+
);
32+
expect(() => parseValue(0.1)).to.throw(
33+
'Int cannot represent non-integer value: 0.1',
34+
);
35+
expect(() => parseValue(NaN)).to.throw(
36+
'Int cannot represent non-integer value: NaN',
37+
);
38+
expect(() => parseValue(Infinity)).to.throw(
39+
'Int cannot represent non-integer value: Infinity',
40+
);
41+
42+
expect(() => parseValue(undefined)).to.throw(
43+
'Int cannot represent non-integer value: undefined',
44+
);
45+
expect(() => parseValue(null)).to.throw(
46+
'Int cannot represent non-integer value: null',
47+
);
48+
expect(() => parseValue('')).to.throw(
49+
'Int cannot represent non-integer value: ""',
50+
);
51+
expect(() => parseValue('123')).to.throw(
52+
'Int cannot represent non-integer value: "123"',
53+
);
54+
expect(() => parseValue(false)).to.throw(
55+
'Int cannot represent non-integer value: false',
56+
);
57+
expect(() => parseValue(true)).to.throw(
58+
'Int cannot represent non-integer value: true',
59+
);
60+
expect(() => parseValue([1])).to.throw(
61+
'Int cannot represent non-integer value: [1]',
62+
);
63+
expect(() => parseValue({ value: 1 })).to.throw(
64+
'Int cannot represent non-integer value: { value: 1 }',
65+
);
66+
});
67+
68+
it('parseLiteral', () => {
69+
function parseLiteral(str) {
70+
return GraphQLInt.parseLiteral(parseValueToAST(str));
71+
}
72+
73+
expect(parseLiteral('1')).to.equal(1);
74+
expect(parseLiteral('0')).to.equal(0);
75+
expect(parseLiteral('-1')).to.equal(-1);
76+
77+
expect(parseLiteral('9876504321')).to.equal(undefined);
78+
expect(parseLiteral('-9876504321')).to.equal(undefined);
79+
80+
expect(parseLiteral('1.0')).to.equal(undefined);
81+
expect(parseLiteral('null')).to.equal(undefined);
82+
expect(parseLiteral('""')).to.equal(undefined);
83+
expect(parseLiteral('"123"')).to.equal(undefined);
84+
expect(parseLiteral('false')).to.equal(undefined);
85+
expect(parseLiteral('[1]')).to.equal(undefined);
86+
expect(parseLiteral('{ value: 1 }')).to.equal(undefined);
87+
expect(parseLiteral('ENUM_VALUE')).to.equal(undefined);
88+
expect(parseLiteral('$var')).to.equal(undefined);
89+
});
90+
});
91+
92+
describe('GraphQLFloat', () => {
93+
it('parseValue', () => {
94+
function parseValue(value) {
95+
return GraphQLFloat.parseValue(value);
96+
}
97+
98+
expect(parseValue(1)).to.equal(1);
99+
expect(parseValue(0)).to.equal(0);
100+
expect(parseValue(-1)).to.equal(-1);
101+
expect(parseValue(0.1)).to.equal(0.1);
102+
expect(parseValue(Math.PI)).to.equal(Math.PI);
103+
104+
expect(() => parseValue(NaN)).to.throw(
105+
'Float cannot represent non numeric value: NaN',
106+
);
107+
expect(() => parseValue(Infinity)).to.throw(
108+
'Float cannot represent non numeric value: Infinity',
109+
);
110+
111+
expect(() => parseValue(undefined)).to.throw(
112+
'Float cannot represent non numeric value: undefined',
113+
);
114+
expect(() => parseValue(null)).to.throw(
115+
'Float cannot represent non numeric value: null',
116+
);
117+
expect(() => parseValue('')).to.throw(
118+
'Float cannot represent non numeric value: ""',
119+
);
120+
expect(() => parseValue('123')).to.throw(
121+
'Float cannot represent non numeric value: "123"',
122+
);
123+
expect(() => parseValue('123.5')).to.throw(
124+
'Float cannot represent non numeric value: "123.5"',
125+
);
126+
expect(() => parseValue(false)).to.throw(
127+
'Float cannot represent non numeric value: false',
128+
);
129+
expect(() => parseValue(true)).to.throw(
130+
'Float cannot represent non numeric value: true',
131+
);
132+
expect(() => parseValue([0.1])).to.throw(
133+
'Float cannot represent non numeric value: [0.1]',
134+
);
135+
expect(() => parseValue({ value: 0.1 })).to.throw(
136+
'Float cannot represent non numeric value: { value: 0.1 }',
137+
);
138+
});
139+
140+
it('parseLiteral', () => {
141+
function parseLiteral(str) {
142+
return GraphQLFloat.parseLiteral(parseValueToAST(str));
143+
}
144+
145+
expect(parseLiteral('1')).to.equal(1);
146+
expect(parseLiteral('0')).to.equal(0);
147+
expect(parseLiteral('-1')).to.equal(-1);
148+
expect(parseLiteral('0.1')).to.equal(0.1);
149+
expect(parseLiteral(Math.PI.toString())).to.equal(Math.PI);
150+
151+
expect(parseLiteral('null')).to.equal(undefined);
152+
expect(parseLiteral('""')).to.equal(undefined);
153+
expect(parseLiteral('"123"')).to.equal(undefined);
154+
expect(parseLiteral('"123.5"')).to.equal(undefined);
155+
expect(parseLiteral('false')).to.equal(undefined);
156+
expect(parseLiteral('[0.1]')).to.equal(undefined);
157+
expect(parseLiteral('{ value: 0.1 }')).to.equal(undefined);
158+
expect(parseLiteral('ENUM_VALUE')).to.equal(undefined);
159+
expect(parseLiteral('$var')).to.equal(undefined);
160+
});
161+
});
162+
163+
describe('GraphQLString', () => {
164+
it('parseValue', () => {
165+
function parseValue(value) {
166+
return GraphQLString.parseValue(value);
167+
}
168+
169+
expect(parseValue('foo')).to.equal('foo');
170+
171+
expect(() => parseValue(undefined)).to.throw(
172+
'String cannot represent a non string value: undefined',
173+
);
174+
expect(() => parseValue(null)).to.throw(
175+
'String cannot represent a non string value: null',
176+
);
177+
expect(() => parseValue(1)).to.throw(
178+
'String cannot represent a non string value: 1',
179+
);
180+
expect(() => parseValue(NaN)).to.throw(
181+
'String cannot represent a non string value: NaN',
182+
);
183+
expect(() => parseValue(false)).to.throw(
184+
'String cannot represent a non string value: false',
185+
);
186+
expect(() => parseValue(['foo'])).to.throw(
187+
'String cannot represent a non string value: ["foo"]',
188+
);
189+
expect(() => parseValue({ value: 'foo' })).to.throw(
190+
'String cannot represent a non string value: { value: "foo" }',
191+
);
192+
});
193+
194+
it('parseLiteral', () => {
195+
function parseLiteral(str) {
196+
return GraphQLString.parseLiteral(parseValueToAST(str));
197+
}
198+
199+
expect(parseLiteral('"foo"')).to.equal('foo');
200+
expect(parseLiteral('"""bar"""')).to.equal('bar');
201+
202+
expect(parseLiteral('null')).to.equal(undefined);
203+
expect(parseLiteral('1')).to.equal(undefined);
204+
expect(parseLiteral('0.1')).to.equal(undefined);
205+
expect(parseLiteral('false')).to.equal(undefined);
206+
expect(parseLiteral('["foo"]')).to.equal(undefined);
207+
expect(parseLiteral('{ value: "foo" }')).to.equal(undefined);
208+
expect(parseLiteral('ENUM_VALUE')).to.equal(undefined);
209+
expect(parseLiteral('$var')).to.equal(undefined);
210+
});
211+
});
212+
213+
describe('GraphQLBoolean', () => {
214+
it('parseValue', () => {
215+
function parseValue(value) {
216+
return GraphQLBoolean.parseValue(value);
217+
}
218+
219+
expect(parseValue(true)).to.equal(true);
220+
expect(parseValue(false)).to.equal(false);
221+
222+
expect(() => parseValue(undefined)).to.throw(
223+
'Boolean cannot represent a non boolean value: undefined',
224+
);
225+
expect(() => parseValue(null)).to.throw(
226+
'Boolean cannot represent a non boolean value: null',
227+
);
228+
expect(() => parseValue(0)).to.throw(
229+
'Boolean cannot represent a non boolean value: 0',
230+
);
231+
expect(() => parseValue(1)).to.throw(
232+
'Boolean cannot represent a non boolean value: 1',
233+
);
234+
expect(() => parseValue(NaN)).to.throw(
235+
'Boolean cannot represent a non boolean value: NaN',
236+
);
237+
expect(() => parseValue('')).to.throw(
238+
'Boolean cannot represent a non boolean value: ""',
239+
);
240+
expect(() => parseValue('false')).to.throw(
241+
'Boolean cannot represent a non boolean value: "false"',
242+
);
243+
expect(() => parseValue([false])).to.throw(
244+
'Boolean cannot represent a non boolean value: [false]',
245+
);
246+
expect(() => parseValue({ value: false })).to.throw(
247+
'Boolean cannot represent a non boolean value: { value: false }',
248+
);
249+
});
250+
251+
it('parseLiteral', () => {
252+
function parseLiteral(str) {
253+
return GraphQLBoolean.parseLiteral(parseValueToAST(str));
254+
}
255+
256+
expect(parseLiteral('true')).to.equal(true);
257+
expect(parseLiteral('false')).to.equal(false);
258+
259+
expect(parseLiteral('null')).to.equal(undefined);
260+
expect(parseLiteral('0')).to.equal(undefined);
261+
expect(parseLiteral('1')).to.equal(undefined);
262+
expect(parseLiteral('0.1')).to.equal(undefined);
263+
expect(parseLiteral('""')).to.equal(undefined);
264+
expect(parseLiteral('"false"')).to.equal(undefined);
265+
expect(parseLiteral('[false]')).to.equal(undefined);
266+
expect(parseLiteral('{ value: false }')).to.equal(undefined);
267+
expect(parseLiteral('ENUM_VALUE')).to.equal(undefined);
268+
expect(parseLiteral('$var')).to.equal(undefined);
269+
});
270+
});
271+
272+
describe('GraphQLID', () => {
273+
it('parseValue', () => {
274+
function parseValue(value) {
275+
return GraphQLID.parseValue(value);
276+
}
277+
278+
expect(parseValue('')).to.equal('');
279+
expect(parseValue('1')).to.equal('1');
280+
expect(parseValue('foo')).to.equal('foo');
281+
expect(parseValue(1)).to.equal('1');
282+
expect(parseValue(0)).to.equal('0');
283+
expect(parseValue(-1)).to.equal('-1');
284+
285+
// Maximum and minimum safe numbers in JS
286+
expect(parseValue(9007199254740991)).to.equal('9007199254740991');
287+
expect(parseValue(-9007199254740991)).to.equal('-9007199254740991');
288+
289+
expect(() => parseValue(undefined)).to.throw(
290+
'ID cannot represent value: undefined',
291+
);
292+
expect(() => parseValue(null)).to.throw(
293+
'ID cannot represent value: null',
294+
);
295+
expect(() => parseValue(0.1)).to.throw('ID cannot represent value: 0.1');
296+
expect(() => parseValue(NaN)).to.throw('ID cannot represent value: NaN');
297+
expect(() => parseValue(Infinity)).to.throw(
298+
'ID cannot represent value: Inf',
299+
);
300+
expect(() => parseValue(false)).to.throw(
301+
'ID cannot represent value: false',
302+
);
303+
expect(() => GraphQLID.parseValue(['1'])).to.throw(
304+
'ID cannot represent value: ["1"]',
305+
);
306+
expect(() => GraphQLID.parseValue({ value: '1' })).to.throw(
307+
'ID cannot represent value: { value: "1" }',
308+
);
309+
});
310+
311+
it('parseLiteral', () => {
312+
function parseLiteral(str) {
313+
return GraphQLID.parseLiteral(parseValueToAST(str));
314+
}
315+
316+
expect(parseLiteral('""')).to.equal('');
317+
expect(parseLiteral('"1"')).to.equal('1');
318+
expect(parseLiteral('"foo"')).to.equal('foo');
319+
expect(parseLiteral('"""foo"""')).to.equal('foo');
320+
expect(parseLiteral('1')).to.equal('1');
321+
expect(parseLiteral('0')).to.equal('0');
322+
expect(parseLiteral('-1')).to.equal('-1');
323+
324+
// Support arbituary long numbers even if they can't be represented in JS
325+
expect(parseLiteral('90071992547409910')).to.equal('90071992547409910');
326+
expect(parseLiteral('-90071992547409910')).to.equal('-90071992547409910');
327+
328+
expect(parseLiteral('null')).to.equal(undefined);
329+
expect(parseLiteral('0.1')).to.equal(undefined);
330+
expect(parseLiteral('false')).to.equal(undefined);
331+
expect(parseLiteral('["1"]')).to.equal(undefined);
332+
expect(parseLiteral('{ value: "1" }')).to.equal(undefined);
333+
expect(parseLiteral('ENUM_VALUE')).to.equal(undefined);
334+
expect(parseLiteral('$var')).to.equal(undefined);
335+
});
336+
});
337+
});

0 commit comments

Comments
 (0)