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

astFromValue-test: improve coverage #2350

Merged
merged 1 commit into from
Jan 15, 2020
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
78 changes: 63 additions & 15 deletions src/utilities/__tests__/astFromValue-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {
GraphQLList,
GraphQLNonNull,
GraphQLScalarType,
GraphQLEnumType,
GraphQLInputObjectType,
} from '../../type/definition';
Expand Down Expand Up @@ -191,6 +192,42 @@ describe('astFromValue', () => {
expect(astFromValue(undefined, GraphQLID)).to.deep.equal(null);
});

it('converts using serialize from a custom scalar type', () => {
const passthroughScalar = new GraphQLScalarType({
name: 'PassthroughScalar',
serialize(value) {
return value;
},
});

expect(astFromValue('value', passthroughScalar)).to.deep.equal({
kind: 'StringValue',
value: 'value',
});

const returnNullScalar = new GraphQLScalarType({
name: 'ReturnNullScalar',
serialize() {
return null;
},
});

expect(astFromValue('value', returnNullScalar)).to.equal(null);

class SomeClass {}

const returnCustomClassScalar = new GraphQLScalarType({
name: 'ReturnCustomClassScalar',
serialize() {
return new SomeClass();
},
});

expect(() => astFromValue('value', returnCustomClassScalar)).to.throw(
'Cannot convert value to AST: {}.',
);
});

it('does not converts NonNull values to NullValue', () => {
const NonNullBoolean = GraphQLNonNull(GraphQLBoolean);
expect(astFromValue(null, NonNullBoolean)).to.deep.equal(null);
Expand Down Expand Up @@ -258,15 +295,30 @@ describe('astFromValue', () => {
});
});

it('converts input objects', () => {
const inputObj = new GraphQLInputObjectType({
name: 'MyInputObj',
fields: {
foo: { type: GraphQLFloat },
bar: { type: myEnum },
},
it('skip invalid list items', () => {
const ast = astFromValue(
['FOO', null, 'BAR'],
GraphQLList(GraphQLNonNull(GraphQLString)),
);

expect(ast).to.deep.equal({
kind: 'ListValue',
values: [
{ kind: 'StringValue', value: 'FOO' },
{ kind: 'StringValue', value: 'BAR' },
],
});
});

const inputObj = new GraphQLInputObjectType({
name: 'MyInputObj',
fields: {
foo: { type: GraphQLFloat },
bar: { type: myEnum },
},
});

it('converts input objects', () => {
expect(astFromValue({ foo: 3, bar: 'HELLO' }, inputObj)).to.deep.equal({
kind: 'ObjectValue',
fields: [
Expand All @@ -285,14 +337,6 @@ describe('astFromValue', () => {
});

it('converts input objects with explicit nulls', () => {
const inputObj = new GraphQLInputObjectType({
name: 'MyInputObj',
fields: {
foo: { type: GraphQLFloat },
bar: { type: myEnum },
},
});

expect(astFromValue({ foo: null }, inputObj)).to.deep.equal({
kind: 'ObjectValue',
fields: [
Expand All @@ -304,4 +348,8 @@ describe('astFromValue', () => {
],
});
});

it('does not converts non-object values as input objects', () => {
expect(astFromValue(5, inputObj)).to.equal(null);
});
});