Skip to content

Commit

Permalink
Add tests for GraphQLDirective (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jan 14, 2019
1 parent d48e481 commit 6741ac2
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 2 deletions.
103 changes: 103 additions & 0 deletions 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.',
);
});
});
4 changes: 2 additions & 2 deletions src/type/directives.js
Expand Up @@ -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.`,
);

Expand Down

0 comments on commit 6741ac2

Please sign in to comment.