Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add testcases for nested list coercion #1528

Merged
merged 3 commits into from
Sep 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions src/utilities/__tests__/coerceValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
GraphQLID,
GraphQLInt,
GraphQLFloat,
GraphQLList,
GraphQLString,
GraphQLEnumType,
GraphQLInputObjectType,
Expand Down Expand Up @@ -237,14 +238,14 @@ describe('coerceValue', () => {
expectValue(result).to.deep.equal({ foo: 123 });
});

it('returns no error for a non-object type', () => {
it('returns an 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', () => {
it('returns an 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"',
Expand Down Expand Up @@ -283,4 +284,60 @@ describe('coerceValue', () => {
]);
});
});

describe('for GraphQLList', () => {
const TestList = GraphQLList(GraphQLInt);

it('returns no error for a valid input', () => {
const result = coerceValue([1, 2, 3], TestList);
expectValue(result).to.deep.equal([1, 2, 3]);
});

it('returns an error for an invalid input', () => {
const result = coerceValue([1, 'b', true], TestList);
expectErrors(result).to.deep.equal([
'Expected type Int at value[1]; Int cannot represent non-integer value: "b"',
'Expected type Int at value[2]; Int cannot represent non-integer value: true',
]);
});

it('returns a list for a non-list value', () => {
const result = coerceValue(42, TestList);
expectValue(result).to.deep.equal([42]);
});

it('returns null for a null value', () => {
const result = coerceValue(null, TestList);
expectValue(result).to.deep.equal(null);
});
});

describe('for nested GraphQLList', () => {
const TestNestedList = GraphQLList(GraphQLList(GraphQLInt));

it('returns no error for a valid input', () => {
const result = coerceValue([[1], [2, 3]], TestNestedList);
expectValue(result).to.deep.equal([[1], [2, 3]]);
});

it('returns a list for a non-list value', () => {
const result = coerceValue(42, TestNestedList);
expectValue(result).to.deep.equal([[42]]);
});

it('returns null for a null value', () => {
const result = coerceValue(null, TestNestedList);
expectValue(result).to.deep.equal(null);
});

it('returns nested lists for nested non-list values', () => {
const result = coerceValue([1, 2, 3], TestNestedList);
expectValue(result).to.deep.equal([[1], [2], [3]]);
});

it('returns nested null for nested null values', () => {
const result = coerceValue([42, [null], null], TestNestedList);
expectValue(result).to.deep.equal([[42], [null], null]);
});
});
});