|
| 1 | +/** |
| 2 | + * Copyright (c) 2015, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it } from 'mocha'; |
| 11 | +import { expect } from 'chai'; |
| 12 | +import { |
| 13 | + GraphQLSchema, |
| 14 | + GraphQLString, |
| 15 | + GraphQLInt, |
| 16 | + GraphQLFloat, |
| 17 | + GraphQLList, |
| 18 | + GraphQLNonNull, |
| 19 | + GraphQLObjectType, |
| 20 | + GraphQLInterfaceType, |
| 21 | + GraphQLUnionType, |
| 22 | +} from '../../type'; |
| 23 | +import { isEqualType, isTypeSubTypeOf } from '../typeComparators'; |
| 24 | + |
| 25 | + |
| 26 | +describe('typeComparators', () => { |
| 27 | + |
| 28 | + describe('isEqualType', () => { |
| 29 | + |
| 30 | + it('same reference are equal', () => { |
| 31 | + expect(isEqualType(GraphQLString, GraphQLString)).to.equal(true); |
| 32 | + }); |
| 33 | + |
| 34 | + it('int and float are not equal', () => { |
| 35 | + expect(isEqualType(GraphQLInt, GraphQLFloat)).to.equal(false); |
| 36 | + }); |
| 37 | + |
| 38 | + it('lists of same type are equal', () => { |
| 39 | + expect( |
| 40 | + isEqualType(new GraphQLList(GraphQLInt), new GraphQLList(GraphQLInt)) |
| 41 | + ).to.equal(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it('lists is not equal to item', () => { |
| 45 | + expect( |
| 46 | + isEqualType(new GraphQLList(GraphQLInt), GraphQLInt) |
| 47 | + ).to.equal(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('non-null of same type are equal', () => { |
| 51 | + expect( |
| 52 | + isEqualType( |
| 53 | + new GraphQLNonNull(GraphQLInt), |
| 54 | + new GraphQLNonNull(GraphQLInt) |
| 55 | + ) |
| 56 | + ).to.equal(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('non-null is not equal to nullable', () => { |
| 60 | + expect( |
| 61 | + isEqualType(new GraphQLNonNull(GraphQLInt), GraphQLInt) |
| 62 | + ).to.equal(false); |
| 63 | + }); |
| 64 | + |
| 65 | + }); |
| 66 | + |
| 67 | + describe('isTypeSubTypeOf', () => { |
| 68 | + |
| 69 | + function testSchema(fields) { |
| 70 | + return new GraphQLSchema({ |
| 71 | + query: new GraphQLObjectType({ |
| 72 | + name: 'Query', |
| 73 | + fields |
| 74 | + }) |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + it('same reference is subtype', () => { |
| 79 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 80 | + expect( |
| 81 | + isTypeSubTypeOf(schema, GraphQLString, GraphQLString) |
| 82 | + ).to.equal(true); |
| 83 | + }); |
| 84 | + |
| 85 | + it('int is not subtype of float', () => { |
| 86 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 87 | + expect( |
| 88 | + isTypeSubTypeOf(schema, GraphQLInt, GraphQLFloat) |
| 89 | + ).to.equal(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it('non-null is subtype of nullable', () => { |
| 93 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 94 | + expect( |
| 95 | + isTypeSubTypeOf(schema, new GraphQLNonNull(GraphQLInt), GraphQLInt) |
| 96 | + ).to.equal(true); |
| 97 | + }); |
| 98 | + |
| 99 | + it('nullable is not subtype of non-null', () => { |
| 100 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 101 | + expect( |
| 102 | + isTypeSubTypeOf(schema, GraphQLInt, new GraphQLNonNull(GraphQLInt)) |
| 103 | + ).to.equal(false); |
| 104 | + }); |
| 105 | + |
| 106 | + it('item is not subtype of list', () => { |
| 107 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 108 | + expect( |
| 109 | + isTypeSubTypeOf(schema, GraphQLInt, new GraphQLList(GraphQLInt)) |
| 110 | + ).to.equal(false); |
| 111 | + }); |
| 112 | + |
| 113 | + it('list is not subtype of item', () => { |
| 114 | + const schema = testSchema({ field: { type: GraphQLString } }); |
| 115 | + expect( |
| 116 | + isTypeSubTypeOf(schema, new GraphQLList(GraphQLInt), GraphQLInt) |
| 117 | + ).to.equal(false); |
| 118 | + }); |
| 119 | + |
| 120 | + it('member is subtype of union', () => { |
| 121 | + const member = new GraphQLObjectType({ |
| 122 | + name: 'Object', |
| 123 | + isTypeOf: () => true, |
| 124 | + fields: { |
| 125 | + field: { type: GraphQLString } |
| 126 | + } |
| 127 | + }); |
| 128 | + const union = new GraphQLUnionType({ name: 'Union', types: [ member ] }); |
| 129 | + const schema = testSchema({ field: { type: union } }); |
| 130 | + expect( |
| 131 | + isTypeSubTypeOf(schema, member, union) |
| 132 | + ).to.equal(true); |
| 133 | + }); |
| 134 | + |
| 135 | + it('implementation is subtype of interface', () => { |
| 136 | + const iface = new GraphQLInterfaceType({ |
| 137 | + name: 'Interface', |
| 138 | + fields: { |
| 139 | + field: { type: GraphQLString } |
| 140 | + } |
| 141 | + }); |
| 142 | + const impl = new GraphQLObjectType({ |
| 143 | + name: 'Object', |
| 144 | + isTypeOf: () => true, |
| 145 | + interfaces: [ iface ], |
| 146 | + fields: { |
| 147 | + field: { type: GraphQLString } |
| 148 | + } |
| 149 | + }); |
| 150 | + const schema = testSchema({ field: { type: impl } }); |
| 151 | + expect( |
| 152 | + isTypeSubTypeOf(schema, impl, iface) |
| 153 | + ).to.equal(true); |
| 154 | + }); |
| 155 | + |
| 156 | + }); |
| 157 | + |
| 158 | +}); |
0 commit comments