Skip to content

Commit 166317a

Browse files
test: Fully cover 'coerceValue' function with tests (#2045)
1 parent a6f822e commit 166317a

File tree

1 file changed

+72
-141
lines changed

1 file changed

+72
-141
lines changed

src/utilities/__tests__/coerceValue-test.js

Lines changed: 72 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import { describe, it } from 'mocha';
44
import { expect } from 'chai';
5-
import { coerceValue } from '../coerceValue';
5+
6+
import invariant from '../../jsutils/invariant';
67
import {
7-
GraphQLID,
88
GraphQLInt,
9-
GraphQLFloat,
109
GraphQLList,
11-
GraphQLString,
10+
GraphQLNonNull,
11+
GraphQLScalarType,
1212
GraphQLEnumType,
1313
GraphQLInputObjectType,
14-
GraphQLNonNull,
1514
} from '../../type';
15+
import { coerceValue } from '../coerceValue';
1616

1717
function expectValue(result) {
1818
expect(result.errors).to.equal(undefined);
@@ -26,162 +26,61 @@ function expectErrors(result) {
2626
}
2727

2828
describe('coerceValue', () => {
29-
describe('for GraphQLString', () => {
30-
it('returns error for array input as string', () => {
31-
const result = coerceValue([1, 2, 3], GraphQLString);
32-
expectErrors(result).to.deep.equal([
33-
'Expected type String. String cannot represent a non string value: [1, 2, 3]',
34-
]);
35-
});
36-
});
37-
38-
describe('for GraphQLID', () => {
39-
it('returns error for array input as ID', () => {
40-
const result = coerceValue([1, 2, 3], GraphQLID);
41-
expectErrors(result).to.deep.equal([
42-
'Expected type ID. ID cannot represent value: [1, 2, 3]',
43-
]);
44-
});
45-
});
29+
describe('for GraphQLNonNull', () => {
30+
const TestNonNull = new GraphQLNonNull(GraphQLInt);
4631

47-
describe('for GraphQLInt', () => {
48-
it('returns value for integer', () => {
49-
const result = coerceValue(1, GraphQLInt);
32+
it('returns no error for non-null value', () => {
33+
const result = coerceValue(1, TestNonNull);
5034
expectValue(result).to.equal(1);
5135
});
5236

53-
it('returns error for numeric looking string', () => {
54-
const result = coerceValue('1', GraphQLInt);
55-
expectErrors(result).to.deep.equal([
56-
'Expected type Int. Int cannot represent non-integer value: "1"',
57-
]);
58-
});
59-
60-
it('returns value for negative int input', () => {
61-
const result = coerceValue(-1, GraphQLInt);
62-
expectValue(result).to.equal(-1);
63-
});
64-
65-
it('returns value for exponent input', () => {
66-
const result = coerceValue(1e3, GraphQLInt);
67-
expectValue(result).to.equal(1000);
68-
});
69-
70-
it('returns null for null value', () => {
71-
const result = coerceValue(null, GraphQLInt);
72-
expectValue(result).to.equal(null);
73-
});
74-
75-
it('returns a single error for empty string as value', () => {
76-
const result = coerceValue('', GraphQLInt);
77-
expectErrors(result).to.deep.equal([
78-
'Expected type Int. Int cannot represent non-integer value: ""',
79-
]);
80-
});
81-
82-
it('returns a single error for 2^32 input as int', () => {
83-
const result = coerceValue(Math.pow(2, 32), GraphQLInt);
84-
expectErrors(result).to.deep.equal([
85-
'Expected type Int. Int cannot represent non 32-bit signed integer value: 4294967296',
86-
]);
87-
});
88-
89-
it('returns a single error for float input as int', () => {
90-
const result = coerceValue(1.5, GraphQLInt);
91-
expectErrors(result).to.deep.equal([
92-
'Expected type Int. Int cannot represent non-integer value: 1.5',
93-
]);
94-
});
95-
96-
it('returns a single error for NaN input as int', () => {
97-
const result = coerceValue(NaN, GraphQLInt);
98-
expectErrors(result).to.deep.equal([
99-
'Expected type Int. Int cannot represent non-integer value: NaN',
100-
]);
101-
});
102-
103-
it('returns a single error for Infinity input as int', () => {
104-
const result = coerceValue(Infinity, GraphQLInt);
105-
expectErrors(result).to.deep.equal([
106-
'Expected type Int. Int cannot represent non-integer value: Infinity',
107-
]);
108-
});
109-
110-
it('returns a single error for char input', () => {
111-
const result = coerceValue('a', GraphQLInt);
37+
it('returns an error for undefined value', () => {
38+
const result = coerceValue(undefined, TestNonNull);
11239
expectErrors(result).to.deep.equal([
113-
'Expected type Int. Int cannot represent non-integer value: "a"',
40+
'Expected non-nullable type Int! not to be null.',
11441
]);
11542
});
11643

117-
it('returns a single error for string input', () => {
118-
const result = coerceValue('meow', GraphQLInt);
44+
it('returns an error for null value', () => {
45+
const result = coerceValue(null, TestNonNull);
11946
expectErrors(result).to.deep.equal([
120-
'Expected type Int. Int cannot represent non-integer value: "meow"',
47+
'Expected non-nullable type Int! not to be null.',
12148
]);
12249
});
12350
});
12451

125-
describe('for GraphQLFloat', () => {
126-
it('returns value for integer', () => {
127-
const result = coerceValue(1, GraphQLFloat);
128-
expectValue(result).to.equal(1);
129-
});
130-
131-
it('returns value for decimal', () => {
132-
const result = coerceValue(1.1, GraphQLFloat);
133-
expectValue(result).to.equal(1.1);
134-
});
135-
136-
it('returns value for exponent input', () => {
137-
const result = coerceValue(1e3, GraphQLFloat);
138-
expectValue(result).to.equal(1000);
52+
describe('for GraphQLScalar', () => {
53+
const TestScalar = new GraphQLScalarType({
54+
name: 'TestScalar',
55+
parseValue(input) {
56+
invariant(typeof input === 'object' && input !== null);
57+
if (input.error != null) {
58+
throw input.error;
59+
}
60+
return input.value;
61+
},
13962
});
14063

141-
it('returns error for numeric looking string', () => {
142-
const result = coerceValue('1', GraphQLFloat);
143-
expectErrors(result).to.deep.equal([
144-
'Expected type Float. Float cannot represent non numeric value: "1"',
145-
]);
64+
it('returns no error for valid input', () => {
65+
const result = coerceValue({ value: 1 }, TestScalar);
66+
expectValue(result).to.equal(1);
14667
});
14768

148-
it('returns null for null value', () => {
149-
const result = coerceValue(null, GraphQLFloat);
69+
it('returns no error for null result', () => {
70+
const result = coerceValue({ value: null }, TestScalar);
15071
expectValue(result).to.equal(null);
15172
});
15273

153-
it('returns a single error for empty string input', () => {
154-
const result = coerceValue('', GraphQLFloat);
155-
expectErrors(result).to.deep.equal([
156-
'Expected type Float. Float cannot represent non numeric value: ""',
157-
]);
158-
});
159-
160-
it('returns a single error for NaN input', () => {
161-
const result = coerceValue(NaN, GraphQLFloat);
162-
expectErrors(result).to.deep.equal([
163-
'Expected type Float. Float cannot represent non numeric value: NaN',
164-
]);
165-
});
166-
167-
it('returns a single error for Infinity input', () => {
168-
const result = coerceValue(Infinity, GraphQLFloat);
169-
expectErrors(result).to.deep.equal([
170-
'Expected type Float. Float cannot represent non numeric value: Infinity',
171-
]);
172-
});
173-
174-
it('returns a single error for char input', () => {
175-
const result = coerceValue('a', GraphQLFloat);
176-
expectErrors(result).to.deep.equal([
177-
'Expected type Float. Float cannot represent non numeric value: "a"',
178-
]);
74+
it('returns an error for undefined result', () => {
75+
const result = coerceValue({ value: undefined }, TestScalar);
76+
expectErrors(result).to.deep.equal(['Expected type TestScalar.']);
17977
});
18078

181-
it('returns a single error for char input', () => {
182-
const result = coerceValue('meow', GraphQLFloat);
79+
it('returns an error for undefined result', () => {
80+
const error = new Error('Some error message');
81+
const result = coerceValue({ error }, TestScalar);
18382
expectErrors(result).to.deep.equal([
184-
'Expected type Float. Float cannot represent non numeric value: "meow"',
83+
'Expected type TestScalar. Some error message',
18584
]);
18685
});
18786
});
@@ -203,14 +102,14 @@ describe('coerceValue', () => {
203102
expectValue(barResult).to.equal(123456789);
204103
});
205104

206-
it('results error for misspelled enum value', () => {
105+
it('returns an error for misspelled enum value', () => {
207106
const result = coerceValue('foo', TestEnum);
208107
expectErrors(result).to.deep.equal([
209108
'Expected type TestEnum. Did you mean FOO?',
210109
]);
211110
});
212111

213-
it('results error for incorrect value type', () => {
112+
it('returns an error for incorrect value type', () => {
214113
const result1 = coerceValue(123, TestEnum);
215114
expectErrors(result1).to.deep.equal(['Expected type TestEnum.']);
216115

@@ -280,6 +179,31 @@ describe('coerceValue', () => {
280179
});
281180
});
282181

182+
describe('for GraphQLInputObject with default value', () => {
183+
const TestInputObject = defaultValue =>
184+
new GraphQLInputObjectType({
185+
name: 'TestInputObject',
186+
fields: {
187+
foo: { type: GraphQLInt, defaultValue },
188+
},
189+
});
190+
191+
it('returns no errors for valid input value', () => {
192+
const result = coerceValue({ foo: 5 }, TestInputObject(7));
193+
expectValue(result).to.deep.equal({ foo: 5 });
194+
});
195+
196+
it('returns object with default value', () => {
197+
const result = coerceValue({}, TestInputObject(7));
198+
expectValue(result).to.deep.equal({ foo: 7 });
199+
});
200+
201+
it('returns null as value', () => {
202+
const result = coerceValue({}, TestInputObject(7));
203+
expectValue(result).to.deep.equal({ foo: 7 });
204+
});
205+
});
206+
283207
describe('for GraphQLList', () => {
284208
const TestList = GraphQLList(GraphQLInt);
285209

@@ -289,7 +213,7 @@ describe('coerceValue', () => {
289213
});
290214

291215
it('returns an error for an invalid input', () => {
292-
const result = coerceValue([1, 'b', true], TestList);
216+
const result = coerceValue([1, 'b', true, 4], TestList);
293217
expectErrors(result).to.deep.equal([
294218
'Expected type Int at value[1]. Int cannot represent non-integer value: "b"',
295219
'Expected type Int at value[2]. Int cannot represent non-integer value: true',
@@ -301,6 +225,13 @@ describe('coerceValue', () => {
301225
expectValue(result).to.deep.equal([42]);
302226
});
303227

228+
it('returns an error for a non-list invalid value', () => {
229+
const result = coerceValue('INVALID', TestList);
230+
expectErrors(result).to.deep.equal([
231+
'Expected type Int. Int cannot represent non-integer value: "INVALID"',
232+
]);
233+
});
234+
304235
it('returns null for a null value', () => {
305236
const result = coerceValue(null, TestList);
306237
expectValue(result).to.deep.equal(null);

0 commit comments

Comments
 (0)