Skip to content

Commit

Permalink
astFromValue-test: improve coverage (#2350)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jan 15, 2020
1 parent 4623c3d commit 34c5580
Showing 1 changed file with 63 additions and 15 deletions.
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);
});
});

0 comments on commit 34c5580

Please sign in to comment.