/
coerceValue-test.js
265 lines (227 loc) · 8.6 KB
/
coerceValue-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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { describe, it } from 'mocha';
import { expect } from 'chai';
import { coerceValue } from '../coerceValue';
import {
GraphQLID,
GraphQLInt,
GraphQLFloat,
GraphQLString,
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLNonNull,
} from '../../type';
function expectValue(result) {
expect(result.errors).to.equal(undefined);
return expect(result.value);
}
function expectErrors(result) {
expect(result.value).to.equal(undefined);
const messages = result.errors && result.errors.map(error => error.message);
return expect(messages);
}
describe('coerceValue', () => {
for (const scalar of [GraphQLString, GraphQLID]) {
describe(`for GraphQL${scalar}`, () => {
it('returns error for array input as string', () => {
const result = coerceValue([1, 2, 3], scalar);
expectErrors(result).to.deep.equal([
`Expected type ${scalar}; String cannot represent an array value: [1, 2, 3]`,
]);
});
});
}
describe('for GraphQLInt', () => {
it('returns no error for int input', () => {
const result = coerceValue('1', GraphQLInt);
expectValue(result).to.equal(1);
});
it('returns no error for negative int input', () => {
const result = coerceValue('-1', GraphQLInt);
expectValue(result).to.equal(-1);
});
it('returns no error for exponent input', () => {
const result = coerceValue('1e3', GraphQLInt);
expectValue(result).to.equal(1000);
});
it('returns a single error for empty value', () => {
const result = coerceValue(null, GraphQLInt);
expectValue(result).to.equal(null);
});
it('returns a single error for empty string as value', () => {
const result = coerceValue('', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: (empty string)',
]);
});
it('returns a single error for 2^32 input as int', () => {
const result = coerceValue(Math.pow(2, 32), GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non 32-bit signed integer value: 4294967296',
]);
});
it('returns a single error for float input as int', () => {
const result = coerceValue('1.5', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "1.5"',
]);
});
it('returns a single error for NaN input as int', () => {
const result = coerceValue(NaN, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: NaN',
]);
});
it('returns a single error for Infinity input as int', () => {
const result = coerceValue(Infinity, GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: Infinity',
]);
});
it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "a"',
]);
});
it('returns a single error for char input', () => {
const result = coerceValue('meow', GraphQLInt);
expectErrors(result).to.deep.equal([
'Expected type Int; Int cannot represent non-integer value: "meow"',
]);
});
});
describe('for GraphQLFloat', () => {
it('returns no error for int input', () => {
const result = coerceValue('1', GraphQLFloat);
expectValue(result).to.equal(1);
});
it('returns no error for exponent input', () => {
const result = coerceValue('1e3', GraphQLFloat);
expectValue(result).to.equal(1000);
});
it('returns no error for float input', () => {
const result = coerceValue('1.5', GraphQLFloat);
expectValue(result).to.equal(1.5);
});
it('returns a single error for empty value', () => {
const result = coerceValue(null, GraphQLFloat);
expectValue(result).to.equal(null);
});
it('returns a single error for empty string input', () => {
const result = coerceValue('', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: (empty string)',
]);
});
it('returns a single error for NaN input', () => {
const result = coerceValue(NaN, GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: NaN',
]);
});
it('returns a single error for Infinity input', () => {
const result = coerceValue(Infinity, GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: Infinity',
]);
});
it('returns a single error for char input', () => {
const result = coerceValue('a', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: "a"',
]);
});
it('returns a single error for char input', () => {
const result = coerceValue('meow', GraphQLFloat);
expectErrors(result).to.deep.equal([
'Expected type Float; Float cannot represent non numeric value: "meow"',
]);
});
});
describe('for GraphQLEnum', () => {
const TestEnum = new GraphQLEnumType({
name: 'TestEnum',
values: {
FOO: { value: 'InternalFoo' },
BAR: { value: 123456789 },
},
});
it('returns no error for a known enum name', () => {
const fooResult = coerceValue('FOO', TestEnum);
expectValue(fooResult).to.equal('InternalFoo');
const barResult = coerceValue('BAR', TestEnum);
expectValue(barResult).to.equal(123456789);
});
it('results error for misspelled enum value', () => {
const result = coerceValue('foo', TestEnum);
expectErrors(result).to.deep.equal([
'Expected type TestEnum; did you mean FOO?',
]);
});
it('results error for incorrect value type', () => {
const result1 = coerceValue(123, TestEnum);
expectErrors(result1).to.deep.equal(['Expected type TestEnum.']);
const result2 = coerceValue({ field: 'value' }, TestEnum);
expectErrors(result2).to.deep.equal(['Expected type TestEnum.']);
});
});
describe('for GraphQLInputObject', () => {
const TestInputObject = new GraphQLInputObjectType({
name: 'TestInputObject',
fields: {
foo: { type: GraphQLNonNull(GraphQLInt) },
bar: { type: GraphQLInt },
},
});
it('returns no error for a valid input', () => {
const result = coerceValue({ foo: 123 }, TestInputObject);
expectValue(result).to.deep.equal({ foo: 123 });
});
it('returns no error for a non-object type', () => {
const result = coerceValue(123, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type TestInputObject to be an object.',
]);
});
it('returns no error for an invalid field', () => {
const result = coerceValue({ foo: 'abc' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non-integer value: "abc"',
]);
});
it('returns multiple errors for multiple invalid fields', () => {
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
expectErrors(result).to.deep.equal([
'Expected type Int at value.foo; Int cannot represent non-integer value: "abc"',
'Expected type Int at value.bar; Int cannot represent non-integer value: "def"',
]);
});
it('returns error for a missing required field', () => {
const result = coerceValue({ bar: 123 }, TestInputObject);
expectErrors(result).to.deep.equal([
'Field value.foo of required type Int! was not provided.',
]);
});
it('returns error for an unknown field', () => {
const result = coerceValue(
{ foo: 123, unknownField: 123 },
TestInputObject,
);
expectErrors(result).to.deep.equal([
'Field "unknownField" is not defined by type TestInputObject.',
]);
});
it('returns error for a misspelled field', () => {
const result = coerceValue({ foo: 123, bart: 123 }, TestInputObject);
expectErrors(result).to.deep.equal([
'Field "bart" is not defined by type TestInputObject; did you mean bar?',
]);
});
});
});