Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take all_vote_count into account in client-side sorting #140

Merged
merged 1 commit into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small detail, missing a comma here

},
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