|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { UserModel } from '../../__mocks__/userModel.js'; |
| 5 | +import updateById from '../updateById'; |
| 6 | +import Resolver from '../../../../graphql-compose/src/resolver/resolver'; |
| 7 | +import TypeComposer from '../../../../graphql-compose/src/typeComposer'; |
| 8 | +import InputTypeComposer from '../../../../graphql-compose/src/typeInputComposer'; |
| 9 | +import { convertModelToGraphQL } from '../../fieldsConverter'; |
| 10 | +import { |
| 11 | + GraphQLNonNull, |
| 12 | + GraphQLInputObjectType, |
| 13 | + getNullableType, |
| 14 | +} from 'graphql'; |
| 15 | +import GraphQLMongoID from '../../types/mongoid'; |
| 16 | + |
| 17 | +const UserType = convertModelToGraphQL(UserModel, 'User'); |
| 18 | + |
| 19 | +describe('updateById() ->', () => { |
| 20 | + let user1; |
| 21 | + let user2; |
| 22 | + |
| 23 | + before('clear UserModel collection', (done) => { |
| 24 | + UserModel.collection.drop(done); |
| 25 | + }); |
| 26 | + |
| 27 | + before('add test user document to mongoDB', () => { |
| 28 | + user1 = new UserModel({ |
| 29 | + name: 'userName1', |
| 30 | + skills: ['js', 'ruby', 'php', 'python'], |
| 31 | + gender: 'male', |
| 32 | + relocation: true, |
| 33 | + }); |
| 34 | + |
| 35 | + user2 = new UserModel({ |
| 36 | + name: 'userName2', |
| 37 | + skills: ['go', 'erlang'], |
| 38 | + gender: 'female', |
| 39 | + relocation: true, |
| 40 | + }); |
| 41 | + |
| 42 | + return Promise.all([ |
| 43 | + user1.save(), |
| 44 | + user2.save(), |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should return Resolver object', () => { |
| 49 | + const resolver = updateById(UserModel, UserType); |
| 50 | + expect(resolver).to.be.instanceof(Resolver); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('Resolver.args', () => { |
| 54 | + it('should have `input` arg', () => { |
| 55 | + const resolver = updateById(UserModel, UserType); |
| 56 | + expect(resolver.hasArg('input')).to.be.true; |
| 57 | + const argConfig = resolver.getArg('input'); |
| 58 | + expect(argConfig).has.deep.property('type.name', 'UpdateByIdUserInput'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should have `input._id` required arg', () => { |
| 62 | + const resolver = updateById(UserModel, UserType); |
| 63 | + const argConfig = resolver.getArg('input') || {}; |
| 64 | + expect(argConfig).property('type').instanceof(GraphQLInputObjectType); |
| 65 | + if (argConfig.type) { |
| 66 | + const _idFieldType = new InputTypeComposer(argConfig.type).getFieldType('_id'); |
| 67 | + expect(_idFieldType).instanceof(GraphQLNonNull); |
| 68 | + expect(getNullableType(_idFieldType)).equal(GraphQLMongoID); |
| 69 | + } |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('Resolver.resolve():Promise', () => { |
| 74 | + it('should be promise', () => { |
| 75 | + const result = updateById(UserModel, UserType).resolve({}); |
| 76 | + expect(result).instanceof(Promise); |
| 77 | + result.catch(() => 'catch error if appear, hide it from mocha'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should rejected with Error if args.input._id is empty', async () => { |
| 81 | + const result = updateById(UserModel, UserType).resolve({ args: { input: {} } }); |
| 82 | + await expect(result).be.rejectedWith(Error, 'requires args.input._id'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return payload.recordId', async () => { |
| 86 | + const result = await updateById(UserModel, UserType).resolve({ |
| 87 | + args: { |
| 88 | + input: { _id: user1.id, name: 'some name' }, |
| 89 | + }, |
| 90 | + }); |
| 91 | + expect(result).have.property('recordId', user1.id); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should change data via args.input in model', async () => { |
| 95 | + const result = await updateById(UserModel, UserType).resolve({ |
| 96 | + args: { |
| 97 | + input: { _id: user1.id, name: 'newName' }, |
| 98 | + }, |
| 99 | + }); |
| 100 | + expect(result).have.deep.property('record.name', 'newName'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should change data via args.input in database', (done) => { |
| 104 | + const checkedName = 'nameForMongoDB'; |
| 105 | + updateById(UserModel, UserType).resolve({ |
| 106 | + args: { |
| 107 | + input: { _id: user1.id, name: checkedName }, |
| 108 | + }, |
| 109 | + }).then(() => { |
| 110 | + UserModel.collection.findOne({ _id: user1._id }, (err, doc) => { |
| 111 | + expect(doc.name).to.be.equal(checkedName); |
| 112 | + done(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should return payload.record', async () => { |
| 118 | + const checkedName = 'anyName123'; |
| 119 | + const result = await updateById(UserModel, UserType).resolve({ |
| 120 | + args: { |
| 121 | + input: { _id: user1.id, name: checkedName }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + expect(result).have.deep.property('record.id', user1.id); |
| 125 | + expect(result).have.deep.property('record.name', checkedName); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('Resolver.getOutputType()', () => { |
| 130 | + it('should have correct output type name', () => { |
| 131 | + const outputType = updateById(UserModel, UserType).getOutputType(); |
| 132 | + expect(outputType.name).to.equal(`UpdateById${UserType.name}Payload`); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should have recordId field', () => { |
| 136 | + const outputType = updateById(UserModel, UserType).getOutputType(); |
| 137 | + const typeComposer = new TypeComposer(outputType); |
| 138 | + expect(typeComposer.hasField('recordId')).to.be.true; |
| 139 | + expect(typeComposer.getField('recordId').type).to.equal(GraphQLMongoID); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should have record field', () => { |
| 143 | + const outputType = updateById(UserModel, UserType).getOutputType(); |
| 144 | + const typeComposer = new TypeComposer(outputType); |
| 145 | + expect(typeComposer.hasField('record')).to.be.true; |
| 146 | + expect(typeComposer.getField('record').type).to.equal(UserType); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments