Skip to content

Commit 34c5580

Browse files
astFromValue-test: improve coverage (#2350)
1 parent 4623c3d commit 34c5580

File tree

1 file changed

+63
-15
lines changed

1 file changed

+63
-15
lines changed

src/utilities/__tests__/astFromValue-test.js

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import {
1414
GraphQLList,
1515
GraphQLNonNull,
16+
GraphQLScalarType,
1617
GraphQLEnumType,
1718
GraphQLInputObjectType,
1819
} from '../../type/definition';
@@ -191,6 +192,42 @@ describe('astFromValue', () => {
191192
expect(astFromValue(undefined, GraphQLID)).to.deep.equal(null);
192193
});
193194

195+
it('converts using serialize from a custom scalar type', () => {
196+
const passthroughScalar = new GraphQLScalarType({
197+
name: 'PassthroughScalar',
198+
serialize(value) {
199+
return value;
200+
},
201+
});
202+
203+
expect(astFromValue('value', passthroughScalar)).to.deep.equal({
204+
kind: 'StringValue',
205+
value: 'value',
206+
});
207+
208+
const returnNullScalar = new GraphQLScalarType({
209+
name: 'ReturnNullScalar',
210+
serialize() {
211+
return null;
212+
},
213+
});
214+
215+
expect(astFromValue('value', returnNullScalar)).to.equal(null);
216+
217+
class SomeClass {}
218+
219+
const returnCustomClassScalar = new GraphQLScalarType({
220+
name: 'ReturnCustomClassScalar',
221+
serialize() {
222+
return new SomeClass();
223+
},
224+
});
225+
226+
expect(() => astFromValue('value', returnCustomClassScalar)).to.throw(
227+
'Cannot convert value to AST: {}.',
228+
);
229+
});
230+
194231
it('does not converts NonNull values to NullValue', () => {
195232
const NonNullBoolean = GraphQLNonNull(GraphQLBoolean);
196233
expect(astFromValue(null, NonNullBoolean)).to.deep.equal(null);
@@ -258,15 +295,30 @@ describe('astFromValue', () => {
258295
});
259296
});
260297

261-
it('converts input objects', () => {
262-
const inputObj = new GraphQLInputObjectType({
263-
name: 'MyInputObj',
264-
fields: {
265-
foo: { type: GraphQLFloat },
266-
bar: { type: myEnum },
267-
},
298+
it('skip invalid list items', () => {
299+
const ast = astFromValue(
300+
['FOO', null, 'BAR'],
301+
GraphQLList(GraphQLNonNull(GraphQLString)),
302+
);
303+
304+
expect(ast).to.deep.equal({
305+
kind: 'ListValue',
306+
values: [
307+
{ kind: 'StringValue', value: 'FOO' },
308+
{ kind: 'StringValue', value: 'BAR' },
309+
],
268310
});
311+
});
269312

313+
const inputObj = new GraphQLInputObjectType({
314+
name: 'MyInputObj',
315+
fields: {
316+
foo: { type: GraphQLFloat },
317+
bar: { type: myEnum },
318+
},
319+
});
320+
321+
it('converts input objects', () => {
270322
expect(astFromValue({ foo: 3, bar: 'HELLO' }, inputObj)).to.deep.equal({
271323
kind: 'ObjectValue',
272324
fields: [
@@ -285,14 +337,6 @@ describe('astFromValue', () => {
285337
});
286338

287339
it('converts input objects with explicit nulls', () => {
288-
const inputObj = new GraphQLInputObjectType({
289-
name: 'MyInputObj',
290-
fields: {
291-
foo: { type: GraphQLFloat },
292-
bar: { type: myEnum },
293-
},
294-
});
295-
296340
expect(astFromValue({ foo: null }, inputObj)).to.deep.equal({
297341
kind: 'ObjectValue',
298342
fields: [
@@ -304,4 +348,8 @@ describe('astFromValue', () => {
304348
],
305349
});
306350
});
351+
352+
it('does not converts non-object values as input objects', () => {
353+
expect(astFromValue(5, inputObj)).to.equal(null);
354+
});
307355
});

0 commit comments

Comments
 (0)