jeudi 31 décembre 2015

How to test stores/actions?

I need to test the alt store; for which I have raised an action using alt dispatcher and trying to listen it at the action listener. But I got my alt object as undefined. Any idea plzz..

Action Class:

var alt = require('../alt');
var LocationSource = require('../sources/LocationSource');

class LocationActions {
updateLocations(locations) {
 this.dispatch(locations);
}

fetchLocations() {
this.dispatch();
var actionDispatcher = this;

LocationSource.fetchLocations().then(function(items) {

actionDispatcher.actions.updateLocations(items);

});
}

locationsFailed(errorMessage) {
this.dispatch(errorMessage);
}

favoriteLocation(location) {
this.dispatch(location);
}

fetchedLocation(location) {
this.dispatch(location);
}

fetchLocation(locationId) {

var actionDispatcher = this;

actionDispatcher.dispatch();

LocationSource.fetchLocation(locationId).then(function(item) {

actionDispatcher.actions.fetchedLocation(item);

});
}
}

module.exports = alt.createActions(LocationActions);

My Store:

var alt = require('../alt');
var LocationActions = require('../actions/LocationActions');

class FavoritesStore {
constructor() {
this.locations = [];

this.bindListeners({
  addFavoriteLocation: LocationActions.FAVORITE_LOCATION
});
}

addFavoriteLocation(location) {
this.locations.push(location); 
}

onRemoveFavoriteLocation(){

}

}

module.exports = alt.createStore(FavoritesStore, 'FavoritesStore');

MY Alt js:

var Alt = require('alt');
var alt = new Alt();
var chromeDebug = require('alt/utils/chromeDebug')

chromeDebug(alt);

module.exports = alt;

Heres the test file:

"use strict";
jest.dontMock('../src/js/stores/FavoritesStore');
jest.dontMock('../src/js/actions/LocationActions');
jest.dontMock('../src/js/alt');
import alt from '../src/js/alt';
var fav =require('../src/js/stores/FavoritesStore');

import LocationActions from '../src/js/actions/LocationActions';

describe('fav', function() {

 beforeEach(function() {
sinon.spy(alt.dispatcher, 'dispatch');

alt.dispatch(LocationActions.FAVORITE_LOCATION, {id:15, name:'khi'});
});

afterEach(function() {
// clean up our sinon spy so we do not affect other tests
alt.dispatch.restore();

});
it('add a location to favorites list ', function() {

  var id: 15, name: 'San Francisco' ,
  action = LocationActions.FAVORITE_LOCATION;
    console.log({id:15, name:'khi'});
  // fire the action
  LocationActions.favoriteLocation({id:15, name:'khi'});

});
});

Aucun commentaire:

Enregistrer un commentaire