Friday 9 June 2017

How do I write a Jasmine test code to check if all the functions are called or not?

So My code is the following in a create post service in Node JS:

create(post, recipients, user, callback) {
    post.reaction_type = this.getReactionType(post.type);
    return this._setVisibility(recipients, (err, visibility) => {
      if (err) this.logger.error({post: err});

      post.visibility = visibility || "private";
      return this.postsModel.create(post)
        .then((post) => {
          post = this.makePlain(post);
          callback(null, post);
          this._publishAndSend(post, recipients, user)
          return;
        })
        .catch((err) => {
          this.logger.error({create_post: "failed to create post", error: err.stack})
          return callback(err);
        })   
    })
  }

The individual functions are the following:

_setVisibility(recipients, callback) {
    if(!recipients) return callback(null, "private");

    if (recipients.topics && recipients.topics[0]) {
      this.topicsModel.count(
        {
          where: {
            id: recipients.topics,
            type: "community"
          }
        }
      )
      .then((response) => {
        if (response > 0) return callback(null, "public");

        return callback(null, "private");
      })
      .catch((err) => {
        this.logger.error({post: "Failed to set visibility. Defaulting to private"});
        return callback(null, "private");
      })
    } else {
      return callback(null, "private");
    }
  }


_publishAndSend(post, recipients, user) {
    if (!recipients) return;
    // publish this posts to topics or users, 
    // i.e. post it to their wall.
    this._publish(post, recipients, user);
    this._sendOutNotifications(recipients, post, user);
  }

This is the Jasmine spec file I wrote. It obviously has some errors.

let app =  require('../../../app')

describe('CreatePostService', () => {

    let CreatePostService = app.Container.get('CreatePostService')
    let models = app.Container.get('models')

    describe('when creating a post', () => {

        let user = {};
        let recipients = {};
        let post = models['Posts'].build();
        let callback = function(){};

        beforeEach(done => {
            spyOn(CreatePostService, 'getReactionType');
            spyOn(CreatePostService, '_setVisibility').and.callFake(function(){
                //Get args : fake result of AJAX call and callback
                 var fakeResult = "private";
                 var callback = arguments[1];
                return callback(null,fakeResult);
            });;
            spyOn(CreatePostService.postsModel, 'create').and.returnValue(new Promise((resolve, reject) => { resolve(post); }));
            spyOn(CreatePostService, 'makePlain');
            spyOn(CreatePostService, '_publishAndSend').and.callThrough();
            spyOn(CreatePostService, '_publish');
            spyOn(CreatePostService, '_sendOutNotifications');
            //spyOn(CreatePostService.logger, 'info');

            CreatePostService.create(post,recipients,user, callback);
            done();



        })

        afterEach(done => {
            done();
        })

        it('should create, publish, and notify a post', done => {
            expect(CreatePostService.getReactionType).toHaveBeenCalled();
            expect(CreatePostService._setVisibility).toHaveBeenCalled();
            expect(CreatePostService.postsModel.create).toHaveBeenCalled();
            expect(CreatePostService.makePlain).toHaveBeenCalled();
            expect(CreatePostService._publishAndSend).toHaveBeenCalled();
            expect(CreatePostService._publish).toHaveBeenCalled();
            expect(CreatePostService._sendOutNotifications).toHaveBeenCalled();
            //expect(CreatePostService.logger.info).toHaveBeenCalled();
            done();
        })
    })


})

Can any correct my code or provide a better jasmine code to test if all the sub function have been called or not?

Any help would be appreciated..



via Sharvil

No comments:

Post a Comment