tests: Finish start quest

This commit is contained in:
Blade Barringer
2016-02-10 08:22:08 -06:00
parent b6ed2f8c44
commit 8ff4cfe709
2 changed files with 58 additions and 27 deletions

View File

@@ -1,17 +1,16 @@
import { sleep } from '../../../../helpers/api-unit.helper';
import { model as Group } from '../../../../../website/src/models/group'; import { model as Group } from '../../../../../website/src/models/group';
import { model as User } from '../../../../../website/src/models/user'; import { model as User } from '../../../../../website/src/models/user';
import { quests as questScrolls } from '../../../../../common/script/content'; import { quests as questScrolls } from '../../../../../common/script/content';
import * as email from '../../../../../website/src/libs/api-v3/email'; import * as email from '../../../../../website/src/libs/api-v3/email';
import Q from 'q';
describe.skip('Group Model', () => { describe('Group Model', () => {
context('Instance Methods', () => { context('Instance Methods', () => {
describe('#startQuest', () => { describe('#startQuest', () => {
let party, questLeader, participatingMember, nonParticipatingMember, undecidedMember; let party, questLeader, participatingMember, nonParticipatingMember, undecidedMember;
beforeEach(async () => { beforeEach(async () => {
sandbox.stub(email, 'sendTxn'); sandbox.stub(email, 'sendTxn');
sandbox.spy(Q, 'allSettled');
party = new Group({ party = new Group({
name: 'test party', name: 'test party',
@@ -173,14 +172,6 @@ describe.skip('Group Model', () => {
expect(undecidedMember.party.quest.key).to.not.eql('whale'); expect(undecidedMember.party.quest.key).to.not.eql('whale');
}); });
it('removes quest scroll from quest leader', async () => {
await party.startQuest(participatingMember);
questLeader = await User.findById(questLeader._id);
expect(questLeader.items.quests.whale).to.eql(0);
});
it('sends email to participating members that quest has started', async () => { it('sends email to participating members that quest has started', async () => {
participatingMember.preferences.emailNotifications.questStarted = true; participatingMember.preferences.emailNotifications.questStarted = true;
questLeader.preferences.emailNotifications.questStarted = true; questLeader.preferences.emailNotifications.questStarted = true;
@@ -191,6 +182,8 @@ describe.skip('Group Model', () => {
await party.startQuest(nonParticipatingMember); await party.startQuest(nonParticipatingMember);
await sleep(0.5);
expect(email.sendTxn).to.be.calledOnce; expect(email.sendTxn).to.be.calledOnce;
let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); let memberIds = _.pluck(email.sendTxn.args[0][0], '_id');
@@ -212,6 +205,8 @@ describe.skip('Group Model', () => {
await party.startQuest(nonParticipatingMember); await party.startQuest(nonParticipatingMember);
await sleep(0.5);
expect(email.sendTxn).to.be.calledOnce; expect(email.sendTxn).to.be.calledOnce;
let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); let memberIds = _.pluck(email.sendTxn.args[0][0], '_id');
@@ -231,6 +226,8 @@ describe.skip('Group Model', () => {
await party.startQuest(participatingMember); await party.startQuest(participatingMember);
await sleep(0.5);
expect(email.sendTxn).to.be.calledOnce; expect(email.sendTxn).to.be.calledOnce;
let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); let memberIds = _.pluck(email.sendTxn.args[0][0], '_id');
@@ -240,21 +237,62 @@ describe.skip('Group Model', () => {
expect(memberIds).to.include(questLeader._id); expect(memberIds).to.include(questLeader._id);
}); });
it('adds participating members to background save operations', async () => { it('updates participting members (not including user)', async () => {
sandbox.spy(User, 'update');
await party.startQuest(nonParticipatingMember); await party.startQuest(nonParticipatingMember);
expect(Q.allSettled).to.be.calledOnce; let members = [questLeader._id, participatingMember._id];
let savePromises = Q.allSettled.args[0][0]; expect(User.update).to.be.calledWith(
expect(savePromises).to.have.a.lengthOf(2); { _id: { $in: members } },
{
$set: {
'party.quest.key': 'whale',
'party.quest.progress.down': 0,
'party.quest.collect': {},
'party.quest.completed': null,
},
}
);
}); });
it('does not include initiating user in background save operations', async () => { it('updates non-user quest leader and decrements quest scroll', async () => {
sandbox.spy(User, 'update');
await party.startQuest(participatingMember); await party.startQuest(participatingMember);
expect(Q.allSettled).to.be.calledOnce; expect(User.update).to.be.calledWith(
let savePromises = Q.allSettled.args[0][0]; { _id: questLeader._id },
expect(savePromises).to.have.a.lengthOf(1); {
$inc: {
'items.quests.whale': -1,
},
}
);
});
it('modifies the participating initiating user directly', async () => {
await party.startQuest(participatingMember);
let userQuest = participatingMember.party.quest;
expect(userQuest.key).to.eql('whale');
expect(userQuest.progress.down).to.eql(0);
expect(userQuest.collect).to.eql({});
expect(userQuest.completed).to.eql(null);
});
it('does not modify user if not participating', async () => {
await party.startQuest(nonParticipatingMember);
expect(nonParticipatingMember.party.quest.key).to.not.eql('whale');
});
it('removes the quest directly if initiating user is the quest leader', async () => {
await party.startQuest(questLeader);
expect(questLeader.items.quests.whale).to.eql(0);
}); });
}); });
}); });

View File

@@ -210,23 +210,16 @@ schema.methods.startQuest = async function startQuest (user) {
user.markModified('party.quest'); user.markModified('party.quest');
} }
// Remove the quest from the quest leader items (if he's the current user) // Remove the quest from the quest leader items (if they are the current user)
if (this.quest.leader === user._id) { if (this.quest.leader === user._id) {
user.items.quests[this.quest.key] -= 1; user.items.quests[this.quest.key] -= 1;
user.markModified('items.quests'); user.markModified('items.quests');
} else { // another user is starting the quest, update the leader separately } else { // another user is starting the quest, update the leader separately
await User.update({_id: this.quest.leader}, { await User.update({_id: this.quest.leader}, {
$set: {
'party.quest.key': this.quest.key,
'party.quest.progress.down': 0,
'party.quest.collect': collected,
'party.quest.completed': null,
},
$inc: { $inc: {
[`items.quests.${this.quest.key}`]: -1, [`items.quests.${this.quest.key}`]: -1,
}, },
}).exec(); }).exec();
removeFromArray(nonUserQuestMembers, this.quest.leader);
} }
// update the remaining users // update the remaining users