|
| 1 | +/** |
| 2 | + * Copyright (c) 2017, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it } from 'mocha'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import { scalarValueFromAST } from '../scalarValueFromAST'; |
| 13 | +import { parseValue } from '../../language'; |
| 14 | + |
| 15 | +describe('scalarValueFromAST', () => { |
| 16 | + |
| 17 | + function testCase(valueText, expected) { |
| 18 | + expect( |
| 19 | + scalarValueFromAST(parseValue(valueText)) |
| 20 | + ).to.deep.equal(expected); |
| 21 | + } |
| 22 | + |
| 23 | + function testNegativeCase(valueText, errorMsg) { |
| 24 | + expect( |
| 25 | + () => scalarValueFromAST(parseValue(valueText)) |
| 26 | + ).to.throw(errorMsg); |
| 27 | + } |
| 28 | + |
| 29 | + it('parses simple values', () => { |
| 30 | + testCase('null', null); |
| 31 | + testCase('true', true); |
| 32 | + testCase('false', false); |
| 33 | + testCase('123', 123); |
| 34 | + testCase('123.456', 123.456); |
| 35 | + testCase('"abc123"', 'abc123'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('parses lists of values', () => { |
| 39 | + testCase('[true, false]', [ true, false ]); |
| 40 | + testCase('[true, 123.45]', [ true, 123.45 ]); |
| 41 | + testCase('[true, null]', [ true, null ]); |
| 42 | + testCase('[true, ["foo", 1.2]]', [ true, [ 'foo', 1.2 ] ]); |
| 43 | + }); |
| 44 | + |
| 45 | + it('parses input objects', () => { |
| 46 | + testCase( |
| 47 | + '{ int: 123, requiredBool: false }', |
| 48 | + { int: 123, requiredBool: false } |
| 49 | + ); |
| 50 | + testCase( |
| 51 | + '{ foo: [{ bar: "baz"}]}', |
| 52 | + { foo: [ { bar: 'baz'} ] } |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('rejects enum values and query variables', () => { |
| 57 | + testNegativeCase('TEST_ENUM_VALUE', 'Scalar value can not contain Enum.'); |
| 58 | + testNegativeCase( |
| 59 | + '$test_variable', |
| 60 | + 'Scalar value can not contain Query variable.' |
| 61 | + ); |
| 62 | + testNegativeCase('[TEST_ENUM_VALUE]', 'Scalar value can not contain Enum.'); |
| 63 | + testNegativeCase( |
| 64 | + '[$test_variable]', |
| 65 | + 'Scalar value can not contain Query variable.' |
| 66 | + ); |
| 67 | + testNegativeCase( |
| 68 | + '{foo: TEST_ENUM_VALUE}', |
| 69 | + 'Scalar value can not contain Enum.' |
| 70 | + ); |
| 71 | + testNegativeCase( |
| 72 | + '{bar: $test_variable}', |
| 73 | + 'Scalar value can not contain Query variable.' |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | +}); |
0 commit comments