This repository has been archived by the owner on Jul 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve test coverage on AST helpers (#82)
* Add tests for ast/traversal helpers * Replace evaluateValueNode with valueFromASTUntyped from graphql * Add tests for normalizeVariables in ast/variables
- Loading branch information
Showing
4 changed files
with
148 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import gql from 'graphql-tag'; | ||
import { getSelectionSet } from './node'; | ||
import { getMainOperation, shouldInclude } from './traversal'; | ||
|
||
describe('getMainOperation', () => { | ||
it('retrieves the first operation', () => { | ||
const doc = gql` | ||
query Query { | ||
field | ||
} | ||
`; | ||
const operation = getMainOperation(doc); | ||
expect(operation).toBe(doc.definitions[0]); | ||
}); | ||
|
||
it('throws when no operation is found', () => { | ||
const doc = gql` | ||
fragment _ on Query { | ||
field | ||
} | ||
`; | ||
expect(() => getMainOperation(doc)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('shouldInclude', () => { | ||
it('should include fields with truthy @include or falsy @skip directives', () => { | ||
const doc = gql` | ||
{ | ||
fieldA @include(if: true) | ||
fieldB @skip(if: false) | ||
} | ||
`; | ||
const fieldA = getSelectionSet(getMainOperation(doc))[0]; | ||
const fieldB = getSelectionSet(getMainOperation(doc))[1]; | ||
expect(shouldInclude(fieldA, {})).toBe(true); | ||
expect(shouldInclude(fieldB, {})).toBe(true); | ||
}); | ||
|
||
it('should exclude fields with falsy @include or truthy @skip directives', () => { | ||
const doc = gql` | ||
{ | ||
fieldA @include(if: false) | ||
fieldB @skip(if: true) | ||
} | ||
`; | ||
const fieldA = getSelectionSet(getMainOperation(doc))[0]; | ||
const fieldB = getSelectionSet(getMainOperation(doc))[1]; | ||
expect(shouldInclude(fieldA, {})).toBe(false); | ||
expect(shouldInclude(fieldB, {})).toBe(false); | ||
}); | ||
|
||
it('ignore other directives', () => { | ||
const doc = gql` | ||
{ | ||
field @test(if: false) | ||
} | ||
`; | ||
const field = getSelectionSet(getMainOperation(doc))[0]; | ||
expect(shouldInclude(field, {})).toBe(true); | ||
}); | ||
|
||
it('ignore unknown arguments on directives', () => { | ||
const doc = gql` | ||
{ | ||
field @skip(if: true, other: false) | ||
} | ||
`; | ||
const field = getSelectionSet(getMainOperation(doc))[0]; | ||
expect(shouldInclude(field, {})).toBe(false); | ||
}); | ||
|
||
it('ignore directives with invalid first arguments', () => { | ||
const doc = gql` | ||
{ | ||
field @skip(other: true) | ||
} | ||
`; | ||
const field = getSelectionSet(getMainOperation(doc))[0]; | ||
expect(shouldInclude(field, {})).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import gql from 'graphql-tag'; | ||
import { getMainOperation } from './traversal'; | ||
import { normalizeVariables } from './variables'; | ||
|
||
describe('normalizeVariables', () => { | ||
it('normalizes variables', () => { | ||
const input = { x: 42 }; | ||
const operation = getMainOperation( | ||
gql` | ||
query($x: Int!) { | ||
field | ||
} | ||
` | ||
); | ||
const normalized = normalizeVariables(operation, input); | ||
expect(normalized).toEqual({ x: 42 }); | ||
}); | ||
|
||
it('normalizes variables with defaults', () => { | ||
const input = { x: undefined }; | ||
const operation = getMainOperation( | ||
gql` | ||
query($x: Int! = 42) { | ||
field | ||
} | ||
` | ||
); | ||
const normalized = normalizeVariables(operation, input); | ||
expect(normalized).toEqual({ x: 42 }); | ||
}); | ||
|
||
it('normalizes variables even with missing fields', () => { | ||
const input = { x: undefined }; | ||
const operation = getMainOperation( | ||
gql` | ||
query($x: Int!) { | ||
field | ||
} | ||
` | ||
); | ||
const normalized = normalizeVariables(operation, input); | ||
expect(normalized).toEqual({}); | ||
}); | ||
|
||
it('skips normalizing for queries without variables', () => { | ||
const operation = getMainOperation( | ||
gql` | ||
query { | ||
field | ||
} | ||
` | ||
); | ||
(operation as any).variableDefinitions = undefined; | ||
const normalized = normalizeVariables(operation, {}); | ||
expect(normalized).toEqual({}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters