Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for GraphQLDirective (#1662)
- Loading branch information
1 parent
d48e481
commit 6741ac2
Showing
2 changed files
with
105 additions
and
2 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,103 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
import { describe, it } from 'mocha'; | ||
import { expect } from 'chai'; | ||
|
||
import { GraphQLDirective, GraphQLString, GraphQLInt } from '../'; | ||
|
||
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: [], | ||
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', | ||
type: GraphQLString, | ||
description: null, | ||
defaultValue: undefined, | ||
astNode: undefined, | ||
}, | ||
{ | ||
name: 'bar', | ||
type: GraphQLInt, | ||
description: null, | ||
defaultValue: undefined, | ||
astNode: undefined, | ||
}, | ||
], | ||
locations: ['QUERY'], | ||
}); | ||
}); | ||
|
||
it('can be stringified and JSON.stringified', () => { | ||
const directive = new GraphQLDirective({ | ||
name: 'Foo', | ||
locations: ['QUERY'], | ||
}); | ||
|
||
expect(String(directive)).to.equal('@Foo'); | ||
expect(JSON.stringify(directive)).to.equal('"@Foo"'); | ||
}); | ||
|
||
it('reject an unnamed directive', () => { | ||
// $DisableFlowOnNegativeTest | ||
expect(() => new GraphQLDirective({ locations: ['Query'] })).to.throw( | ||
'Directive must be named.', | ||
); | ||
}); | ||
|
||
it('reject directive incorrectly typed args', () => { | ||
expect( | ||
() => | ||
// $DisableFlowOnNegativeTest | ||
new GraphQLDirective({ | ||
name: 'Foo', | ||
locations: ['Query'], | ||
args: [], | ||
}), | ||
).to.throw('@Foo args must be an object with argument names as keys.'); | ||
}); | ||
|
||
it('reject an directive with undefined locations', () => { | ||
// $DisableFlowOnNegativeTest | ||
expect(() => new GraphQLDirective({ name: 'Foo' })).to.throw( | ||
'@Foo locations must be an Array.', | ||
); | ||
}); | ||
|
||
it('reject an directive with incorrectly typed locations', () => { | ||
// $DisableFlowOnNegativeTest | ||
expect(() => new GraphQLDirective({ name: 'Foo', locations: {} })).to.throw( | ||
'@Foo locations must be an Array.', | ||
); | ||
}); | ||
}); |
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