|
| 1 | +import { Resolver, schemaComposer, ObjectTypeComposer } from 'graphql-compose'; |
| 2 | +import { UserModel, IUser } from '../../__mocks__/userModel'; |
| 3 | +import { PostModel, IPost } from '../../__mocks__/postModel'; |
| 4 | +import findByIdsLean from '../findByIdsLean'; |
| 5 | +import { convertModelToGraphQL } from '../../fieldsConverter'; |
| 6 | +import { ExtendedResolveParams } from '..'; |
| 7 | + |
| 8 | +beforeAll(() => UserModel.base.createConnection()); |
| 9 | +afterAll(() => UserModel.base.disconnect()); |
| 10 | + |
| 11 | +describe('findByIdsLean() ->', () => { |
| 12 | + let UserTC: ObjectTypeComposer; |
| 13 | + let PostTypeComposer: ObjectTypeComposer; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + schemaComposer.clear(); |
| 17 | + UserTC = convertModelToGraphQL(UserModel, 'User', schemaComposer); |
| 18 | + PostTypeComposer = convertModelToGraphQL(PostModel, 'Post', schemaComposer); |
| 19 | + }); |
| 20 | + |
| 21 | + let user1: IUser; |
| 22 | + let user2: IUser; |
| 23 | + let user3: IUser; |
| 24 | + let post1: IPost; |
| 25 | + let post2: IPost; |
| 26 | + let post3: IPost; |
| 27 | + |
| 28 | + beforeEach(async () => { |
| 29 | + await UserModel.deleteMany({}); |
| 30 | + |
| 31 | + user1 = new UserModel({ name: 'nodkz1', contacts: { email: 'mail' } }); |
| 32 | + user2 = new UserModel({ name: 'nodkz2', contacts: { email: 'mail' } }); |
| 33 | + user3 = new UserModel({ name: 'nodkz3', contacts: { email: 'mail' } }); |
| 34 | + |
| 35 | + await Promise.all([user1.save(), user2.save(), user3.save()]); |
| 36 | + |
| 37 | + await PostModel.deleteMany({}); |
| 38 | + |
| 39 | + post1 = new PostModel({ _id: 1, title: 'Post 1' }); |
| 40 | + post2 = new PostModel({ _id: 2, title: 'Post 2' }); |
| 41 | + post3 = new PostModel({ _id: 3, title: 'Post 3' }); |
| 42 | + |
| 43 | + await Promise.all([post1.save(), post2.save(), post3.save()]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return Resolver object', () => { |
| 47 | + const resolver = findByIdsLean(UserModel, UserTC); |
| 48 | + expect(resolver).toBeInstanceOf(Resolver); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Resolver.args', () => { |
| 52 | + it('should have non-null `_ids` arg', () => { |
| 53 | + const resolver = findByIdsLean(UserModel, UserTC); |
| 54 | + expect(resolver.getArgTypeName('_ids')).toBe('[MongoID]!'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should have `limit` arg', () => { |
| 58 | + const resolver = findByIdsLean(UserModel, UserTC); |
| 59 | + expect(resolver.hasArg('limit')).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should have `sort` arg', () => { |
| 63 | + const resolver = findByIdsLean(UserModel, UserTC); |
| 64 | + expect(resolver.hasArg('sort')).toBe(true); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Resolver.resolve():Promise', () => { |
| 69 | + it('should be fulfilled promise', async () => { |
| 70 | + const result = findByIdsLean(UserModel, UserTC).resolve({}); |
| 71 | + await expect(result).resolves.toBeDefined(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return empty array if args._ids is empty', async () => { |
| 75 | + const result = await findByIdsLean(UserModel, UserTC).resolve({}); |
| 76 | + expect(result).toBeInstanceOf(Array); |
| 77 | + expect(Object.keys(result)).toHaveLength(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return array of documents', async () => { |
| 81 | + const result = await findByIdsLean(UserModel, UserTC).resolve({ |
| 82 | + args: { _ids: [user1._id, user2._id, user3._id] }, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result).toBeInstanceOf(Array); |
| 86 | + expect(result).toHaveLength(3); |
| 87 | + expect(result.map((d: any) => d.name)).toEqual( |
| 88 | + expect.arrayContaining([user1.name, user2.name, user3.name]) |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return array of documents if object id is string', async () => { |
| 93 | + const stringId = `${user1._id}`; |
| 94 | + const result = await findByIdsLean(UserModel, UserTC).resolve({ |
| 95 | + args: { _ids: [stringId] }, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result).toBeInstanceOf(Array); |
| 99 | + expect(result).toHaveLength(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return array of documents if args._ids are integers', async () => { |
| 103 | + const result = await findByIdsLean(PostModel, PostTypeComposer).resolve({ |
| 104 | + args: { _ids: [1, 2, 3] }, |
| 105 | + }); |
| 106 | + expect(result).toBeInstanceOf(Array); |
| 107 | + expect(result).toHaveLength(3); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should return mongoose documents', async () => { |
| 111 | + const result = await findByIdsLean(UserModel, UserTC).resolve({ |
| 112 | + args: { _ids: [user1._id, user2._id] }, |
| 113 | + }); |
| 114 | + expect(result[0]).not.toBeInstanceOf(UserModel); |
| 115 | + expect(result[1]).not.toBeInstanceOf(UserModel); |
| 116 | + // should translate aliases fields |
| 117 | + expect(result).toEqual([ |
| 118 | + expect.objectContaining({ name: 'nodkz1' }), |
| 119 | + expect.objectContaining({ name: 'nodkz2' }), |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should call `beforeQuery` method with non-executed `query` as arg', async () => { |
| 124 | + const result = await findByIdsLean(UserModel, UserTC).resolve({ |
| 125 | + args: { _ids: [user1._id, user2._id] }, |
| 126 | + beforeQuery(query: any, rp: ExtendedResolveParams) { |
| 127 | + expect(rp.model).toBe(UserModel); |
| 128 | + expect(rp.query).toHaveProperty('exec'); |
| 129 | + return query.where({ _id: user1._id }); |
| 130 | + }, |
| 131 | + }); |
| 132 | + |
| 133 | + expect(result).toHaveLength(1); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments