|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { UserModel } from '../../__mocks__/userModel.js'; |
| 5 | +import count from '../count'; |
| 6 | +import Resolver from '../../../../graphql-compose/src/resolver/resolver'; |
| 7 | +import { GraphQLObjectType, GraphQLInt } from 'graphql'; |
| 8 | + |
| 9 | +const UserType = new GraphQLObjectType({ |
| 10 | + name: 'MockUserType', |
| 11 | +}); |
| 12 | + |
| 13 | +describe('count() ->', () => { |
| 14 | + let user1; |
| 15 | + let user2; |
| 16 | + |
| 17 | + before('clear UserModel collection', (done) => { |
| 18 | + UserModel.collection.drop(() => { |
| 19 | + done(); |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + before('add test user document to mongoDB', () => { |
| 24 | + user1 = new UserModel({ |
| 25 | + name: 'userName1', |
| 26 | + skills: ['js', 'ruby', 'php', 'python'], |
| 27 | + gender: 'male', |
| 28 | + relocation: true, |
| 29 | + }); |
| 30 | + |
| 31 | + user2 = new UserModel({ |
| 32 | + name: 'userName2', |
| 33 | + skills: ['go', 'erlang'], |
| 34 | + gender: 'female', |
| 35 | + relocation: false, |
| 36 | + }); |
| 37 | + |
| 38 | + return Promise.all([ |
| 39 | + user1.save(), |
| 40 | + user2.save(), |
| 41 | + ]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return Resolver object', () => { |
| 45 | + const resolver = count(UserModel, UserType); |
| 46 | + expect(resolver).to.be.instanceof(Resolver); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('Resolver.args', () => { |
| 50 | + it('should have `filter` arg', () => { |
| 51 | + const resolver = count(UserModel, UserType); |
| 52 | + expect(resolver.hasArg('filter')).to.be.true; |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('Resolver.resolve():Promise', () => { |
| 57 | + it('should be fulfilled promise', async () => { |
| 58 | + const result = count(UserModel, UserType).resolve({}); |
| 59 | + await expect(result).be.fulfilled; |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return total number of documents in collection if args is empty', async () => { |
| 63 | + const result = await count(UserModel, UserType).resolve({ args: {} }); |
| 64 | + expect(result).to.equal(2); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return number of document by filter data', async () => { |
| 68 | + const result = await count(UserModel, UserType).resolve( |
| 69 | + { args: { filter: { gender: 'male' } } } |
| 70 | + ); |
| 71 | + expect(result).to.equal(1); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('Resolver.getOutputType()', () => { |
| 76 | + it('should return GraphQLInt type', () => { |
| 77 | + const outputType = count(UserModel, UserType).getOutputType(); |
| 78 | + expect(outputType).to.equal(GraphQLInt); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments