Skip to content

Commit df543ae

Browse files
committed
add one of validation tests
1 parent 62d9bbc commit df543ae

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed

src/utilities/__tests__/validateInputValue-test.ts

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,111 @@ describe('validateInputValue', () => {
373373
});
374374
});
375375

376+
describe('for GraphQLInputObject that isOneOf', () => {
377+
const TestInputObject = new GraphQLInputObjectType({
378+
name: 'TestInputObject',
379+
fields: {
380+
foo: { type: GraphQLInt },
381+
bar: { type: GraphQLInt },
382+
},
383+
isOneOf: true,
384+
});
385+
386+
it('no error for a valid input', () => {
387+
test({ foo: 123 }, TestInputObject, []);
388+
});
389+
390+
it('returns error if more than one field is specified', () => {
391+
test({ foo: 123, bar: null }, TestInputObject, [
392+
{
393+
error:
394+
'Exactly one key must be specified for OneOf type "TestInputObject".',
395+
path: [],
396+
},
397+
]);
398+
});
399+
400+
it('returns error if the one field is null', () => {
401+
test({ bar: null }, TestInputObject, [
402+
{
403+
error:
404+
'Field "bar" for OneOf type "TestInputObject" must be non-null.',
405+
path: [],
406+
},
407+
]);
408+
});
409+
410+
it('returns error for an invalid field', () => {
411+
test({ foo: NaN }, TestInputObject, [
412+
{
413+
error: 'Int cannot represent non-integer value: NaN',
414+
path: ['foo'],
415+
},
416+
]);
417+
});
418+
419+
it('returns multiple errors for multiple invalid fields', () => {
420+
test({ foo: 'abc', bar: 'def' }, TestInputObject, [
421+
{
422+
error: 'Int cannot represent non-integer value: "abc"',
423+
path: ['foo'],
424+
},
425+
{
426+
error: 'Int cannot represent non-integer value: "def"',
427+
path: ['bar'],
428+
},
429+
{
430+
error:
431+
'Exactly one key must be specified for OneOf type "TestInputObject".',
432+
path: [],
433+
},
434+
]);
435+
});
436+
437+
it('returns error for an unknown field', () => {
438+
// TODO: not technically a OneOf error, as the OneOf validation assumes known fields
439+
test({ foo: 123, unknownField: 123 }, TestInputObject, [
440+
{
441+
error:
442+
'Expected value of type "TestInputObject" not to include unknown field "unknownField", found: { foo: 123, unknownField: 123 }.',
443+
path: [],
444+
},
445+
{
446+
error:
447+
'Exactly one key must be specified for OneOf type "TestInputObject".',
448+
path: [],
449+
},
450+
]);
451+
});
452+
453+
it('returns error for a misspelled field', () => {
454+
// TODO: technically also a OneOf error, as the OneOf validation assumes known fields, so there are no errors here
455+
test({ bart: 123 }, TestInputObject, [
456+
{
457+
error:
458+
'Expected value of type "TestInputObject" not to include unknown field "bart". Did you mean "bar"? Found: { bart: 123 }.',
459+
path: [],
460+
},
461+
]);
462+
});
463+
464+
it('returns error for a misspelled field (no suggestions)', () => {
465+
// TODO: technically also a OneOf error, as the OneOf validation assumes known fields, so there are no errors here
466+
test(
467+
{ bart: 123 },
468+
TestInputObject,
469+
[
470+
{
471+
error:
472+
'Expected value of type "TestInputObject" not to include unknown field "bart", found: { bart: 123 }.',
473+
path: [],
474+
},
475+
],
476+
true,
477+
);
478+
});
479+
});
480+
376481
describe('for GraphQLList', () => {
377482
const TestList = new GraphQLList(GraphQLInt);
378483

@@ -849,6 +954,157 @@ describe('validateInputLiteral', () => {
849954
});
850955
});
851956

