@@ -4,16 +4,21 @@ import { expect } from 'chai';
4
4
import { GraphQLNonNull , GraphQLList } from 'graphql' ;
5
5
import { Resolver } from 'graphql-compose' ;
6
6
import { UserModel } from '../../__mocks__/userModel' ;
7
+ import { PostModel } from '../../__mocks__/postModel' ;
7
8
import findByIds from '../findByIds' ;
8
9
import GraphQLMongoID from '../../types/mongoid' ;
9
10
import { composeWithMongoose } from '../../composeWithMongoose' ;
10
11
11
12
const UserTypeComposer = composeWithMongoose ( UserModel ) ;
13
+ const PostTypeComposer = composeWithMongoose ( PostModel ) ;
12
14
13
15
describe ( 'findByIds() ->' , ( ) => {
14
16
let user1 ;
15
17
let user2 ;
16
18
let user3 ;
19
+ let post1 ;
20
+ let post2 ;
21
+ let post3 ;
17
22
18
23
before ( 'clear UserModel collection' , ( done ) => {
19
24
UserModel . collection . drop ( ( ) => {
@@ -33,6 +38,24 @@ describe('findByIds() ->', () => {
33
38
] ) . then ( ( ) => done ( ) ) ;
34
39
} ) ;
35
40
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
+
36
59
it ( 'should return Resolver object' , ( ) => {
37
60
const resolver = findByIds ( UserModel , UserTypeComposer ) ;
38
61
expect ( resolver ) . to . be . instanceof ( Resolver ) ;
@@ -71,13 +94,6 @@ describe('findByIds() ->', () => {
71
94
expect ( result ) . to . be . empty ;
72
95
} ) ;
73
96
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
-
81
97
it ( 'should return array of documents' , async ( ) => {
82
98
const result = await findByIds ( UserModel , UserTypeComposer )
83
99
. resolve ( { args : { _ids : [ user1 . _id , user2 . _id , user3 . _id ] } } ) ;
@@ -97,6 +113,13 @@ describe('findByIds() ->', () => {
97
113
expect ( result ) . to . have . lengthOf ( 1 ) ;
98
114
} ) ;
99
115
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
+
100
123
it ( 'should return mongoose documents' , async ( ) => {
101
124
const result = await findByIds ( UserModel , UserTypeComposer )
102
125
. resolve ( { args : { _ids : [ user1 . _id , user2 . _id ] } } ) ;
0 commit comments