Skip to content

Commit f06b6d9

Browse files
committed
Add test for enum custom values as input args
1 parent 4a9e37a commit f06b6d9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/execution/__tests__/variables-test.js

+57
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
GraphQLString,
1919
GraphQLNonNull,
2020
GraphQLScalarType,
21+
GraphQLEnumType,
2122
} from '../../type';
2223

2324
const TestComplexScalar = new GraphQLScalarType({
@@ -60,6 +61,18 @@ const TestNestedInputObject = new GraphQLInputObjectType({
6061
},
6162
});
6263

64+
const TestEnum = new GraphQLEnumType({
65+
name: 'TestEnum',
66+
values: {
67+
NULL: { value: null },
68+
UNDEFINED: { value: undefined },
69+
NAN: { value: NaN },
70+
FALSE: { value: false },
71+
CUSTOM: { value: 'custom value' },
72+
DEFAULT_VALUE: {},
73+
},
74+
});
75+
6376
function fieldWithInputArg(inputArg) {
6477
return {
6578
type: GraphQLString,
@@ -75,6 +88,10 @@ function fieldWithInputArg(inputArg) {
7588
const TestType = new GraphQLObjectType({
7689
name: 'TestType',
7790
fields: {
91+
fieldWithEnumInput: fieldWithInputArg({ type: TestEnum }),
92+
fieldWithNonNullableEnumInput: fieldWithInputArg({
93+
type: GraphQLNonNull(TestEnum),
94+
}),
7895
fieldWithObjectInput: fieldWithInputArg({ type: TestInputObject }),
7996
fieldWithNullableStringInput: fieldWithInputArg({ type: GraphQLString }),
8097
fieldWithNonNullableStringInput: fieldWithInputArg({
@@ -358,6 +375,46 @@ describe('Execute: Handles inputs', () => {
358375
});
359376
});
360377

378+
describe('Handles custom enum values', () => {
379+
it('allows custom enum values as inputs', () => {
380+
const result = executeQuery(`
381+
{
382+
null: fieldWithEnumInput(input: NULL)
383+
undefined: fieldWithEnumInput(input: UNDEFINED)
384+
NaN: fieldWithEnumInput(input: NAN)
385+
false: fieldWithEnumInput(input: FALSE)
386+
customValue: fieldWithEnumInput(input: CUSTOM)
387+
defaultValue: fieldWithEnumInput(input: DEFAULT_VALUE)
388+
}
389+
`);
390+
391+
expect(result).to.deep.equal({
392+
data: {
393+
null: 'null',
394+
undefined: 'undefined',
395+
NaN: 'NaN',
396+
false: 'false',
397+
customValue: "'custom value'",
398+
defaultValue: "'DEFAULT_VALUE'",
399+
},
400+
});
401+
});
402+
403+
it('allows non-nullable inputs to have null as enum custom value', () => {
404+
const result = executeQuery(`
405+
{
406+
fieldWithNonNullableEnumInput(input: NULL)
407+
}
408+
`);
409+
410+
expect(result).to.deep.equal({
411+
data: {
412+
fieldWithNonNullableEnumInput: 'null',
413+
},
414+
});
415+
});
416+
});
417+
361418
describe('Handles nullable scalars', () => {
362419
it('allows nullable inputs to be omitted', () => {
363420
const result = executeQuery(`

src/utilities/__tests__/valueFromAST-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ describe('valueFromAST', () => {
6666
BLUE: { value: 3 },
6767
NULL: { value: null },
6868
UNDEFINED: { value: undefined },
69+
NAN: { value: NaN },
6970
},
7071
});
7172

@@ -77,6 +78,7 @@ describe('valueFromAST', () => {
7778
testCase(testEnum, 'null', null);
7879
testCase(testEnum, 'NULL', null);
7980
testCase(testEnum, 'UNDEFINED', undefined);
81+
testCase(testEnum, 'NAN', NaN);
8082
});
8183

8284
// Boolean!

0 commit comments

Comments
 (0)