Ben
Ben

Reputation: 308

Meteor client integration async testing with Velocity / Jasmine; How to get return value?

Updated: User count before and after signup still failing

Trying to test a new user signing up through the UI (see jQuery "signUp"). Users count from Method.call("usersCount") before and after signup both return 'undefined'.

I see 'undefined' -> user object -> 'undefined' in log. Not sure why user count is not getting assigned to the variable(s) in the spec code.

Second test checking the signup/logged-in users passes.

/tests/jasmine/client/integration/spec.js

// New user signup
function signUp (user, callback) {
    $('.dropdown-toggle').trigger('click');
    $('#signup-link').trigger('click');
    $('#login-username').val(user.username);
    $('#login-password').val(user.password);
    $('#login-password-again').val(user.password);
    $('#login-buttons-password').trigger('click');
    callback;
}

describe('User signup', function() {

    var user = { username: 'larry', password: 'password' };

    beforeEach(function(done) {
        Meteor.call("clearDB", done);
    });

    it('should increase users by one', function (done) {
        var userCountBefore = Meteor.call("usersCount");
        var userCountAfter = signUp(user, Meteor.call("usersCount"));
        expect(userCountBefore + 1).toEqual(userCountAfter);
    });

    it('should automatically log-in new user', function () {
        expect(Meteor.user().username).toEqual(user.username);
    });
});

/packages/test-helpers.js (custom debug testing package; clearDB method from [https://gist.github.com/qnub/97d828f11c677007cb07][1])

  if ((typeof process !== 'undefined') && process.env.IS_MIRROR) {
  Meteor.methods({
    usersCount: function () {
        var count = Meteor.users.find({}).count();
        return count;
    },
    clearDB: function(){
      console.log('Clear DB');

      var collectionsRemoved = 0;
      var db = Meteor.users.find()._mongo.db;
      db.collections(function (err, collections) {

        // Filter out velocity and system.indexes from collections
        var appCollections = _.reject(collections, function (col) {
          return col.collectionName.indexOf('velocity') === 0 ||
            col.collectionName === 'system.indexes';
        });

        // Remove each collection
        _.each(appCollections, function (appCollection) {
          appCollection.remove(function (e) {
            if (e) {
              console.error('Failed removing collection', e);
              fut.return('fail: ' + e);
            }
            collectionsRemoved++;
            console.log('Removed collection');
            if (appCollections.length === collectionsRemoved) {
              console.log('Finished resetting database');
            }
          });
        });

      });

      console.log('Finished clearing');
    }
  });
};

Upvotes: 0

Views: 443

Answers (1)

Ben
Ben

Reputation: 308

Ok, this is one way to solve this:

it('should increase users by one', function (done) {
    Meteor.call("usersCount", function(error, userCountBefore) {
        signUp(user);
        Meteor.call("usersCount", function (error, userCountAfter) {
            expect(userCountAfter).toEqual(userCountBefore + 1);
            done();
        });
    });
});

Future viewers, checkout the following links for reference/alternate approaches: https://github.com/caolan/async https://atmospherejs.com/peerlibrary/async http://www.html5rocks.com/en/tutorials/es6/promises/

Thanks to @sanjo for helping me see the light!

Upvotes: 1

Related Questions