Skip to content

Commit

Permalink
Add test cases for NoUndefined/NoUnused/InAllowedPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmahone committed Dec 30, 2022
1 parent 1b701ba commit 87332ed
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/validation/__tests__/NoUndefinedVariablesRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,67 @@ describe('Validate: No undefined variables', () => {
},
]);
});

it('fragment defined arguments are not undefined variables', () => {
expectValid(`
query Foo {
...FragA
}
fragment FragA($a: String) on Type {
field1(a: $a)
}
`);
});

it('defined variables used as fragment arguments are not undefined variables', () => {
expectValid(`
query Foo($b: String) {
...FragA(a: $b)
}
fragment FragA($a: String) on Type {
field1
}
`);
});

it('variables used as fragment arguments may be undefined variables', () => {
expectErrors(`
query Foo {
...FragA(a: $a)
}
fragment FragA($a: String) on Type {
field1
}
`).toDeepEqual([
{
message: 'Variable "$a" is not defined by operation "Foo".',
locations: [
{ line: 3, column: 21 },
{ line: 2, column: 7 },
],
},
]);
});

it('variables shadowed by parent fragment arguments are still undefined variables', () => {
expectErrors(`
query Foo {
...FragA
}
fragment FragA($a: String) on Type {
...FragB
}
fragment FragB on Type {
field1(a: $a)
}
`).toDeepEqual([
{
message: 'Variable "$a" is not defined by operation "Foo".',
locations: [
{ line: 9, column: 19 },
{ line: 2, column: 7 },
],
},
]);
});
});
22 changes: 22 additions & 0 deletions src/validation/__tests__/NoUnusedVariablesRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,26 @@ describe('Validate: No unused variables', () => {
},
]);
});

it('fragment defined arguments are not unused variables', () => {
expectValid(`
query Foo {
...FragA
}
fragment FragA($a: String) on Type {
field1(a: $a)
}
`);
});

it('defined variables used as fragment arguments are not unused variables', () => {
expectValid(`
query Foo($b: String) {
...FragA(a: $b)
}
fragment FragA($a: String) on Type {
field1
}
`);
});
});
61 changes: 61 additions & 0 deletions src/validation/__tests__/VariablesInAllowedPositionRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,65 @@ describe('Validate: Variables are in allowed positions', () => {
}`);
});
});

describe('Fragment arguments are validated', () => {
it('Boolean => Boolean', () => {
expectValid(`
query Query($booleanArg: Boolean)
{
complicatedArgs {
...A(b: $booleanArg)
}
}
fragment A($b: Boolean) on ComplicatedArgs {
booleanArgField(booleanArg: $b)
}
`);
});

it('Boolean => Boolean!', () => {
expectErrors(`
query Query($fb: Boolean)
{
complicatedArgs {
...A(b: $fb)
}
}
fragment A($b: Boolean!) on ComplicatedArgs {
booleanArgField(booleanArg: $b)
}
`).toDeepEqual([
{
message:
'Variable "$booleanArg" of type "Boolean" used in position expecting type "Boolean!".',
locations: [
{ line: 2, column: 21 },
{ line: 4, column: 47 },
],
},
]);
});

it('Int => Int! fails when variable provides null default value', () => {
expectErrors(`
query Query($intVar: Int = null) {
complicatedArgs {
...A(i: $intVar)
}
}
fragment A($i: Int!) on ComplicatedArgs {
nonNullIntArgField(nonNullIntArg: $i)
}
`).toDeepEqual([
{
message:
'Variable "$intVar" of type "Int" used in position expecting type "Int!".',
locations: [
{ line: 2, column: 21 },
{ line: 4, column: 47 },
],
},
]);
});
});
});

0 comments on commit 87332ed

Please sign in to comment.