Skip to content

Commit f462347

Browse files
valueFromAST-test: improve readability (#2347)
1 parent 6c377ac commit f462347

File tree

1 file changed

+122
-93
lines changed

1 file changed

+122
-93
lines changed

src/utilities/__tests__/valueFromAST-test.js

Lines changed: 122 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,59 @@ import {
2121
import { valueFromAST } from '../valueFromAST';
2222

2323
describe('valueFromAST', () => {
24-
function testCase(type, valueText, expected) {
25-
expect(valueFromAST(parseValue(valueText), type)).to.deep.equal(expected);
26-
}
27-
28-
function testCaseWithVars(variables, type, valueText, expected) {
29-
expect(valueFromAST(parseValue(valueText), type, variables)).to.deep.equal(
30-
expected,
31-
);
24+
function expectValueFrom(valueText, type, variables) {
25+
const ast = parseValue(valueText);
26+
const value = valueFromAST(ast, type, variables);
27+
return expect(value);
3228
}
3329

3430
it('rejects empty input', () => {
3531
expect(valueFromAST(null, GraphQLBoolean)).to.deep.equal(undefined);
3632
});
3733

3834
it('converts according to input coercion rules', () => {
39-
testCase(GraphQLBoolean, 'true', true);
40-
testCase(GraphQLBoolean, 'false', false);
41-
testCase(GraphQLInt, '123', 123);
42-
testCase(GraphQLFloat, '123', 123);
43-
testCase(GraphQLFloat, '123.456', 123.456);
44-
testCase(GraphQLString, '"abc123"', 'abc123');
45-
testCase(GraphQLID, '123456', '123456');
46-
testCase(GraphQLID, '"123456"', '123456');
35+
expectValueFrom('true', GraphQLBoolean).to.equal(true);
36+
expectValueFrom('false', GraphQLBoolean).to.equal(false);
37+
expectValueFrom('123', GraphQLInt).to.equal(123);
38+
expectValueFrom('123', GraphQLFloat).to.equal(123);
39+
expectValueFrom('123.456', GraphQLFloat).to.equal(123.456);
40+
expectValueFrom('"abc123"', GraphQLString).to.equal('abc123');
41+
expectValueFrom('123456', GraphQLID).to.equal('123456');
42+
expectValueFrom('"123456"', GraphQLID).to.equal('123456');
4743
});
4844

4945
it('does not convert when input coercion rules reject a value', () => {
50-
testCase(GraphQLBoolean, '123', undefined);
51-
testCase(GraphQLInt, '123.456', undefined);
52-
testCase(GraphQLInt, 'true', undefined);
53-
testCase(GraphQLInt, '"123"', undefined);
54-
testCase(GraphQLFloat, '"123"', undefined);
55-
testCase(GraphQLString, '123', undefined);
56-
testCase(GraphQLString, 'true', undefined);
57-
testCase(GraphQLID, '123.456', undefined);
58-
});
59-
60-
const testEnum = new GraphQLEnumType({
61-
name: 'TestColor',
62-
values: {
63-
RED: { value: 1 },
64-
GREEN: { value: 2 },
65-
BLUE: { value: 3 },
66-
NULL: { value: null },
67-
NAN: { value: NaN },
68-
NO_CUSTOM_VALUE: { value: undefined },
69-
},
46+
expectValueFrom('123', GraphQLBoolean).to.equal(undefined);
47+
expectValueFrom('123.456', GraphQLInt).to.equal(undefined);
48+
expectValueFrom('true', GraphQLInt).to.equal(undefined);
49+
expectValueFrom('"123"', GraphQLInt).to.equal(undefined);
50+
expectValueFrom('"123"', GraphQLFloat).to.equal(undefined);
51+
expectValueFrom('123', GraphQLString).to.equal(undefined);
52+
expectValueFrom('true', GraphQLString).to.equal(undefined);
53+
expectValueFrom('123.456', GraphQLString).to.equal(undefined);
7054
});
7155

7256
it('converts enum values according to input coercion rules', () => {
73-
testCase(testEnum, 'RED', 1);
74-
testCase(testEnum, 'BLUE', 3);
75-
testCase(testEnum, '3', undefined);
76-
testCase(testEnum, '"BLUE"', undefined);
77-
testCase(testEnum, 'null', null);
78-
testCase(testEnum, 'NULL', null);
79-
testCase(testEnum, 'NAN', NaN);
80-
testCase(testEnum, 'NO_CUSTOM_VALUE', 'NO_CUSTOM_VALUE');
57+
const testEnum = new GraphQLEnumType({
58+
name: 'TestColor',
59+
values: {
60+
RED: { value: 1 },
61+
GREEN: { value: 2 },
62+
BLUE: { value: 3 },
63+
NULL: { value: null },
64+
NAN: { value: NaN },
65+
NO_CUSTOM_VALUE: { value: undefined },
66+
},
67+
});
68+
69+
expectValueFrom('RED', testEnum).to.equal(1);
70+
expectValueFrom('BLUE', testEnum).to.equal(3);
71+
expectValueFrom('3', testEnum).to.equal(undefined);
72+
expectValueFrom('"BLUE"', testEnum).to.equal(undefined);
73+
expectValueFrom('null', testEnum).to.equal(null);
74+
expectValueFrom('NULL', testEnum).to.equal(null);
75+
expectValueFrom('NAN', testEnum).to.deep.equal(NaN);
76+
expectValueFrom('NO_CUSTOM_VALUE', testEnum).to.equal('NO_CUSTOM_VALUE');
8177
});
8278

8379
// Boolean!
@@ -92,45 +88,61 @@ describe('valueFromAST', () => {
9288
const nonNullListOfNonNullBool = GraphQLNonNull(listOfNonNullBool);
9389

9490
it('coerces to null unless non-null', () => {
95-
testCase(GraphQLBoolean, 'null', null);
96-
testCase(nonNullBool, 'null', undefined);
91+
expectValueFrom('null', GraphQLBoolean).to.equal(null);
92+
expectValueFrom('null', nonNullBool).to.equal(undefined);
9793
});
9894

9995
it('coerces lists of values', () => {
100-
testCase(listOfBool, 'true', [true]);
101-
testCase(listOfBool, '123', undefined);
102-
testCase(listOfBool, 'null', null);
103-
testCase(listOfBool, '[true, false]', [true, false]);
104-
testCase(listOfBool, '[true, 123]', undefined);
105-
testCase(listOfBool, '[true, null]', [true, null]);
106-
testCase(listOfBool, '{ true: true }', undefined);
96+
expectValueFrom('true', listOfBool).to.deep.equal([true]);
97+
expectValueFrom('123', listOfBool).to.equal(undefined);
98+
expectValueFrom('null', listOfBool).to.equal(null);
99+
expectValueFrom('[true, false]', listOfBool).to.deep.equal([true, false]);
100+
expectValueFrom('[true, 123]', listOfBool).to.equal(undefined);
101+
expectValueFrom('[true, null]', listOfBool).to.deep.equal([true, null]);
102+
expectValueFrom('{ true: true }', listOfBool).to.equal(undefined);
107103
});
108104

109105
it('coerces non-null lists of values', () => {
110-
testCase(nonNullListOfBool, 'true', [true]);
111-
testCase(nonNullListOfBool, '123', undefined);
112-
testCase(nonNullListOfBool, 'null', undefined);
113-
testCase(nonNullListOfBool, '[true, false]', [true, false]);
114-
testCase(nonNullListOfBool, '[true, 123]', undefined);
115-
testCase(nonNullListOfBool, '[true, null]', [true, null]);
106+
expectValueFrom('true', nonNullListOfBool).to.deep.equal([true]);
107+
expectValueFrom('123', nonNullListOfBool).to.equal(undefined);
108+
expectValueFrom('null', nonNullListOfBool).to.equal(undefined);
109+
expectValueFrom('[true, false]', nonNullListOfBool).to.deep.equal([
110+
true,
111+
false,
112+
]);
113+
expectValueFrom('[true, 123]', nonNullListOfBool).to.equal(undefined);
114+
expectValueFrom('[true, null]', nonNullListOfBool).to.deep.equal([
115+
true,
116+
null,
117+
]);
116118
});
117119

118120
it('coerces lists of non-null values', () => {
119-
testCase(listOfNonNullBool, 'true', [true]);
120-
testCase(listOfNonNullBool, '123', undefined);
121-
testCase(listOfNonNullBool, 'null', null);
122-
testCase(listOfNonNullBool, '[true, false]', [true, false]);
123-
testCase(listOfNonNullBool, '[true, 123]', undefined);
124-
testCase(listOfNonNullBool, '[true, null]', undefined);
121+
expectValueFrom('true', listOfNonNullBool).to.deep.equal([true]);
122+
expectValueFrom('123', listOfNonNullBool).to.equal(undefined);
123+
expectValueFrom('null', listOfNonNullBool).to.equal(null);
124+
expectValueFrom('[true, false]', listOfNonNullBool).to.deep.equal([
125+
true,
126+
false,
127+
]);
128+
expectValueFrom('[true, 123]', listOfNonNullBool).to.equal(undefined);
129+
expectValueFrom('[true, null]', listOfNonNullBool).to.equal(undefined);
125130
});
126131

127132
it('coerces non-null lists of non-null values', () => {
128-
testCase(nonNullListOfNonNullBool, 'true', [true]);
129-
testCase(nonNullListOfNonNullBool, '123', undefined);
130-
testCase(nonNullListOfNonNullBool, 'null', undefined);
131-
testCase(nonNullListOfNonNullBool, '[true, false]', [true, false]);
132-
testCase(nonNullListOfNonNullBool, '[true, 123]', undefined);
133-
testCase(nonNullListOfNonNullBool, '[true, null]', undefined);
133+
expectValueFrom('true', nonNullListOfNonNullBool).to.deep.equal([true]);
134+
expectValueFrom('123', nonNullListOfNonNullBool).to.equal(undefined);
135+
expectValueFrom('null', nonNullListOfNonNullBool).to.equal(undefined);
136+
expectValueFrom('[true, false]', nonNullListOfNonNullBool).to.deep.equal([
137+
true,
138+
false,
139+
]);
140+
expectValueFrom('[true, 123]', nonNullListOfNonNullBool).to.equal(
141+
undefined,
142+
);
143+
expectValueFrom('[true, null]', nonNullListOfNonNullBool).to.equal(
144+
undefined,
145+
);
134146
});
135147

136148
const testInputObj = new GraphQLInputObjectType({
@@ -143,48 +155,65 @@ describe('valueFromAST', () => {
143155
});
144156

145157
it('coerces input objects according to input coercion rules', () => {
146-
testCase(testInputObj, 'null', null);
147-
testCase(testInputObj, '123', undefined);
148-
testCase(testInputObj, '[]', undefined);
149-
testCase(testInputObj, '{ int: 123, requiredBool: false }', {
158+
expectValueFrom('null', testInputObj).to.equal(null);
159+
expectValueFrom('123', testInputObj).to.equal(undefined);
160+
expectValueFrom('[]', testInputObj).to.equal(undefined);
161+
expectValueFrom(
162+
'{ int: 123, requiredBool: false }',
163+
testInputObj,
164+
).to.deep.equal({
150165
int: 123,
151166
requiredBool: false,
152167
});
153-
testCase(testInputObj, '{ bool: true, requiredBool: false }', {
168+
expectValueFrom(
169+
'{ bool: true, requiredBool: false }',
170+
testInputObj,
171+
).to.deep.equal({
154172
int: 42,
155173
bool: true,
156174
requiredBool: false,
157175
});
158-
testCase(testInputObj, '{ int: true, requiredBool: true }', undefined);
159-
testCase(testInputObj, '{ requiredBool: null }', undefined);
160-
testCase(testInputObj, '{ bool: true }', undefined);
176+
expectValueFrom('{ int: true, requiredBool: true }', testInputObj).to.equal(
177+
undefined,
178+
);
179+
expectValueFrom('{ requiredBool: null }', testInputObj).to.equal(undefined);
180+
expectValueFrom('{ bool: true }', testInputObj).to.equal(undefined);
161181
});
162182

163183
it('accepts variable values assuming already coerced', () => {
164-
testCaseWithVars({}, GraphQLBoolean, '$var', undefined);
165-
testCaseWithVars({ var: true }, GraphQLBoolean, '$var', true);
166-
testCaseWithVars({ var: null }, GraphQLBoolean, '$var', null);
184+
expectValueFrom('$var', GraphQLBoolean, {}).to.equal(undefined);
185+
expectValueFrom('$var', GraphQLBoolean, { var: true }).to.equal(true);
186+
expectValueFrom('$var', GraphQLBoolean, { var: null }).to.equal(null);
167187
});
168188

169189
it('asserts variables are provided as items in lists', () => {
170-
testCaseWithVars({}, listOfBool, '[ $foo ]', [null]);
171-
testCaseWithVars({}, listOfNonNullBool, '[ $foo ]', undefined);
172-
testCaseWithVars({ foo: true }, listOfNonNullBool, '[ $foo ]', [true]);
190+
expectValueFrom('[ $foo ]', listOfBool, {}).to.deep.equal([null]);
191+
expectValueFrom('[ $foo ]', listOfNonNullBool, {}).to.equal(undefined);
192+
expectValueFrom('[ $foo ]', listOfNonNullBool, {
193+
foo: true,
194+
}).to.deep.equal([true]);
173195
// Note: variables are expected to have already been coerced, so we
174196
// do not expect the singleton wrapping behavior for variables.
175-
testCaseWithVars({ foo: true }, listOfNonNullBool, '$foo', true);
176-
testCaseWithVars({ foo: [true] }, listOfNonNullBool, '$foo', [true]);
197+
expectValueFrom('$foo', listOfNonNullBool, { foo: true }).to.equal(true);
198+
expectValueFrom('$foo', listOfNonNullBool, { foo: [true] }).to.deep.equal([
199+
true,
200+
]);
177201
});
178202

179203
it('omits input object fields for unprovided variables', () => {
180-
testCaseWithVars(
181-
{},
182-
testInputObj,
204+
expectValueFrom(
183205
'{ int: $foo, bool: $foo, requiredBool: true }',
184-
{ int: 42, requiredBool: true },
206+
testInputObj,
207+
{},
208+
).to.deep.equal({ int: 42, requiredBool: true });
209+
210+
expectValueFrom('{ requiredBool: $foo }', testInputObj, {}).to.equal(
211+
undefined,
185212
);
186-
testCaseWithVars({}, testInputObj, '{ requiredBool: $foo }', undefined);
187-
testCaseWithVars({ foo: true }, testInputObj, '{ requiredBool: $foo }', {
213+
214+
expectValueFrom('{ requiredBool: $foo }', testInputObj, {
215+
foo: true,
216+
}).to.deep.equal({
188217
int: 42,
189218
requiredBool: true,
190219
});

0 commit comments

Comments
 (0)