From 2193adb3cf0b22a3ce4ccb1d303e96cf14e7d951 Mon Sep 17 00:00:00 2001 From: Christoph Zwerschke Date: Fri, 8 Mar 2019 20:45:27 +0100 Subject: [PATCH] Add directive predicates tests and fix test names --- src/type/__tests__/directive-test.js | 10 ++-- src/type/__tests__/predicate-test.js | 78 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/src/type/__tests__/directive-test.js b/src/type/__tests__/directive-test.js index c93680a000..387bf2046c 100644 --- a/src/type/__tests__/directive-test.js +++ b/src/type/__tests__/directive-test.js @@ -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'], @@ -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({ @@ -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.', diff --git a/src/type/__tests__/predicate-test.js b/src/type/__tests__/predicate-test.js index 7e002cec35..b23e568982 100644 --- a/src/type/__tests__/predicate-test.js +++ b/src/type/__tests__/predicate-test.js @@ -20,6 +20,10 @@ import { GraphQLList, GraphQLNonNull, GraphQLString, + GraphQLDirective, + GraphQLIncludeDirective, + GraphQLSkipDirective, + GraphQLDeprecatedDirective, isType, isScalarType, isObjectType, @@ -39,6 +43,8 @@ import { isNamedType, isRequiredArgument, isRequiredInputField, + isDirective, + isSpecifiedDirective, assertType, assertScalarType, assertObjectType, @@ -56,6 +62,7 @@ import { assertWrappingType, assertNullableType, assertNamedType, + assertDirective, getNullableType, getNamedType, } from '../'; @@ -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); + }); + }); +});