2
2
3
3
import { describe , it } from 'mocha' ;
4
4
import { expect } from 'chai' ;
5
- import { coerceValue } from '../coerceValue' ;
5
+
6
+ import invariant from '../../jsutils/invariant' ;
6
7
import {
7
- GraphQLID ,
8
8
GraphQLInt ,
9
- GraphQLFloat ,
10
9
GraphQLList ,
11
- GraphQLString ,
10
+ GraphQLNonNull ,
11
+ GraphQLScalarType ,
12
12
GraphQLEnumType ,
13
13
GraphQLInputObjectType ,
14
- GraphQLNonNull ,
15
14
} from '../../type' ;
15
+ import { coerceValue } from '../coerceValue' ;
16
16
17
17
function expectValue ( result ) {
18
18
expect ( result . errors ) . to . equal ( undefined ) ;
@@ -26,162 +26,61 @@ function expectErrors(result) {
26
26
}
27
27
28
28
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 ) ;
46
31
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 ) ;
50
34
expectValue ( result ) . to . equal ( 1 ) ;
51
35
} ) ;
52
36
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 ) ;
112
39
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. ' ,
114
41
] ) ;
115
42
} ) ;
116
43
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 ) ;
119
46
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. ' ,
121
48
] ) ;
122
49
} ) ;
123
50
} ) ;
124
51
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
+ } ,
139
62
} ) ;
140
63
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 ) ;
146
67
} ) ;
147
68
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 ) ;
150
71
expectValue ( result ) . to . equal ( null ) ;
151
72
} ) ;
152
73
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.' ] ) ;
179
77
} ) ;
180
78
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 ) ;
183
82
expectErrors ( result ) . to . deep . equal ( [
184
- 'Expected type Float. Float cannot represent non numeric value: "meow" ' ,
83
+ 'Expected type TestScalar. Some error message ' ,
185
84
] ) ;
186
85
} ) ;
187
86
} ) ;
@@ -203,14 +102,14 @@ describe('coerceValue', () => {
203
102
expectValue ( barResult ) . to . equal ( 123456789 ) ;
204
103
} ) ;
205
104
206
- it ( 'results error for misspelled enum value' , ( ) => {
105
+ it ( 'returns an error for misspelled enum value' , ( ) => {
207
106
const result = coerceValue ( 'foo' , TestEnum ) ;
208
107
expectErrors ( result ) . to . deep . equal ( [
209
108
'Expected type TestEnum. Did you mean FOO?' ,
210
109
] ) ;
211
110
} ) ;
212
111
213
- it ( 'results error for incorrect value type' , ( ) => {
112
+ it ( 'returns an error for incorrect value type' , ( ) => {
214
113
const result1 = coerceValue ( 123 , TestEnum ) ;
215
114
expectErrors ( result1 ) . to . deep . equal ( [ 'Expected type TestEnum.' ] ) ;
216
115
@@ -280,6 +179,31 @@ describe('coerceValue', () => {
280
179
} ) ;
281
180
} ) ;
282
181
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
+
283
207
describe ( 'for GraphQLList' , ( ) => {
284
208
const TestList = GraphQLList ( GraphQLInt ) ;
285
209
@@ -289,7 +213,7 @@ describe('coerceValue', () => {
289
213
} ) ;
290
214
291
215
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 ) ;
293
217
expectErrors ( result ) . to . deep . equal ( [
294
218
'Expected type Int at value[1]. Int cannot represent non-integer value: "b"' ,
295
219
'Expected type Int at value[2]. Int cannot represent non-integer value: true' ,
@@ -301,6 +225,13 @@ describe('coerceValue', () => {
301
225
expectValue ( result ) . to . deep . equal ( [ 42 ] ) ;
302
226
} ) ;
303
227
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
+
304
235
it ( 'returns null for a null value' , ( ) => {
305
236
const result = coerceValue ( null , TestList ) ;
306
237
expectValue ( result ) . to . deep . equal ( null ) ;
0 commit comments