Skip to content

Commit

Permalink
Merge pull request #140 from labzero/jeffrey/sort-all-votes
Browse files Browse the repository at this point in the history
Take all_vote_count into account in client-side sorting
  • Loading branch information
nyscherm authored Mar 29, 2018
2 parents df46967 + 31b75ad commit 62e8c77
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/reducerMaps/restaurants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ export default new Map([
const restaurantA = getRestaurantById({ restaurants: state }, a);
const restaurantB = getRestaurantById({ restaurants: state }, b);

// stable sort
if (restaurantA.votes.length !== restaurantB.votes.length) {
return restaurantB.votes.length - restaurantA.votes.length;
}
if (restaurantA.all_vote_count !== restaurantB.all_vote_count) {
return restaurantB.all_vote_count - restaurantA.all_vote_count;
}
// stable sort
return sortIndexes[a] - sortIndexes[b];
});
// If array contents match, return original (for shallow comparison)
Expand Down
39 changes: 30 additions & 9 deletions src/reducerMaps/tests/restaurants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,70 @@ describe('reducerMaps/restaurants', () => {
1: {
id: 1,
name: 'Tokyo Express',
votes: [1],
votes: [1, 2],
all_vote_count: 0,
},
2: {
id: 2,
name: 'Ferry Building',
votes: [2, 3, 4, 5, 6],
all_vote_count: 0,
},
3: {
id: 3,
name: 'Ramen Grill',
votes: [7, 8],
votes: [7],
all_vote_count: 5,
},
4: {
id: 4,
name: 'Burger Bonanza',
votes: [7],
all_vote_count: 10
},
5: {
id: 5,
name: 'Sandwich Area',
votes: [7, 8],
all_vote_count: 0,
},
6: {
id: 6,
name: 'Taco Deli',
votes: [],
all_vote_count: 0,
}
}
},
result: [1,2,3,4]
}
};
result: [1,2,3,4,5,6]
}
};

afterState = restaurants.get(ActionTypes.SORT_RESTAURANTS)(beforeState, {
decision: {restaurant_id: 3},
newlyAdded: {id: 4, userId: 1},
decision: {restaurant_id: 5},
newlyAdded: {id: 6, userId: 1},
user: {id: 1}
});
});

it('places new restaurant at the top', () => {
expect(afterState.items.result[0]).to.eq(4);
expect(afterState.items.result[0]).to.eq(6);
});

it('places restaurant with decison below new restaurants', () => {
expect(afterState.items.result[1]).to.eq(3);
expect(afterState.items.result[1]).to.eq(5);
});

it('places restaurant with more votes above restaurants with fewer votes', () => {
expect(afterState.items.result[2]).to.eq(2);
expect(afterState.items.result[3]).to.eq(1);
});

it('places restaurant with more past votes above restaurants with fewer past votes', () => {
expect(afterState.items.result[4]).to.eq(4);
expect(afterState.items.result[5]).to.eq(3);
});

});

describe('DECISION_POSTED', () => {
Expand Down

0 comments on commit 62e8c77

Please sign in to comment.