Skip to content

Commit 6741ac2

Browse files
Add tests for GraphQLDirective (#1662)
1 parent d48e481 commit 6741ac2

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
});

src/type/directives.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ export class GraphQLDirective {
6363
invariant(config.name, 'Directive must be named.');
6464
invariant(
6565
Array.isArray(config.locations),
66-
'Must provide locations for directive.',
66+
`@${config.name} locations must be an Array.`,
6767
);
6868

6969
const args = config.args || {};
7070
invariant(
71-
!Array.isArray(args),
71+
typeof args === 'object' && !Array.isArray(args),
7272
`@${config.name} args must be an object with argument names as keys.`,
7373
);
7474

0 commit comments

Comments
 (0)