Skip to content

Commit

Permalink
KnownDirectivesRule-test: add tests for missing directive locations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jun 14, 2021
1 parent 1a96306 commit f9bf263
Showing 1 changed file with 76 additions and 55 deletions.
131 changes: 76 additions & 55 deletions src/validation/__tests__/KnownDirectivesRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,16 @@ describe('Validate: Known directives', () => {
`);
});

it('with known directives', () => {
it('with standard directives', () => {
expectValid(`
{
dog @include(if: true) {
name
}
human @skip(if: false) {
name
pets {
... on Dog @include(if: true) {
name
}
}
}
}
`);
Expand All @@ -90,121 +92,140 @@ describe('Validate: Known directives', () => {
it('with unknown directive', () => {
expectErrors(`
{
dog @unknown(directive: "value") {
human @unknown(directive: "value") {
name
}
}
`).to.deep.equal([
{
message: 'Unknown directive "@unknown".',
locations: [{ line: 3, column: 13 }],
locations: [{ line: 3, column: 15 }],
},
]);
});

it('with many unknown directives', () => {
expectErrors(`
{
dog @unknown(directive: "value") {
name
}
human @unknown(directive: "value") {
__typename @unknown
human @unknown {
name
pets @unknown(directive: "value") {
pets @unknown {
name
}
}
}
`).to.deep.equal([
{
message: 'Unknown directive "@unknown".',
locations: [{ line: 3, column: 13 }],
locations: [{ line: 3, column: 20 }],
},
{
message: 'Unknown directive "@unknown".',
locations: [{ line: 6, column: 15 }],
locations: [{ line: 4, column: 15 }],
},
{
message: 'Unknown directive "@unknown".',
locations: [{ line: 8, column: 16 }],
locations: [{ line: 6, column: 16 }],
},
]);
});

it('with well placed directives', () => {
expectValid(`
query ($var: Boolean) @onQuery {
name @include(if: $var)
...Frag @include(if: true)
skippedField @skip(if: true)
...SkippedFrag @skip(if: true)
... @skip(if: true) {
skippedField
query ($var: Boolean @onVariableDefinition) @onQuery {
human @onField {
...Frag @onFragmentSpread
... @onInlineFragment {
name @onField
}
}
}
mutation @onMutation {
someField
someField @onField
}
subscription @onSubscription {
someField
someField @onField
}
fragment Frag on SomeType @onFragmentDefinition {
someField
}
`);
});

it('with well placed variable definition directive', () => {
expectValid(`
query Foo($var: Boolean @onVariableDefinition) {
name
fragment Frag on Human @onFragmentDefinition {
name @onField
}
`);
});

it('with misplaced directives', () => {
expectErrors(`
query Foo($var: Boolean) @include(if: true) {
name @onQuery @include(if: $var)
...Frag @onQuery
query ($var: Boolean @onQuery) @onMutation {
human @onQuery {
...Frag @onQuery
... @onQuery {
name @onQuery
}
}
}
mutation @onQuery {
someField @onQuery
}
mutation Bar @onQuery {
someField
subscription @onQuery {
someField @onQuery
}
fragment Frag on Human @onQuery {
name @onQuery
}
`).to.deep.equal([
{
message: 'Directive "@include" may not be used on QUERY.',
locations: [{ line: 2, column: 32 }],
message: 'Directive "@onQuery" may not be used on VARIABLE_DEFINITION.',
locations: [{ line: 2, column: 28 }],
},
{
message: 'Directive "@onMutation" may not be used on QUERY.',
locations: [{ line: 2, column: 38 }],
},
{
message: 'Directive "@onQuery" may not be used on FIELD.',
locations: [{ line: 3, column: 14 }],
locations: [{ line: 3, column: 15 }],
},
{
message: 'Directive "@onQuery" may not be used on FRAGMENT_SPREAD.',
locations: [{ line: 4, column: 17 }],
locations: [{ line: 4, column: 19 }],
},
{
message: 'Directive "@onQuery" may not be used on INLINE_FRAGMENT.',
locations: [{ line: 5, column: 15 }],
},
{
message: 'Directive "@onQuery" may not be used on FIELD.',
locations: [{ line: 6, column: 18 }],
},
{
message: 'Directive "@onQuery" may not be used on MUTATION.',
locations: [{ line: 7, column: 20 }],
locations: [{ line: 11, column: 16 }],
},
]);
});

it('with misplaced variable definition directive', () => {
expectErrors(`
query Foo($var: Boolean @onField) {
name
}
`).to.deep.equal([
{
message: 'Directive "@onField" may not be used on VARIABLE_DEFINITION.',
locations: [{ line: 2, column: 31 }],
message: 'Directive "@onQuery" may not be used on FIELD.',
locations: [{ column: 19, line: 12 }],
},
{
message: 'Directive "@onQuery" may not be used on SUBSCRIPTION.',
locations: [{ column: 20, line: 15 }],
},
{
message: 'Directive "@onQuery" may not be used on FIELD.',
locations: [{ column: 19, line: 16 }],
},
{
message: 'Directive "@onQuery" may not be used on FRAGMENT_DEFINITION.',
locations: [{ column: 30, line: 19 }],
},
{
message: 'Directive "@onQuery" may not be used on FIELD.',
locations: [{ column: 14, line: 20 }],
},
]);
});
Expand Down

0 comments on commit f9bf263

Please sign in to comment.