Skip to content

Commit dec24f9

Browse files
authored
add testcases for nested list coercion (#1528)
1 parent 19ea592 commit dec24f9

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

src/utilities/__tests__/coerceValue-test.js

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
GraphQLID,
1515
GraphQLInt,
1616
GraphQLFloat,
17+
GraphQLList,
1718
GraphQLString,
1819
GraphQLEnumType,
1920
GraphQLInputObjectType,
@@ -239,14 +240,14 @@ describe('coerceValue', () => {
239240
expectValue(result).to.deep.equal({ foo: 123 });
240241
});
241242

242-
it('returns no error for a non-object type', () => {
243+
it('returns an error for a non-object type', () => {
243244
const result = coerceValue(123, TestInputObject);
244245
expectErrors(result).to.deep.equal([
245246
'Expected type TestInputObject to be an object.',
246247
]);
247248
});
248249

249-
it('returns no error for an invalid field', () => {
250+
it('returns an error for an invalid field', () => {
250251
const result = coerceValue({ foo: 'abc' }, TestInputObject);
251252
expectErrors(result).to.deep.equal([
252253
'Expected type Int at value.foo; Int cannot represent non-integer value: "abc"',
@@ -285,4 +286,60 @@ describe('coerceValue', () => {
285286
]);
286287
});
287288
});
289+
290+
describe('for GraphQLList', () => {
291+
const TestList = GraphQLList(GraphQLInt);
292+
293+
it('returns no error for a valid input', () => {
294+
const result = coerceValue([1, 2, 3], TestList);
295+
expectValue(result).to.deep.equal([1, 2, 3]);
296+
});
297+
298+
it('returns an error for an invalid input', () => {
299+
const result = coerceValue([1, 'b', true], TestList);
300+
expectErrors(result).to.deep.equal([
301+
'Expected type Int at value[1]; Int cannot represent non-integer value: "b"',
302+
'Expected type Int at value[2]; Int cannot represent non-integer value: true',
303+
]);
304+
});
305+
306+
it('returns a list for a non-list value', () => {
307+
const result = coerceValue(42, TestList);
308+
expectValue(result).to.deep.equal([42]);
309+
});
310+
311+
it('returns null for a null value', () => {
312+
const result = coerceValue(null, TestList);
313+
expectValue(result).to.deep.equal(null);
314+
});
315+
});
316+
317+
describe('for nested GraphQLList', () => {
318+
const TestNestedList = GraphQLList(GraphQLList(GraphQLInt));
319+
320+
it('returns no error for a valid input', () => {
321+
const result = coerceValue([[1], [2, 3]], TestNestedList);
322+
expectValue(result).to.deep.equal([[1], [2, 3]]);
323+
});
324+
325+
it('returns a list for a non-list value', () => {
326+
const result = coerceValue(42, TestNestedList);
327+
expectValue(result).to.deep.equal([[42]]);
328+
});
329+
330+
it('returns null for a null value', () => {
331+
const result = coerceValue(null, TestNestedList);
332+
expectValue(result).to.deep.equal(null);
333+
});
334+
335+
it('returns nested lists for nested non-list values', () => {
336+
const result = coerceValue([1, 2, 3], TestNestedList);
337+
expectValue(result).to.deep.equal([[1], [2], [3]]);
338+
});
339+
340+
it('returns nested null for nested null values', () => {
341+
const result = coerceValue([42, [null], null], TestNestedList);
342+
expectValue(result).to.deep.equal([[42], [null], null]);
343+
});
344+
});
288345
});

0 commit comments

Comments
 (0)