diff --git a/src/reducerMaps/restaurants.js b/src/reducerMaps/restaurants.js index 20278c093..75ddc3d7b 100644 --- a/src/reducerMaps/restaurants.js +++ b/src/reducerMaps/restaurants.js @@ -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) diff --git a/src/reducerMaps/tests/restaurants.test.js b/src/reducerMaps/tests/restaurants.test.js index 08fc5ae4a..3508debd7 100644 --- a/src/reducerMaps/tests/restaurants.test.js +++ b/src/reducerMaps/tests/restaurants.test.js @@ -19,42 +19,58 @@ 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', () => { @@ -62,6 +78,11 @@ describe('reducerMaps/restaurants', () => { 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', () => {