11/* @flow */
22
3- import {
4- graphql ,
5- InputTypeComposer ,
6- SchemaComposer ,
7- schemaComposer ,
8- TypeComposer ,
9- InterfaceTypeComposer ,
10- } from 'graphql-compose' ;
3+ import { InputTypeComposer , schemaComposer , TypeComposer } from 'graphql-compose' ;
114import { getCharacterModels } from '../__mocks__/characterModels' ;
125import { MovieModel } from '../__mocks__/movieModel' ;
136import { composeWithMongooseDiscriminators } from '../composeWithMongooseDiscriminators' ;
@@ -30,16 +23,11 @@ describe('composeWithMongooseDiscriminators ->', () => {
3023 ) ;
3124 } ) ;
3225
33- it ( 'should return a TypeComposer as childTC' , ( ) => {
26+ it ( 'should return a TypeComposer as childTC on discriminator() call ' , ( ) => {
3427 expect (
3528 composeWithMongooseDiscriminators ( CharacterModel ) . discriminator ( PersonModel )
3629 ) . toBeInstanceOf ( TypeComposer ) ;
3730 } ) ;
38-
39- it ( 'should have an interface, accessed with getDInterface' , ( ) => {
40- const cDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
41- expect ( cDTC . getDInterface ( ) ) . toBeInstanceOf ( InterfaceTypeComposer ) ;
42- } ) ;
4331 } ) ;
4432
4533 describe ( 'composeWithMongoose customisationOptions' , ( ) => {
@@ -73,191 +61,4 @@ describe('composeWithMongooseDiscriminators ->', () => {
7361 expect ( itc . isRequired ( 'friends' ) ) . toBe ( true ) ;
7462 } ) ;
7563 } ) ;
76-
77- describe ( 'DInterface' , ( ) => {
78- it ( 'should have same field names as baseModel used to create it' , ( ) => {
79- const baseDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
80- expect ( baseDTC . getFieldNames ( ) ) . toEqual (
81- expect . arrayContaining ( Object . keys ( baseDTC . getDInterface ( ) . getFields ( ) ) )
82- ) ;
83- } ) ;
84-
85- it ( 'should have field names synced with the baseTC' , ( ) => {
86- const baseDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
87-
88- expect ( baseDTC . getFieldNames ( ) ) . toEqual ( Object . keys ( baseDTC . getDInterface ( ) . getFields ( ) ) ) ;
89-
90- beforeAll ( ( ) =>
91- baseDTC . addFields ( {
92- field1 : 'String' ,
93- field2 : 'String' ,
94- } ) ) ;
95-
96- expect ( baseDTC . getFieldNames ( ) ) . toEqual ( Object . keys ( baseDTC . getDInterface ( ) . getFields ( ) ) ) ;
97- } ) ;
98- } ) ;
99-
100- describe ( 'DiscriminatorTypeComposer' , ( ) => {
101- it ( 'should have as interface DInterface' , ( ) => {
102- const baseDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
103- expect ( baseDTC . hasInterface ( baseDTC . getDInterface ( ) ) ) . toBeTruthy ( ) ;
104- } ) ;
105-
106- describe ( 'hasChildTC(DName)' , ( ) => {
107- const baseDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
108- const personModel = baseDTC . discriminator ( PersonModel ) ;
109-
110- it ( 'should check and return boolean if childDTC is available' , ( ) => {
111- expect ( baseDTC . hasChildTC ( personModel . getTypeName ( ) ) ) . toBeTruthy ( ) ;
112- } ) ;
113-
114- it ( 'should be falsified as childDTC not found' , ( ) => {
115- expect ( baseDTC . hasChildTC ( 'NOT_AVAILABLE' ) ) . toBeFalsy ( ) ;
116- } ) ;
117- } ) ;
118-
119- describe ( 'addFields(newFields)' , ( ) => {
120- const characterDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
121- const personTC = characterDTC . discriminator ( PersonModel ) ;
122- const droidTC = characterDTC . discriminator ( DroidModel ) ;
123- const newFields = {
124- field1 : 'String' ,
125- field2 : 'String' ,
126- } ;
127-
128- beforeAll ( ( ) => {
129- characterDTC . addFields ( newFields ) ;
130- } ) ;
131-
132- it ( 'should add fields to baseTC' , ( ) => {
133- expect ( characterDTC . getFieldNames ( ) ) . toEqual (
134- expect . arrayContaining ( Object . keys ( newFields ) )
135- ) ;
136- } ) ;
137-
138- it ( 'should add fields to DInterface' , ( ) => {
139- expect ( Object . keys ( characterDTC . getDInterface ( ) . getFields ( ) ) ) . toEqual (
140- expect . arrayContaining ( Object . keys ( newFields ) )
141- ) ;
142- } ) ;
143-
144- it ( 'should add fields to childTC' , ( ) => {
145- expect ( personTC . getFieldNames ( ) ) . toEqual ( expect . arrayContaining ( Object . keys ( newFields ) ) ) ;
146- expect ( droidTC . getFieldNames ( ) ) . toEqual ( expect . arrayContaining ( Object . keys ( newFields ) ) ) ;
147- } ) ;
148- } ) ;
149-
150- describe ( 'removeField()' , ( ) => {
151- const characterDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
152- const personTC = characterDTC . discriminator ( PersonModel ) ;
153- const droidTC = characterDTC . discriminator ( DroidModel ) ;
154- const field = 'friends' ;
155-
156- beforeAll ( ( ) => {
157- characterDTC . removeField ( field ) ;
158- } ) ;
159-
160- it ( 'should remove fields from baseTC' , ( ) => {
161- expect ( characterDTC . hasField ( field ) ) . toBeFalsy ( ) ;
162- } ) ;
163-
164- it ( 'should remove fields from DInterface' , ( ) => {
165- expect ( characterDTC . getDInterface ( ) . getFields ( ) [ field ] ) . toBeFalsy ( ) ;
166- } ) ;
167-
168- it ( 'should remove fields from childTC' , ( ) => {
169- expect ( personTC . hasField ( field ) ) . toBeFalsy ( ) ;
170- expect ( droidTC . hasField ( field ) ) . toBeFalsy ( ) ;
171- } ) ;
172- } ) ;
173-
174- describe ( 'extendFields(fieldName, extensionField)' , ( ) => {
175- const characterDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
176- const personTC = characterDTC . discriminator ( PersonModel ) ;
177- const droidTC = characterDTC . discriminator ( DroidModel ) ;
178- const fieldName = 'kind' ;
179- const fieldExtension = {
180- type : 'String' ,
181- description : 'Hello I am changed' ,
182- } ;
183-
184- beforeAll ( ( ) => {
185- characterDTC . extendField ( fieldName , fieldExtension ) ;
186- } ) ;
187-
188- it ( 'should extend field on baseTC' , ( ) => {
189- expect ( characterDTC . getFieldType ( fieldName ) . toString ( ) ) . toEqual ( graphql . GraphQLString . name ) ;
190-
191- expect ( ( characterDTC . getField ( fieldName ) : any ) . description ) . toEqual (
192- fieldExtension . description
193- ) ;
194- } ) ;
195-
196- it ( 'should extend field type on DInterface' , ( ) => {
197- expect ( characterDTC . getDInterface ( ) . getFields ( ) [ fieldName ] ) . toBeTruthy ( ) ;
198- expect (
199- characterDTC
200- . getDInterface ( )
201- . getFieldType ( fieldName )
202- . toString ( )
203- ) . toEqual ( fieldExtension . type ) ;
204- } ) ;
205-
206- it ( 'should extend field on childTC' , ( ) => {
207- expect ( personTC . getFieldType ( fieldName ) . toString ( ) ) . toEqual ( graphql . GraphQLString . name ) ;
208-
209- expect ( ( personTC . getField ( fieldName ) : any ) . description ) . toEqual ( fieldExtension . description ) ;
210-
211- expect ( droidTC . getFieldType ( fieldName ) . toString ( ) ) . toEqual ( graphql . GraphQLString . name ) ;
212-
213- expect ( ( droidTC . getField ( fieldName ) : any ) . description ) . toEqual ( fieldExtension . description ) ;
214- } ) ;
215- } ) ;
216-
217- describe ( 'discriminator()' , ( ) => {
218- let sc ;
219- let characterDTC ;
220-
221- beforeEach ( ( ) => {
222- sc = new SchemaComposer ( ) ;
223- characterDTC = composeWithMongooseDiscriminators ( CharacterModel , {
224- customizationOptions : { schemaComposer : sc } ,
225- } ) ;
226- } ) ;
227-
228- it ( 'should return an instance of TypeComposer as childTC' , ( ) => {
229- /*
230- Test keeps on failing, FIXME: Recheck
231- Expected constructor: TypeComposer
232- Received constructor: TypeComposer
233- Received value: {"gqType": "Person"} */
234- // expect(characterDTC.discriminator(PersonModel)).toBeInstanceOf(TypeComposer);
235- // expect(characterDTC.discriminator(DroidModel)).toBeInstanceOf(TypeComposer);
236- } ) ;
237-
238- it ( 'should register itself in childTC(childTCs) array' , ( ) => {
239- const childTC = characterDTC . discriminator ( DroidModel ) ;
240- expect ( characterDTC . hasChildTC ( childTC . getTypeName ( ) ) ) . toBeTruthy ( ) ;
241- } ) ;
242-
243- it ( 'should apply filters passed' , ( ) => {
244- const tc = characterDTC . discriminator ( PersonModel , {
245- fields : {
246- remove : [ 'dob' , 'starShips' ] ,
247- } ,
248- } ) ;
249-
250- expect ( tc . getFieldNames ( ) ) . not . toEqual ( expect . arrayContaining ( [ 'dob' , 'starShips' ] ) ) ;
251- } ) ;
252- } ) ;
253- } ) ;
254-
255- describe ( 'DiscriminatorTypes' , ( ) => {
256- it ( 'should have as an interface DInterface' , ( ) => {
257- const baseDTC = composeWithMongooseDiscriminators ( CharacterModel ) ;
258- expect ( baseDTC . discriminator ( DroidModel ) . getInterfaces ( ) ) . toEqual (
259- expect . arrayContaining ( Array . of ( baseDTC . getDInterface ( ) ) )
260- ) ;
261- } ) ;
262- } ) ;
26364} ) ;
0 commit comments