Skip to content

Commit 1ededa1

Browse files
erikkessler1benjie
authored andcommitted
Dedicated coerceInputValue tests for oneOf
Extracted from original v17 implementation within graphql#3513 Co-authored-by: Benjie Gillam <benjie@jemjie.com>
1 parent d04dd72 commit 1ededa1

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

src/utilities/__tests__/coerceInputValue-test.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,111 @@ describe('coerceInputValue', () => {
411411
});
412412
});
413413

414+
describe('for GraphQLInputObject that isOneOf', () => {
415+
const TestInputObject = new GraphQLInputObjectType({
416+
name: 'TestInputObject',
417+
fields: {
418+
foo: { type: GraphQLInt },
419+
bar: { type: GraphQLInt },
420+
},
421+
isOneOf: true,
422+
});
423+
424+
it('returns no error for a valid input', () => {
425+
const result = coerceValue({ foo: 123 }, TestInputObject);
426+
expectValue(result).to.deep.equal({ foo: 123 });
427+
});
428+
429+
it('returns an error if more than one field is specified', () => {
430+
const result = coerceValue({ foo: 123, bar: null }, TestInputObject);
431+
expectErrors(result).to.deep.equal([
432+
{
433+
error:
434+
'Exactly one key must be specified for OneOf type "TestInputObject".',
435+
path: [],
436+
value: { foo: 123, bar: null },
437+
},
438+
]);
439+
});
440+
441+
it('returns an error the one field is null', () => {
442+
const result = coerceValue({ bar: null }, TestInputObject);
443+
expectErrors(result).to.deep.equal([
444+
{
445+
error: 'Field "bar" must be non-null.',
446+
path: ['bar'],
447+
value: null,
448+
},
449+
]);
450+
});
451+
452+
it('returns an error for an invalid field', () => {
453+
const result = coerceValue({ foo: NaN }, TestInputObject);
454+
expectErrors(result).to.deep.equal([
455+
{
456+
error: 'Int cannot represent non-integer value: NaN',
457+
path: ['foo'],
458+
value: NaN,
459+
},
460+
]);
461+
});
462+
463+
it('returns multiple errors for multiple invalid fields', () => {
464+
const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject);
465+
expectErrors(result).to.deep.equal([
466+
{
467+
error: 'Int cannot represent non-integer value: "abc"',
468+
path: ['foo'],
469+
value: 'abc',
470+
},
471+
{
472+
error: 'Int cannot represent non-integer value: "def"',
473+
path: ['bar'],
474+
value: 'def',
475+
},
476+
{
477+
error:
478+
'Exactly one key must be specified for OneOf type "TestInputObject".',
479+
path: [],
480+
value: { foo: 'abc', bar: 'def' },
481+
},
482+
]);
483+
});
484+
485+
it('returns error for an unknown field', () => {
486+
const result = coerceValue(
487+
{ foo: 123, unknownField: 123 },
488+
TestInputObject,
489+
);
490+
expectErrors(result).to.deep.equal([
491+
{
492+
error:
493+
'Field "unknownField" is not defined by type "TestInputObject".',
494+
path: [],
495+
value: { foo: 123, unknownField: 123 },
496+
},
497+
]);
498+
});
499+
500+
it('returns error for a misspelled field', () => {
501+
const result = coerceValue({ bart: 123 }, TestInputObject);
502+
expectErrors(result).to.deep.equal([
503+
{
504+
error:
505+
'Field "bart" is not defined by type "TestInputObject". Did you mean "bar"?',
506+
path: [],
507+
value: { bart: 123 },
508+
},
509+
{
510+
error:
511+
'Exactly one key must be specified for OneOf type "TestInputObject".',
512+
path: [],
513+
value: { bart: 123 },
514+
},
515+
]);
516+
});
517+
});
518+
414519
describe('for GraphQLInputObject with default value', () => {
415520
const makeTestInputObject = (defaultValue: any) =>
416521
new GraphQLInputObjectType({

0 commit comments

Comments
 (0)