|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it } from 'mocha'; |
| 11 | +import { expect } from 'chai'; |
| 12 | + |
| 13 | +import { GraphQLDirective, GraphQLString, GraphQLInt } from '../'; |
| 14 | + |
| 15 | +describe('Type System: Directive', () => { |
| 16 | + it('defines a directive with no args', () => { |
| 17 | + const directive = new GraphQLDirective({ |
| 18 | + name: 'Foo', |
| 19 | + locations: ['QUERY'], |
| 20 | + }); |
| 21 | + |
| 22 | + expect(directive).to.deep.include({ |
| 23 | + name: 'Foo', |
| 24 | + args: [], |
| 25 | + locations: ['QUERY'], |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('defines a directive with multiple args', () => { |
| 30 | + const directive = new GraphQLDirective({ |
| 31 | + name: 'Foo', |
| 32 | + args: { |
| 33 | + foo: { type: GraphQLString }, |
| 34 | + bar: { type: GraphQLInt }, |
| 35 | + }, |
| 36 | + locations: ['QUERY'], |
| 37 | + }); |
| 38 | + |
| 39 | + expect(directive).to.deep.include({ |
| 40 | + name: 'Foo', |
| 41 | + args: [ |
| 42 | + { |
| 43 | + name: 'foo', |
| 44 | + type: GraphQLString, |
| 45 | + description: null, |
| 46 | + defaultValue: undefined, |
| 47 | + astNode: undefined, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'bar', |
| 51 | + type: GraphQLInt, |
| 52 | + description: null, |
| 53 | + defaultValue: undefined, |
| 54 | + astNode: undefined, |
| 55 | + }, |
| 56 | + ], |
| 57 | + locations: ['QUERY'], |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('can be stringified and JSON.stringified', () => { |
| 62 | + const directive = new GraphQLDirective({ |
| 63 | + name: 'Foo', |
| 64 | + locations: ['QUERY'], |
| 65 | + }); |
| 66 | + |
| 67 | + expect(String(directive)).to.equal('@Foo'); |
| 68 | + expect(JSON.stringify(directive)).to.equal('"@Foo"'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('reject an unnamed directive', () => { |
| 72 | + // $DisableFlowOnNegativeTest |
| 73 | + expect(() => new GraphQLDirective({ locations: ['Query'] })).to.throw( |
| 74 | + 'Directive must be named.', |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it('reject directive incorrectly typed args', () => { |
| 79 | + expect( |
| 80 | + () => |
| 81 | + // $DisableFlowOnNegativeTest |
| 82 | + new GraphQLDirective({ |
| 83 | + name: 'Foo', |
| 84 | + locations: ['Query'], |
| 85 | + args: [], |
| 86 | + }), |
| 87 | + ).to.throw('@Foo args must be an object with argument names as keys.'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('reject an directive with undefined locations', () => { |
| 91 | + // $DisableFlowOnNegativeTest |
| 92 | + expect(() => new GraphQLDirective({ name: 'Foo' })).to.throw( |
| 93 | + '@Foo locations must be an Array.', |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it('reject an directive with incorrectly typed locations', () => { |
| 98 | + // $DisableFlowOnNegativeTest |
| 99 | + expect(() => new GraphQLDirective({ name: 'Foo', locations: {} })).to.throw( |
| 100 | + '@Foo locations must be an Array.', |
| 101 | + ); |
| 102 | + }); |
| 103 | +}); |
0 commit comments