Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/type/__tests__/directive-test.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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