11/* @flow */
22/* eslint-disable no-unused-expressions, no-template-curly-in-string */
33
4- import { EnumTypeComposer , schemaComposer , ListComposer } from 'graphql-compose' ;
4+ import { EnumTypeComposer , schemaComposer , ListComposer , SchemaComposer } from 'graphql-compose' ;
55import { UserModel } from '../__mocks__/userModel' ;
66import {
77 deriveComplexType ,
@@ -14,6 +14,7 @@ import {
1414 enumToGraphQL ,
1515 documentArrayToGraphQL ,
1616 referenceToGraphQL ,
17+ convertModelToGraphQL ,
1718} from '../fieldsConverter' ;
1819import GraphQLMongoID from '../types/mongoid' ;
1920import GraphQLBSONDecimal from '../types/bsonDecimal' ;
@@ -252,4 +253,62 @@ describe('fieldConverter', () => {
252253 expect ( referenceToGraphQL ( fields . user ) ) . toBe ( 'MongoID' ) ;
253254 } ) ;
254255 } ) ;
256+
257+ describe ( 'convertModelToGraphQL()' , ( ) => {
258+ const sc = new SchemaComposer ( ) ;
259+ const tc = convertModelToGraphQL ( UserModel , 'User' , sc ) ;
260+
261+ it ( 'should work with String' , ( ) => {
262+ expect ( tc . getFieldTypeName ( 'name' ) ) . toBe ( 'String' ) ;
263+ expect ( tc . getFieldTypeName ( 'skills' ) ) . toBe ( '[String]' ) ;
264+ } ) ;
265+
266+ it ( 'should work with Number' , ( ) => {
267+ expect ( tc . getFieldTypeName ( 'age' ) ) . toBe ( 'Float' ) ;
268+ } ) ;
269+
270+ it ( 'should work with ObjectId' , ( ) => {
271+ expect ( tc . getFieldTypeName ( 'user' ) ) . toBe ( 'MongoID' ) ;
272+ } ) ;
273+
274+ it ( 'should work with Enum' , ( ) => {
275+ expect ( ( tc : any ) . getFieldTC ( 'gender' ) . getFieldNames ( ) ) . toEqual ( [ 'male' , 'female' , 'ladyboy' ] ) ;
276+
277+ expect ( ( tc : any ) . getFieldTC ( 'employment' ) . getFieldNames ( ) ) . toEqual ( [
278+ 'full' ,
279+ 'partial' ,
280+ 'remote' ,
281+ ] ) ;
282+ } ) ;
283+
284+ it ( 'should work with Boolean' , ( ) => {
285+ expect ( tc . getFieldTypeName ( 'relocation' ) ) . toBe ( 'Boolean' ) ;
286+ } ) ;
287+
288+ it ( 'should extract sub schemas' , ( ) => {
289+ const contactsTC = tc . getFieldOTC ( 'contacts' ) ;
290+ expect ( contactsTC . getFieldNames ( ) ) . toEqual ( [ 'phones' , 'email' , 'skype' , 'locationId' , '_id' ] ) ;
291+ } ) ;
292+
293+ it ( 'should skip __secretField' , ( ) => {
294+ expect ( tc . hasField ( '__secretField' ) ) . toBeFalsy ( ) ;
295+ } ) ;
296+
297+ it ( 'should work with Mixed' , ( ) => {
298+ expect ( tc . getFieldTypeName ( 'someDynamic' ) ) . toBe ( 'JSON' ) ;
299+ } ) ;
300+
301+ it ( 'should work with Array' , ( ) => {
302+ expect ( tc . getFieldTypeName ( 'periods' ) ) . toBe ( '[UserPeriods]' ) ;
303+ expect ( sc . getOTC ( 'UserPeriods' ) . getFieldNames ( ) ) . toEqual ( [ 'from' , 'to' , '_id' ] ) ;
304+
305+ expect ( tc . getFieldTypeName ( 'someDeep' ) ) . toBe ( 'UserSomeDeep' ) ;
306+ expect ( tc . getFieldOTC ( 'someDeep' ) . getFieldTypeName ( 'periods' ) ) . toBe ( '[UserSomeDeepPeriods]' ) ;
307+ expect ( sc . getOTC ( 'UserSomeDeepPeriods' ) . getFieldNames ( ) ) . toEqual ( [ 'from' , 'to' , '_id' ] ) ;
308+ } ) ;
309+
310+ it ( 'should work with Decimal128' , ( ) => {
311+ expect ( tc . getFieldTypeName ( 'salary' ) ) . toBe ( 'BSONDecimal' ) ;
312+ } ) ;
313+ } ) ;
255314} ) ;
0 commit comments