samedi 25 avril 2015

Meteor client integration signup test with Velocity and Jasmine

I'm trying to test a user signing up through the DOM (i.e., my jQuery 'signUp' method below) with inconsistent results. I suspect there is some issue with the mirrored app and/or referencing the correct db (jasmine-client-integration db vs default, etc...). I can see that the signUp method working from the log of a reactive query in my app's main (non-testing) code, but output is inconsistent and velocity/jasmine finds either null or no change in user count (before = 0, after = 0).

I've tried:

  • All manner of applying the 'done()' method to rectify an asynchronous timing issue to no avail
  • Local collection (i.e., Meteor.users.find({}).count()) as well as Meteor.call to Meteor.methods in lib/test-helper.js

/tests/jasmine/client/integration/authSpec.js

CustomTestHelpers = {
    signUp : function (user) {
        $('.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');
    }
};

describe('User signup', function() {

    please = CustomTestHelpers;

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

    beforeEach(function() {
        Meteor.call("clearDB");
        userCountBefore = Meteor.call("usersCount");
        console.log('users before ' + userCountBefore.toString());
        please.signUp(user);
    });

    afterEach(function() {
        console.log('users after ' + userCountAfter.toString());
    })

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

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

/lib/test-helpers.js (adapted from http://ift.tt/1DnkHpF)

if ((typeof process !== 'undefined') && process.env.IS_MIRROR) {
  Meteor.methods({
    'usersCount': function () {
      return Meteor.users.find({}).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');
    }
  });
}

Aucun commentaire:

Enregistrer un commentaire