|
1 | 1 | /* @flow */ |
2 | 2 |
|
3 | 3 | import { Resolver, schemaComposer } from 'graphql-compose'; |
| 4 | +import { Query } from 'mongoose'; |
4 | 5 | import { UserModel } from '../../__mocks__/userModel'; |
5 | 6 | import connection, { prepareCursorQuery } from '../connection'; |
6 | 7 | import findMany from '../findMany'; |
@@ -217,6 +218,63 @@ describe('connection() resolver', () => { |
217 | 218 | expect(result.edges[0].node).toBeInstanceOf(UserModel); |
218 | 219 | expect(result.edges[1].node).toBeInstanceOf(UserModel); |
219 | 220 | }); |
| 221 | + |
| 222 | + it('should call `beforeQuery` method with non-executed `query` as arg', async () => { |
| 223 | + const mongooseActions = []; |
| 224 | + |
| 225 | + UserModel.base.set('debug', function debugMongoose(...args) { |
| 226 | + mongooseActions.push(args); |
| 227 | + }); |
| 228 | + |
| 229 | + const resolver = connection(UserModel, UserTC); |
| 230 | + |
| 231 | + if (!resolver) { |
| 232 | + throw new Error('resolver is undefined'); |
| 233 | + } |
| 234 | + |
| 235 | + const result = await resolver.resolve({ |
| 236 | + args: {}, |
| 237 | + beforeQuery: (query, rp) => { |
| 238 | + expect(query).toBeInstanceOf(Query); |
| 239 | + expect(rp.model).toBe(UserModel); |
| 240 | + // modify query before execution |
| 241 | + return query.where({ _id: user1.id }).limit(1989); |
| 242 | + }, |
| 243 | + }); |
| 244 | + |
| 245 | + expect(mongooseActions).toEqual([ |
| 246 | + [ |
| 247 | + 'users', |
| 248 | + 'find', |
| 249 | + { _id: user1._id }, |
| 250 | + { |
| 251 | + limit: 1989, |
| 252 | + projection: {}, |
| 253 | + }, |
| 254 | + ], |
| 255 | + ]); |
| 256 | + |
| 257 | + expect(result.edges).toHaveLength(1); |
| 258 | + }); |
| 259 | + |
| 260 | + it('should override result with `beforeQuery`', async () => { |
| 261 | + const resolver = connection(UserModel, UserTC); |
| 262 | + |
| 263 | + if (!resolver) { |
| 264 | + throw new Error('resolver is undefined'); |
| 265 | + } |
| 266 | + |
| 267 | + const result = await resolver.resolve({ |
| 268 | + args: {}, |
| 269 | + beforeQuery: (query, rp) => { |
| 270 | + expect(query).toBeInstanceOf(Query); |
| 271 | + expect(rp.model).toBe(UserModel); |
| 272 | + return [{ overrides: true }]; |
| 273 | + }, |
| 274 | + }); |
| 275 | + |
| 276 | + expect(result).toHaveProperty('edges.0.node', { overrides: true }); |
| 277 | + }); |
220 | 278 | }); |
221 | 279 | }); |
222 | 280 | }); |
0 commit comments