Fix challenge sorting

Closes #7543
Closes #7507
This commit is contained in:
Shervin Sarain
2016-05-30 19:04:13 +02:00
committed by Blade Barringer
parent a11a999839
commit de21487038
3 changed files with 244 additions and 110 deletions

View File

@@ -64,6 +64,20 @@ describe('GET challenges/group/:groupId', () => {
profile: {name: user.profile.name},
});
});
it('should return newest challenges first', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(0);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
});
context('Private Guild', () => {
@@ -115,4 +129,56 @@ describe('GET challenges/group/:groupId', () => {
});
});
});
context('official challenge is present', () => {
let publicGuild, user, officialChallenge, challenge, challenge2;
before(async () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestGuild',
type: 'guild',
privacy: 'public',
},
});
user = groupLeader;
publicGuild = group;
await user.update({
'contributor.admin': true,
});
officialChallenge = await generateChallenge(user, group, {
official: true,
});
challenge = await generateChallenge(user, group);
challenge2 = await generateChallenge(user, group);
});
it('should return official challenges first', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should return newest challenges first, after official ones', async () => {
let challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge._id });
expect(foundChallengeIndex).to.eql(2);
foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(1);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get(`/challenges/groups/${publicGuild._id}`);
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(1);
});
});
});

View File

@@ -5,6 +5,7 @@ import {
} from '../../../../helpers/api-v3-integration.helper';
describe('GET challenges/user', () => {
context('no official challenges', () => {
let user, member, nonMember, challenge, challenge2, publicGuild;
before(async () => {
@@ -113,6 +114,20 @@ describe('GET challenges/user', () => {
});
});
it('should return newest challenges first', async () => {
let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(0);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get('/challenges/user');
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should not return challenges user doesn\'t have access to', async () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
@@ -129,4 +144,57 @@ describe('GET challenges/user', () => {
let foundChallenge = _.find(challenges, { _id: privateChallenge._id });
expect(foundChallenge).to.not.exist;
});
});
context('official challenge is present', () => {
let user, officialChallenge, challenge, challenge2, publicGuild;
before(async () => {
let { group, groupLeader } = await createAndPopulateGroup({
groupDetails: {
name: 'TestGuild',
type: 'guild',
privacy: 'public',
},
});
user = groupLeader;
publicGuild = group;
await user.update({
'contributor.admin': true,
});
officialChallenge = await generateChallenge(user, group, {
official: true,
});
challenge = await generateChallenge(user, group);
challenge2 = await generateChallenge(user, group);
});
it('should return official challenges first', async () => {
let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: officialChallenge._id });
expect(foundChallengeIndex).to.eql(0);
});
it('should return newest challenges first, after official ones', async () => {
let challenges = await user.get('/challenges/user');
let foundChallengeIndex = _.findIndex(challenges, { _id: challenge._id });
expect(foundChallengeIndex).to.eql(2);
foundChallengeIndex = _.findIndex(challenges, { _id: challenge2._id });
expect(foundChallengeIndex).to.eql(1);
let newChallenge = await generateChallenge(user, publicGuild);
challenges = await user.get('/challenges/user');
foundChallengeIndex = _.findIndex(challenges, { _id: newChallenge._id });
expect(foundChallengeIndex).to.eql(1);
});
});
});

View File

@@ -201,7 +201,7 @@ api.leaveChallenge = {
* @apiName GetUserChallenges
* @apiGroup Challenge
*
* @apiSuccess {Array} data An array of challenges
* @apiSuccess {Array} data An array of challenges sorted with official challenges first, followed by the challenges in order from newest to oldest
*/
api.getUserChallenges = {
method: 'GET',
@@ -218,7 +218,7 @@ api.getUserChallenges = {
],
_id: {$ne: '95533e05-1ff9-4e46-970b-d77219f199e9'}, // remove the Spread the Word Challenge for now, will revisit when we fix the closing-challenge bug TODO revisit
})
.sort('-official -timestamp')
.sort('-official -createdAt')
// see below why we're not using populate
// .populate('group', basicGroupFields)
// .populate('leader', nameFields)
@@ -249,7 +249,7 @@ api.getUserChallenges = {
*
* @apiParam {groupId} groupId The group _id
*
* @apiSuccess {Array} data An array of challenges
* @apiSuccess {Array} data An array of challenges sorted with official challenges first, followed by the challenges in order from newest to oldest
*/
api.getGroupChallenges = {
method: 'GET',
@@ -268,7 +268,7 @@ api.getGroupChallenges = {
if (!group) throw new NotFound(res.t('groupNotFound'));
let challenges = await Challenge.find({group: groupId})
.sort('-official -timestamp')
.sort('-official -createdAt')
// .populate('leader', nameFields) // Only populate the leader as the group is implicit
.exec();