Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
Fix backbone trigger method when used inside jest
Browse files Browse the repository at this point in the history
  • Loading branch information
contolini committed May 15, 2015
1 parent 515a377 commit b88527c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
38 changes: 19 additions & 19 deletions src/js/stores/Classes/TeamStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ var Team = Store.backbone.Model.extend({
initialize: function() {
this.isAddingAsset = {}
},
isMember: function( userId ) {
isMember: function(userId) {
var roles = this.get('roles');
return _.any(roles, function( roleData ) {
return _.any(roles, function(roleData) {
return (roleData.members || []).indexOf(userId) > 0
})
},
getMembersByRole: function( role ) {
getMembersByRole: function(role) {
var roleData = this.get('roles')[role] || {};
var memberNames = roleData.members || [];
var members = userStore.filter(function( user ) {
var members = userStore.filter(function(user) {
return memberNames.indexOf(user.get('name')) >= 0;
});
return members;
},
getNonMembersByRole: function( role ) {
getNonMembersByRole: function(role) {
var roleData = this.get('roles')[role] || {};
var memberNames = roleData.members || [];
var members = userStore.filter(function( user ) {
var members = userStore.filter(function(user) {
return memberNames.indexOf(user.get('name')) < 0;
});
return members;
Expand All @@ -41,34 +41,34 @@ var Team = Store.backbone.Model.extend({
return members;
},
actions: {
TEAM_ADD_MEMBER: function( action ) {
TEAM_ADD_MEMBER: function(action) {
var that = this;
common.teamAddMember(action).done(function( newTeamData ) {
common.teamAddMember(action).done(function(newTeamData) {
that.set(newTeamData);
});
},
TEAM_REMOVE_MEMBER: function( action ) {
TEAM_REMOVE_MEMBER: function(action) {
var that = this;
common.teamRemoveMember(action).done(function( newTeamData ) {
common.teamRemoveMember(action).done(function(newTeamData) {
that.set(newTeamData);
});
},
TEAM_ADD_ASSET: function( action ) {
TEAM_ADD_ASSET: function(action) {
this.isAddingAsset[action.resourceName] = true;
this.trigger('change');
// this.trigger('change');
var that = this;
common.teamAddAsset(action)
.done(function( newTeamData ) {
.done(function(newTeamData) {
that.set(newTeamData);
})
.always(function() {
that.isAddingAsset[action.resourceName] = false;
that.trigger('change');
// that.trigger('change');
});
},
TEAM_REMOVE_ASSET: function( action ) {
TEAM_REMOVE_ASSET: function(action) {
var that = this;
common.teamRemoveAsset(action).done(function( newTeamData ) {
common.teamRemoveAsset(action).done(function(newTeamData) {
that.set(newTeamData);
});
}
Expand All @@ -81,17 +81,17 @@ var TeamStore = Store.Collection.extend({
myTeams: function() {
var loggedInUserStore = require('../loggedInUserStore');
var myId = loggedInUserStore.id;
return this.filter(function( model ) {
return this.filter(function(model) {
return model.isMember(myId);
})
},
actions: {
TEAM_CREATE: function( action ) {
TEAM_CREATE: function(action) {
if (!action.teamName) {
throw new Error('Please provide a team name.');
}
var that = this;
common.teamCreate(action).done(function( newTeamData ) {
common.teamCreate(action).done(function(newTeamData) {
that.add(newTeamData);
});
}
Expand Down
16 changes: 9 additions & 7 deletions src/js/stores/__tests__/TeamStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('TeamStore', function() {
name: 'foo'
};


beforeEach(function() {
_ = require('lodash');
TeamStore = require('../Classes/TeamStore');
Expand Down Expand Up @@ -73,7 +72,7 @@ describe('TeamStore', function() {
var action = teamStore.actions.TEAM_CREATE;

spyOn(common, 'teamCreate').andReturn({
done: function( cb ) {
done: function(cb) {
cb({})
}
});
Expand All @@ -83,7 +82,7 @@ describe('TeamStore', function() {
expect(teamStore.get('foo').get('id')).toEqual('cheesy');
});

it('should make a call to common to for all model actions and update the model with the result', function() {
it('should make a call to common for all model actions and update the model with the result', function() {
var store = new TeamStore(team);
var teamModel = store.models[0];

Expand All @@ -94,25 +93,28 @@ describe('TeamStore', function() {
TEAM_REMOVE_ASSET: 'teamRemoveAsset'
}

_.forIn(teamModel.actions, function( value, key ) {
_.forIn(teamModel.actions, function(value, key) {
var action = teamModel.actions[key];

teamModel.trigger = function noop(){};

spyOn(common, actionHash[key]).andReturn({
done: function( cb ) {
done: function(cb) {
cb({id: '123', name: 'foo', updatedKey: 'bar'})
return {
always: function() {
}
}
}

});

action.call(teamModel, key)

expect(common[actionHash[key]]).toHaveBeenCalled();
expect(teamModel.get('updatedKey')).toEqual('bar');
});
});

it('should sort members by role', function() {
team.roles = {
'member': {
Expand All @@ -126,7 +128,7 @@ describe('TeamStore', function() {
var store = new TeamStore(team);
var teamModel = store.models[0];
spyOn(userStore, 'filter').andCallFake(
function( user ) {
function(user) {
return [];
});
var result = teamModel.getMembersSortedByRole();
Expand Down
1 change: 0 additions & 1 deletion src/js/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ common.getAllUsers = function() {
};

common.teamCreate = function(action) {
console.log(action)
return $.ajax({
url: resources.routes.team(action),
dataType: 'json',
Expand Down

0 comments on commit b88527c

Please sign in to comment.