Skip to content

Add directive predicates tests and fix test names #1781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/type/__tests__/directive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Type System: Directive', () => {
});
});

it('can be stringified, JSON.stringified and Object.toStingified', () => {
it('can be stringified, JSON.stringified and Object.toStringified', () => {
const directive = new GraphQLDirective({
name: 'Foo',
locations: ['QUERY'],
Expand All @@ -71,14 +71,14 @@ describe('Type System: Directive', () => {
);
});

it('reject an unnamed directive', () => {
it('rejects an unnamed directive', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLDirective({ locations: ['Query'] })).to.throw(
'Directive must be named.',
);
});

it('reject directive incorrectly typed args', () => {
it('rejects a directive with incorrectly typed args', () => {
expect(
() =>
new GraphQLDirective({
Expand All @@ -90,14 +90,14 @@ describe('Type System: Directive', () => {
).to.throw('@Foo args must be an object with argument names as keys.');
});

it('reject an directive with undefined locations', () => {
it('rejects a 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', () => {
it('rejects a directive with incorrectly typed locations', () => {
// $DisableFlowOnNegativeTest
expect(() => new GraphQLDirective({ name: 'Foo', locations: {} })).to.throw(
'@Foo locations must be an Array.',
Expand Down
78 changes: 78 additions & 0 deletions src/type/__tests__/predicate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
GraphQLList,
GraphQLNonNull,
GraphQLString,
GraphQLDirective,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
isType,
isScalarType,
isObjectType,
Expand All @@ -39,6 +43,8 @@ import {
isNamedType,
isRequiredArgument,
isRequiredInputField,
isDirective,
isSpecifiedDirective,
assertType,
assertScalarType,
assertObjectType,
Expand All @@ -56,6 +62,7 @@ import {
assertWrappingType,
assertNullableType,
assertNamedType,
assertDirective,
getNullableType,
getNamedType,
} from '../';
Expand Down Expand Up @@ -583,3 +590,74 @@ describe('Type predicates', () => {
});
});
});

describe('Directive predicates', () => {
describe('isDirective', () => {
it('returns true for directives', () => {
const directive = new GraphQLDirective({
name: 'Foo',
locations: ['QUERY'],
});
expect(isDirective(directive)).to.equal(true);
expect(() => assertDirective(directive)).not.to.throw();
expect(isDirective(GraphQLSkipDirective)).to.equal(true);
expect(() => assertDirective(GraphQLSkipDirective)).not.to.throw();
});

it('returns false for directive class (rather than instance)', () => {
// $DisableFlowOnNegativeTest
expect(isDirective(GraphQLDirective)).to.equal(false);
expect(() => assertDirective(GraphQLDirective)).to.throw();
});

it('returns false for object type', () => {
expect(isDirective(ObjectType)).to.equal(false);
expect(() => assertDirective(ObjectType)).to.throw();
});

it('returns false for scalar type', () => {
expect(isDirective(GraphQLString)).to.equal(false);
expect(() => assertDirective(GraphQLString)).to.throw();
});

it('returns false for random garbage', () => {
expect(isDirective({ what: 'is this' })).to.equal(false);
expect(() => assertDirective({ what: 'is this' })).to.throw();
});
});
describe('isSpecifiedDirective', () => {
it('returns true for specified directives', () => {
expect(isSpecifiedDirective(GraphQLIncludeDirective)).to.equal(true);
expect(isSpecifiedDirective(GraphQLSkipDirective)).to.equal(true);
expect(isSpecifiedDirective(GraphQLDeprecatedDirective)).to.equal(true);
});

it('returns false for custom directive', () => {
const directive = new GraphQLDirective({
name: 'Foo',
locations: ['QUERY'],
});
expect(isSpecifiedDirective(directive)).to.equal(false);
});

it('returns false for directive class (rather than specified instance)', () => {
// $DisableFlowOnNegativeTest
expect(isSpecifiedDirective(GraphQLDirective)).to.equal(false);
});

it('returns false for object type', () => {
// $DisableFlowOnNegativeTest
expect(isSpecifiedDirective(ObjectType)).to.equal(false);
});

it('returns false for spec defined scalar type', () => {
// $DisableFlowOnNegativeTest
expect(isSpecifiedDirective(GraphQLString)).to.equal(false);
});

it('returns false for random garbage', () => {
// $DisableFlowOnNegativeTest
expect(isSpecifiedDirective({ what: 'is this' })).to.equal(false);
});
});
});