From c4929acad1b604a2a62ef2740687e0469ac45464 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 14 Jan 2019 11:16:31 +0200 Subject: [PATCH] Add tests for GraphQLDirective --- src/type/__tests__/directive-test.js | 103 +++++++++++++++++++++++++++ src/type/directives.js | 4 +- 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 src/type/__tests__/directive-test.js diff --git a/src/type/__tests__/directive-test.js b/src/type/__tests__/directive-test.js new file mode 100644 index 0000000000..e700afbe87 --- /dev/null +++ b/src/type/__tests__/directive-test.js @@ -0,0 +1,103 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict + */ + +import { describe, it } from 'mocha'; +import { expect } from 'chai'; + +import { GraphQLDirective, GraphQLString, GraphQLInt } from '../'; + +describe('Type System: Directive', () => { + it('defines a directive with no args', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + locations: ['QUERY'], + }); + + expect(directive).to.deep.include({ + name: 'Foo', + args: [], + locations: ['QUERY'], + }); + }); + + it('defines a directive with multiple args', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + args: { + foo: { type: GraphQLString }, + bar: { type: GraphQLInt }, + }, + locations: ['QUERY'], + }); + + expect(directive).to.deep.include({ + name: 'Foo', + args: [ + { + name: 'foo', + type: GraphQLString, + description: null, + defaultValue: undefined, + astNode: undefined, + }, + { + name: 'bar', + type: GraphQLInt, + description: null, + defaultValue: undefined, + astNode: undefined, + }, + ], + locations: ['QUERY'], + }); + }); + + it('can be stringified and JSON.stringified', () => { + const directive = new GraphQLDirective({ + name: 'Foo', + locations: ['QUERY'], + }); + + expect(String(directive)).to.equal('@Foo'); + expect(JSON.stringify(directive)).to.equal('"@Foo"'); + }); + + it('reject an unnamed directive', () => { + // $DisableFlowOnNegativeTest + expect(() => new GraphQLDirective({ locations: ['Query'] })).to.throw( + 'Directive must be named.', + ); + }); + + it('reject directive incorrectly typed args', () => { + expect( + () => + // $DisableFlowOnNegativeTest + new GraphQLDirective({ + name: 'Foo', + locations: ['Query'], + args: [], + }), + ).to.throw('@Foo args must be an object with argument names as keys.'); + }); + + it('reject an directive with undefined locations', () => { + // $DisableFlowOnNegativeTest + expect(() => new GraphQLDirective({ name: 'Foo' })).to.throw( + '@Foo locations must be an Array.', + ); + }); + + it('reject an directive with incorrectly typed locations', () => { + // $DisableFlowOnNegativeTest + expect(() => new GraphQLDirective({ name: 'Foo', locations: {} })).to.throw( + '@Foo locations must be an Array.', + ); + }); +}); diff --git a/src/type/directives.js b/src/type/directives.js index 747bbda56c..a9f512f250 100644 --- a/src/type/directives.js +++ b/src/type/directives.js @@ -63,12 +63,12 @@ export class GraphQLDirective { invariant(config.name, 'Directive must be named.'); invariant( Array.isArray(config.locations), - 'Must provide locations for directive.', + `@${config.name} locations must be an Array.`, ); const args = config.args || {}; invariant( - !Array.isArray(args), + typeof args === 'object' && !Array.isArray(args), `@${config.name} args must be an object with argument names as keys.`, );