@@ -4,16 +4,21 @@ import { expect } from 'chai';
44import { GraphQLNonNull , GraphQLList } from 'graphql' ;
55import { Resolver } from 'graphql-compose' ;
66import { UserModel } from '../../__mocks__/userModel' ;
7+ import { PostModel } from '../../__mocks__/postModel' ;
78import findByIds from '../findByIds' ;
89import GraphQLMongoID from '../../types/mongoid' ;
910import { composeWithMongoose } from '../../composeWithMongoose' ;
1011
1112const UserTypeComposer = composeWithMongoose ( UserModel ) ;
13+ const PostTypeComposer = composeWithMongoose ( PostModel ) ;
1214
1315describe ( 'findByIds() ->' , ( ) => {
1416 let user1 ;
1517 let user2 ;
1618 let user3 ;
19+ let post1 ;
20+ let post2 ;
21+ let post3 ;
1722
1823 before ( 'clear UserModel collection' , ( done ) => {
1924 UserModel . collection . drop ( ( ) => {
@@ -33,6 +38,24 @@ describe('findByIds() ->', () => {
3338 ] ) . then ( ( ) => done ( ) ) ;
3439 } ) ;
3540
41+ before ( 'clear PostModel collection' , ( done ) => {
42+ PostModel . collection . drop ( ( ) => {
43+ done ( ) ;
44+ } ) ;
45+ } ) ;
46+
47+ before ( 'add test post documents with integer _id to mongoDB' , ( done ) => {
48+ post1 = new PostModel ( { _id : 1 , title : 'Post 1' } ) ;
49+ post2 = new PostModel ( { _id : 2 , title : 'Post 2' } ) ;
50+ post3 = new PostModel ( { _id : 3 , title : 'Post 3' } ) ;
51+
52+ Promise . all ( [
53+ post1 . save ( ) ,
54+ post2 . save ( ) ,
55+ post3 . save ( ) ,
56+ ] ) . then ( ( ) => done ( ) ) ;
57+ } ) ;
58+
3659 it ( 'should return Resolver object' , ( ) => {
3760 const resolver = findByIds ( UserModel , UserTypeComposer ) ;
3861 expect ( resolver ) . to . be . instanceof ( Resolver ) ;
@@ -71,13 +94,6 @@ describe('findByIds() ->', () => {
7194 expect ( result ) . to . be . empty ;
7295 } ) ;
7396
74- it ( 'should return empty array if args._ids is not valid objectIds' , async ( ) => {
75- const result = await findByIds ( UserModel , UserTypeComposer )
76- . resolve ( { args : { _ids : [ 'd' , 'e' ] } } ) ;
77- expect ( result ) . to . be . instanceOf ( Array ) ;
78- expect ( result ) . to . be . empty ;
79- } ) ;
80-
8197 it ( 'should return array of documents' , async ( ) => {
8298 const result = await findByIds ( UserModel , UserTypeComposer )
8399 . resolve ( { args : { _ids : [ user1 . _id , user2 . _id , user3 . _id ] } } ) ;
@@ -97,6 +113,13 @@ describe('findByIds() ->', () => {
97113 expect ( result ) . to . have . lengthOf ( 1 ) ;
98114 } ) ;
99115
116+ it ( 'should return array of documents if args._ids are integers' , async ( ) => {
117+ const result = await findByIds ( PostModel , PostTypeComposer )
118+ . resolve ( { args : { _ids : [ 1 , 2 , 3 ] } } ) ;
119+ expect ( result ) . to . be . instanceOf ( Array ) ;
120+ expect ( result ) . to . have . lengthOf ( 3 ) ;
121+ } ) ;
122+
100123 it ( 'should return mongoose documents' , async ( ) => {
101124 const result = await findByIds ( UserModel , UserTypeComposer )
102125 . resolve ( { args : { _ids : [ user1 . _id , user2 . _id ] } } ) ;
0 commit comments