dimanche 28 juin 2015

Having trouble using Jest with external dependancies

I'm trying to test some code, and I'm reasonably new to unit testing. I'm having some trouble.

I'm using React with Flux and using Jest to test them. But I think I'm doing something wrong with Mocking my dependancies or something ( to be honest the mocking thing kind of confuses me ) but here is what I'm having trouble with:

//LoginStore-test.js
jest.dontMock('../../constants/LoginConstants');
jest.dontMock('jsonwebtoken');
jest.dontMock('underscore');
jest.dontMock('../LoginStore');

describe("login Store", function(){
    var LoginConstants = require('../../constants/LoginConstants');
    var AppDispatcher;
    var LoginStore;
    var callback;
    var jwt = require('jsonwebtoken');

    var _user = {
        email: 'test@test.com'
    };

    //mock actions                                                                                                                                                                                                                            
    var actionLogin = {
        actionType: LoginConstants.LOGIN_USER,
        'jwt': jwt.sign(_user, 'shhh', { expiresInMinutes: 60*5 })
    };

    beforeEach(function(){
        AppDispatcher = require('../../dispatchers/AppDispatcher');
        LoginStore = require('../LoginStore');
        callback = AppDispatcher.register.mock.calls[0][0];
    });

    ...

    it('should save the user', function(){
        callback(actionLogin);
        var user = LoginStore.getUser();
        expect(user).toEqual(_user);
    });

});


});

and then my LoginStore.js file

var AppDispatcher = require('../dispatchers/AppDispatcher');
var BaseStore = require('./BaseStore');
var LoginConstants = require('../constants/LoginConstants.js');
var _ = require('underscore');
var jwt = require('jsonwebtoken');

//initiate some variables                                                                                                                                                                                                                     
var _user;
var _jwt;

var LoginStore = _.extend({}, BaseStore, {
    getUser: function(){
    return _user;
    }
});

AppDispatcher.register(function(action){

    switch(action.actionType){
    case LoginConstants.LOGIN_USER:
        //set the user                                                                                                                                                                                                                        
        _user = jwt.decode(action.jwt);
        //save the token                                                                                                                                                                                                                      
        _jwt = action.jwt;
    break;

    //do nothing with the default                                                                                                                                                                                                         
    default:
        return true;
    }

    LoginStore.emitChange();

    return true;

});

module.exports = LoginStore;

But the jsonwebtoken functionality doesn't seem to be working at all. if I log actionLogin.jwt it just returns 'undefined'. Any idea what I'm doing wrong here?

Cheers

Aucun commentaire:

Enregistrer un commentaire