957+
describe('for GraphQLInputObject that isOneOf', () => {
958+
const TestInputObject = new GraphQLInputObjectType({
959+
name: 'TestInputObject',
960+
fields: {
961+
foo: { type: GraphQLInt },
962+
bar: { type: GraphQLInt },
963+
},
964+
isOneOf: true,
965+
});
966+
967+
it('returns no error for a valid input', () => {
968+
test('{ foo: 123 }', TestInputObject, []);
969+
});
970+
971+
it('returns an error if more than one field is specified', () => {
972+
test('{ foo: 123, bar: null }', TestInputObject, [
973+
{
974+
error:
975+
'OneOf Input Object "TestInputObject" must specify exactly one key.',
976+
path: [],
977+
},
978+
]);
979+
});
980+
981+
it('returns an error if the one field is null', () => {
982+
test('{ bar: null }', TestInputObject, [
983+
{
984+
error:
985+
'Field "TestInputObject.bar" used for OneOf Input Object must be non-null.',
986+
path: ['bar'],
987+
},
988+
]);
989+
});
990+
991+
it('returns an error for a non-object type', () => {
992+
test('123', TestInputObject, [
993+
{
994+
error:
995+
'Expected value of type "TestInputObject" to be an object, found: 123.',
996+
path: [],
997+
},
998+
]);
999+
});
1000+
1001+
it('returns an error for an invalid field', () => {
1002+
test('{ foo: 1.5 }', TestInputObject, [
1003+
{
1004+
error: 'Int cannot represent non-integer value: 1.5',
1005+
path: ['foo'],
1006+
},
1007+
]);
1008+
});
1009+
1010+
it('returns error for an unknown field', () => {
1011+
// TODO: not technically a OneOf error, as the OneOf validation assumes known fields
1012+
test('{ foo: 123, unknownField: 123 }', TestInputObject, [
1013+
{
1014+
error:
1015+
'Expected value of type "TestInputObject" not to include unknown field "unknownField", found: { foo: 123, unknownField: 123 }.',
1016+
path: [],
1017+
},
1018+
{
1019+
error:
1020+
'OneOf Input Object "TestInputObject" must specify exactly one key.',
1021+
path: [],
1022+
},
1023+
]);
1024+
});
1025+
1026+
it('returns error for a misspelled field', () => {
1027+
// TODO: not technically a OneOf error, as the OneOf validation assumes known fields
1028+
test('{ foo: 123, bart: 123 }', TestInputObject, [
1029+
{
1030+
error:
1031+
'Expected value of type "TestInputObject" not to include unknown field "bart". Did you mean "bar"? Found: { foo: 123, bart: 123 }.',
1032+
path: [],
1033+
},
1034+
{
1035+
error:
1036+
'OneOf Input Object "TestInputObject" must specify exactly one key.',
1037+
path: [],
1038+
},
1039+
]);
1040+
});
1041+
1042+
it('allows variables in an object, statically', () => {
1043+
test('{ foo: $var }', TestInputObject, []);
1044+
});
1045+
1046+
it('allows correct use of variables', () => {
1047+
testWithVariables(
1048+
'($var: Int)',
1049+
{ var: 123 },
1050+
'{ foo: $var }',
1051+
TestInputObject,
1052+
[],
1053+
);
1054+
});
1055+
1056+
it('returns error with variable provided a value of null', () => {
1057+
testWithVariables(
1058+
'($var: Int)',
1059+
{ var: null },
1060+
'{ foo: $var }',
1061+
TestInputObject,
1062+
[
1063+
{
1064+
error:
1065+
'Expected variable "$var" provided to field "foo" for OneOf Input Object type "TestInputObject" not to be null.',
1066+
path: [],
1067+
},
1068+
],
1069+
);
1070+
});
1071+
1072+
it('errors with missing variables as the additional field', () => {
1073+
// TODO: the duplicate errors here should be combined
1074+
testWithVariables('', {}, '{ foo: 123, bar: $var }', TestInputObject, [
1075+
{
1076+
error:
1077+
'Expected variable "$var" provided to field "bar" for OneOf Input Object type "TestInputObject" to provide a runtime value.',
1078+
path: [],
1079+
},
1080+
{
1081+
error:
1082+
'OneOf Input Object "TestInputObject" must specify exactly one key.',
1083+
path: [],
1084+
},
1085+
]);
1086+
// TODO: the duplicate errors here should be combined
1087+
testWithVariables(
1088+
'($var: Int)',
1089+
{},
1090+
'{ foo: 123, bar: $var }',
1091+
TestInputObject,
1092+
[
1093+
{
1094+
error:
1095+
'Expected variable "$var" provided to field "bar" for OneOf Input Object type "TestInputObject" to provide a runtime value.',
1096+
path: [],
1097+
},
1098+
{
1099+
error:
1100+
'OneOf Input Object "TestInputObject" must specify exactly one key.',
1101+
path: [],
1102+
},
1103+
],
1104+
);
1105+
});
1106+
});
1107+
8521108
describe('for GraphQLList', () => {
8531109
const TestList = new GraphQLList(GraphQLInt);
8541110

0 commit comments

Comments
 (0)