|
8 | 8 | import { describe, it } from 'mocha';
|
9 | 9 | import { expect } from 'chai';
|
10 | 10 | import { coerceValue } from '../coerceValue';
|
11 |
| -import { GraphQLInt, GraphQLFloat, GraphQLString } from '../../type'; |
| 11 | +import { |
| 12 | + GraphQLInt, |
| 13 | + GraphQLFloat, |
| 14 | + GraphQLString, |
| 15 | + GraphQLEnumType, |
| 16 | + GraphQLInputObjectType, |
| 17 | + GraphQLNonNull, |
| 18 | +} from '../../type'; |
12 | 19 |
|
13 | 20 | function expectNoErrors(result) {
|
14 | 21 | expect(result.errors).to.equal(undefined);
|
| 22 | + expect(result.value).not.to.equal(undefined); |
15 | 23 | }
|
16 | 24 |
|
17 | 25 | function expectError(result, expected) {
|
18 | 26 | const messages = result.errors && result.errors.map(error => error.message);
|
19 | 27 | expect(messages).to.deep.equal([expected]);
|
| 28 | + expect(result.value).to.equal(undefined); |
20 | 29 | }
|
21 | 30 |
|
22 | 31 | describe('coerceValue', () => {
|
@@ -130,4 +139,103 @@ describe('coerceValue', () => {
|
130 | 139 | );
|
131 | 140 | });
|
132 | 141 | });
|
| 142 | + |
| 143 | + describe('for GraphQLEnum', () => { |
| 144 | + const TestEnum = new GraphQLEnumType({ |
| 145 | + name: 'TestEnum', |
| 146 | + values: { |
| 147 | + FOO: { value: 'InternalFoo' }, |
| 148 | + BAR: { value: 123456789 }, |
| 149 | + }, |
| 150 | + }); |
| 151 | + |
| 152 | + it('returns no error for a known enum name', () => { |
| 153 | + const fooResult = coerceValue('FOO', TestEnum); |
| 154 | + expectNoErrors(fooResult); |
| 155 | + expect(fooResult.value).to.equal('InternalFoo'); |
| 156 | + |
| 157 | + const barResult = coerceValue('BAR', TestEnum); |
| 158 | + expectNoErrors(barResult); |
| 159 | + expect(barResult.value).to.equal(123456789); |
| 160 | + }); |
| 161 | + |
| 162 | + it('results error for misspelled enum value', () => { |
| 163 | + const result = coerceValue('foo', TestEnum); |
| 164 | + expectError(result, 'Expected type TestEnum; did you mean FOO?'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('results error for incorrect value type', () => { |
| 168 | + const result1 = coerceValue(123, TestEnum); |
| 169 | + expectError(result1, 'Expected type TestEnum.'); |
| 170 | + |
| 171 | + const result2 = coerceValue({ field: 'value' }, TestEnum); |
| 172 | + expectError(result2, 'Expected type TestEnum.'); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('for GraphQLInputObject', () => { |
| 177 | + const TestInputObject = new GraphQLInputObjectType({ |
| 178 | + name: 'TestInputObject', |
| 179 | + fields: { |
| 180 | + foo: { type: GraphQLNonNull(GraphQLInt) }, |
| 181 | + bar: { type: GraphQLInt }, |
| 182 | + }, |
| 183 | + }); |
| 184 | + |
| 185 | + it('returns no error for a valid input', () => { |
| 186 | + const result = coerceValue({ foo: 123 }, TestInputObject); |
| 187 | + expectNoErrors(result); |
| 188 | + expect(result.value).to.deep.equal({ foo: 123 }); |
| 189 | + }); |
| 190 | + |
| 191 | + it('returns no error for a non-object type', () => { |
| 192 | + const result = coerceValue(123, TestInputObject); |
| 193 | + expectError(result, 'Expected type TestInputObject to be an object.'); |
| 194 | + }); |
| 195 | + |
| 196 | + it('returns no error for an invalid field', () => { |
| 197 | + const result = coerceValue({ foo: 'abc' }, TestInputObject); |
| 198 | + expectError( |
| 199 | + result, |
| 200 | + 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc', |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it('returns multiple errors for multiple invalid fields', () => { |
| 205 | + const result = coerceValue({ foo: 'abc', bar: 'def' }, TestInputObject); |
| 206 | + expect( |
| 207 | + result.errors && result.errors.map(error => error.message), |
| 208 | + ).to.deep.equal([ |
| 209 | + 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc', |
| 210 | + 'Expected type Int at value.bar; Int cannot represent non 32-bit signed integer value: def', |
| 211 | + ]); |
| 212 | + }); |
| 213 | + |
| 214 | + it('returns error for a missing required field', () => { |
| 215 | + const result = coerceValue({ bar: 123 }, TestInputObject); |
| 216 | + expectError( |
| 217 | + result, |
| 218 | + 'Field value.foo of required type Int! was not provided.', |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + it('returns error for an unknown field', () => { |
| 223 | + const result = coerceValue( |
| 224 | + { foo: 123, unknownField: 123 }, |
| 225 | + TestInputObject, |
| 226 | + ); |
| 227 | + expectError( |
| 228 | + result, |
| 229 | + 'Field "unknownField" is not defined by type TestInputObject.', |
| 230 | + ); |
| 231 | + }); |
| 232 | + |
| 233 | + it('returns error for a misspelled field', () => { |
| 234 | + const result = coerceValue({ foo: 123, bart: 123 }, TestInputObject); |
| 235 | + expectError( |
| 236 | + result, |
| 237 | + 'Field "bart" is not defined by type TestInputObject; did you mean bar?', |
| 238 | + ); |
| 239 | + }); |
| 240 | + }); |
133 | 241 | });
|
0 commit comments