Skip to content

Commit f9bf263

Browse files
KnownDirectivesRule-test: add tests for missing directive locations (#3179)
1 parent 1a96306 commit f9bf263

File tree

1 file changed

+76
-55
lines changed

1 file changed

+76
-55
lines changed

src/validation/__tests__/KnownDirectivesRule-test.ts

Lines changed: 76 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ describe('Validate: Known directives', () => {
7474
`);
7575
});
7676

77-
it('with known directives', () => {
77+
it('with standard directives', () => {
7878
expectValid(`
7979
{
80-
dog @include(if: true) {
81-
name
82-
}
8380
human @skip(if: false) {
8481
name
82+
pets {
83+
... on Dog @include(if: true) {
84+
name
85+
}
86+
}
8587
}
8688
}
8789
`);
@@ -90,121 +92,140 @@ describe('Validate: Known directives', () => {
9092
it('with unknown directive', () => {
9193
expectErrors(`
9294
{
93-
dog @unknown(directive: "value") {
95+
human @unknown(directive: "value") {
9496
name
9597
}
9698
}
9799
`).to.deep.equal([
98100
{
99101
message: 'Unknown directive "@unknown".',
100-
locations: [{ line: 3, column: 13 }],
102+
locations: [{ line: 3, column: 15 }],
101103
},
102104
]);
103105
});
104106

105107
it('with many unknown directives', () => {
106108
expectErrors(`
107109
{
108-
dog @unknown(directive: "value") {
109-
name
110-
}
111-
human @unknown(directive: "value") {
110+
__typename @unknown
111+
human @unknown {
112112
name
113-
pets @unknown(directive: "value") {
113+
pets @unknown {
114114
name
115115
}
116116
}
117117
}
118118
`).to.deep.equal([
119119
{
120120
message: 'Unknown directive "@unknown".',
121-
locations: [{ line: 3, column: 13 }],
121+
locations: [{ line: 3, column: 20 }],
122122
},
123123
{
124124
message: 'Unknown directive "@unknown".',
125-
locations: [{ line: 6, column: 15 }],
125+
locations: [{ line: 4, column: 15 }],
126126
},
127127
{
128128
message: 'Unknown directive "@unknown".',
129-
locations: [{ line: 8, column: 16 }],
129+
locations: [{ line: 6, column: 16 }],
130130
},
131131
]);
132132
});
133133

134134
it('with well placed directives', () => {
135135
expectValid(`
136-
query ($var: Boolean) @onQuery {
137-
name @include(if: $var)
138-
...Frag @include(if: true)
139-
skippedField @skip(if: true)
140-
...SkippedFrag @skip(if: true)
141-
142-
... @skip(if: true) {
143-
skippedField
136+
query ($var: Boolean @onVariableDefinition) @onQuery {
137+
human @onField {
138+
...Frag @onFragmentSpread
139+
... @onInlineFragment {
140+
name @onField
141+
}
144142
}
145143
}
146144
147145
mutation @onMutation {
148-
someField
146+
someField @onField
149147
}
150148
151149
subscription @onSubscription {
152-
someField
150+
someField @onField
153151
}
154152
155-
fragment Frag on SomeType @onFragmentDefinition {
156-
someField
157-
}
158-
`);
159-
});
160-
161-
it('with well placed variable definition directive', () => {
162-
expectValid(`
163-
query Foo($var: Boolean @onVariableDefinition) {
164-
name
153+
fragment Frag on Human @onFragmentDefinition {
154+
name @onField
165155
}
166156
`);
167157
});
168158

169159
it('with misplaced directives', () => {
170160
expectErrors(`
171-
query Foo($var: Boolean) @include(if: true) {
172-
name @onQuery @include(if: $var)
173-
...Frag @onQuery
161+
query ($var: Boolean @onQuery) @onMutation {
162+
human @onQuery {
163+
...Frag @onQuery
164+
... @onQuery {
165+
name @onQuery
166+
}
167+
}
168+
}
169+
170+
mutation @onQuery {
171+
someField @onQuery
174172
}
175173
176-
mutation Bar @onQuery {
177-
someField
174+
subscription @onQuery {
175+
someField @onQuery
176+
}
177+
178+
fragment Frag on Human @onQuery {
179+
name @onQuery
178180
}
179181
`).to.deep.equal([
180182
{
181-
message: 'Directive "@include" may not be used on QUERY.',
182-
locations: [{ line: 2, column: 32 }],
183+
message: 'Directive "@onQuery" may not be used on VARIABLE_DEFINITION.',
184+
locations: [{ line: 2, column: 28 }],
185+
},
186+
{
187+
message: 'Directive "@onMutation" may not be used on QUERY.',
188+
locations: [{ line: 2, column: 38 }],
183189
},
184190
{
185191
message: 'Directive "@onQuery" may not be used on FIELD.',
186-
locations: [{ line: 3, column: 14 }],
192+
locations: [{ line: 3, column: 15 }],
187193
},
188194
{
189195
message: 'Directive "@onQuery" may not be used on FRAGMENT_SPREAD.',
190-
locations: [{ line: 4, column: 17 }],
196+
locations: [{ line: 4, column: 19 }],
197+
},
198+
{
199+
message: 'Directive "@onQuery" may not be used on INLINE_FRAGMENT.',
200+
locations: [{ line: 5, column: 15 }],
201+
},
202+
{
203+
message: 'Directive "@onQuery" may not be used on FIELD.',
204+
locations: [{ line: 6, column: 18 }],
191205
},
192206
{
193207
message: 'Directive "@onQuery" may not be used on MUTATION.',
194-
locations: [{ line: 7, column: 20 }],
208+
locations: [{ line: 11, column: 16 }],
195209
},
196-
]);
197-
});
198-
199-
it('with misplaced variable definition directive', () => {
200-
expectErrors(`
201-
query Foo($var: Boolean @onField) {
202-
name
203-
}
204-
`).to.deep.equal([
205210
{
206-
message: 'Directive "@onField" may not be used on VARIABLE_DEFINITION.',
207-
locations: [{ line: 2, column: 31 }],
211+
message: 'Directive "@onQuery" may not be used on FIELD.',
212+
locations: [{ column: 19, line: 12 }],
213+
},
214+
{
215+
message: 'Directive "@onQuery" may not be used on SUBSCRIPTION.',
216+
locations: [{ column: 20, line: 15 }],
217+
},
218+
{
219+
message: 'Directive "@onQuery" may not be used on FIELD.',
220+
locations: [{ column: 19, line: 16 }],
221+
},
222+
{
223+
message: 'Directive "@onQuery" may not be used on FRAGMENT_DEFINITION.',
224+
locations: [{ column: 30, line: 19 }],
225+
},
226+
{
227+
message: 'Directive "@onQuery" may not be used on FIELD.',
228+
locations: [{ column: 14, line: 20 }],
208229
},
209230
]);
210231
});

0 commit comments

Comments
 (0)