-
Notifications
You must be signed in to change notification settings - Fork 2k
/
directive-test.js
119 lines (105 loc) · 2.98 KB
/
directive-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// @flow strict
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { GraphQLDirective } from '../directives';
import { GraphQLString, GraphQLInt } from '../scalars';
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: [],
isRepeatable: false,
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',
description: undefined,
type: GraphQLString,
defaultValue: undefined,
extensions: undefined,
astNode: undefined,
},
{
name: 'bar',
description: undefined,
type: GraphQLInt,
defaultValue: undefined,
extensions: undefined,
astNode: undefined,
},
],
isRepeatable: false,
locations: ['QUERY'],
});
});
it('defines a repeatable directive', () => {
const directive = new GraphQLDirective({
name: 'Foo',
isRepeatable: true,
locations: ['QUERY'],
});
expect(directive).to.deep.include({
name: 'Foo',
args: [],
isRepeatable: true,
locations: ['QUERY'],
});
});
it('can be stringified, JSON.stringified and Object.toStringified', () => {
const directive = new GraphQLDirective({
name: 'Foo',
locations: ['QUERY'],
});
expect(String(directive)).to.equal('@Foo');
expect(JSON.stringify(directive)).to.equal('"@Foo"');
expect(Object.prototype.toString.call(directive)).to.equal(
'[object GraphQLDirective]',
);
});
it('rejects an unnamed directive', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLDirective({ locations: ['Query'] })).to.throw(
'Directive must be named.',
);
});
it('rejects a directive with incorrectly typed args', () => {
expect(
() =>
new GraphQLDirective({
name: 'Foo',
locations: ['QUERY'],
// $DisableFlowOnNegativeTest
args: [],
}),
).to.throw('@Foo args must be an object with argument names as keys.');
});
it('rejects a directive with undefined locations', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLDirective({ name: 'Foo' })).to.throw(
'@Foo locations must be an Array.',
);
});
it('rejects a directive with incorrectly typed locations', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLDirective({ name: 'Foo', locations: {} })).to.throw(
'@Foo locations must be an Array.',
);
});
});