Skip to content

Commit 854b865

Browse files
committed
Add test for enum custom values as input args
1 parent 3473ca5 commit 854b865

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/execution/__tests__/variables-test.js

Lines changed: 55 additions & 0 deletions
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({
@@ -439,6 +456,44 @@ describe('Execute: Handles inputs', () => {
439456
});
440457
});
441458

459+
describe('Handles custom enum values', () => {
460+
it('allows custom enum values as inputs', () => {
461+
const result = executeQuery(`
462+
{
463+
null: fieldWithEnumInput(input: NULL)
464+
NaN: fieldWithEnumInput(input: NAN)
465+
false: fieldWithEnumInput(input: FALSE)
466+
customValue: fieldWithEnumInput(input: CUSTOM)
467+
defaultValue: fieldWithEnumInput(input: DEFAULT_VALUE)
468+
}
469+
`);
470+
471+
expect(result).to.deep.equal({
472+
data: {
473+
null: 'null',
474+
NaN: 'NaN',
475+
false: 'false',
476+
customValue: "'custom value'",
477+
defaultValue: "'DEFAULT_VALUE'",
478+
},
479+
});
480+
});
481+
482+
it('allows non-nullable inputs to have null as enum custom value', () => {
483+
const result = executeQuery(`
484+
{
485+
fieldWithNonNullableEnumInput(input: NULL)
486+
}
487+
`);
488+
489+
expect(result).to.deep.equal({
490+
data: {
491+
fieldWithNonNullableEnumInput: 'null',
492+
},
493+
});
494+
});
495+
});
496+
442497
describe('Handles nullable scalars', () => {
443498
it('allows nullable inputs to be omitted', () => {
444499
const result = executeQuery(`

src/utilities/__tests__/valueFromAST-test.js

Lines changed: 2 additions & 0 deletions
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)