@
@
@Richswitch2018-01-17 18:52:39
JavaScript
@Richswitch, 2018-01-17 18:52:39

How to write a test for this resolver?

Hey!
I write unit tests for GraphQl resolvers , connect to the database using Sequelize , and write tests using Jest .
How to write a test for a resolver with a promise?
resolver

…
  updateUser(root, { id, input }) {
    return models.UserModel.findById(id)              // Ищем User в БД по ID
      .then(user => user.update(input));                   // Добавляем объект с новыми значениями
  }
…

My test:
…
  it('update User', () => {
    return Mutation.updateUser(null, {
      currentId,                                          // currentId  = текущий ID юзера 
      input: {
        login: 'T-Jane',
        homeFloor: 2,
        avatarUrl: 'http://pic.com/2',
      },
    })
      .then((result) => {
        return console.log(result);
      });
  });
…

The error I am getting in the console is:
TypeError: Cannot read property 'update' of null

       7 |   updateUser(root, { id, input }) {
       8 |     return models.UserModel.findById(id)
    >  9 |       .then(user => user.update(input));
      10 |   },
      11 |   removeUser(root, { id }) {
      12 |     return models.UserModel.findById(id)

      at models.UserModel.findById.then.user (graphql/resolvers/mutation.js:9:23)
      at tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (node_modules/bluebird/js/release/promise.js:512:31)
      at Promise._settlePromise (node_modules/bluebird/js/release/promise.js:569:18)
      at Promise._settlePromiseCtx (node_modules/bluebird/js/release/promise.js:606:10)
      at Async.Object.<anonymous>.Async._drainQueue (node_modules/bluebird/js/release/async.js:138:12)
      at Async.Object.<anonymous>.Async._drainQueues (node_modules/bluebird/js/release/async.js:143:10)
      at Immediate.Async.drainQueues [as _onImmediate] (node_modules/bluebird/js/release/async.js:17:14)

As I understand it, the test sees the string with return models.UserModel.findById(id)and returns the update null result.
There is documentation on this topic, but I didn’t quite understand what was there and how, because promises are a new topic for me. Here is a link to the Jest documentation on the topic of promises.
Thank you all for your answers!)
Give advice!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question