From 8ff4cfe709668aeed1a3b0846b84b5b28fc3e5ef Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Wed, 10 Feb 2016 08:22:08 -0600 Subject: [PATCH] tests: Finish start quest --- test/api/v3/unit/models/group.test.js | 76 ++++++++++++++++++++------- website/src/models/group.js | 9 +--- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/test/api/v3/unit/models/group.test.js b/test/api/v3/unit/models/group.test.js index b86cc7a1d9..ef76e7b984 100644 --- a/test/api/v3/unit/models/group.test.js +++ b/test/api/v3/unit/models/group.test.js @@ -1,17 +1,16 @@ +import { sleep } from '../../../../helpers/api-unit.helper'; import { model as Group } from '../../../../../website/src/models/group'; import { model as User } from '../../../../../website/src/models/user'; import { quests as questScrolls } from '../../../../../common/script/content'; import * as email from '../../../../../website/src/libs/api-v3/email'; -import Q from 'q'; -describe.skip('Group Model', () => { +describe('Group Model', () => { context('Instance Methods', () => { describe('#startQuest', () => { let party, questLeader, participatingMember, nonParticipatingMember, undecidedMember; beforeEach(async () => { sandbox.stub(email, 'sendTxn'); - sandbox.spy(Q, 'allSettled'); party = new Group({ name: 'test party', @@ -173,14 +172,6 @@ describe.skip('Group Model', () => { 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 () => { participatingMember.preferences.emailNotifications.questStarted = true; questLeader.preferences.emailNotifications.questStarted = true; @@ -191,6 +182,8 @@ describe.skip('Group Model', () => { await party.startQuest(nonParticipatingMember); + await sleep(0.5); + expect(email.sendTxn).to.be.calledOnce; let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); @@ -212,6 +205,8 @@ describe.skip('Group Model', () => { await party.startQuest(nonParticipatingMember); + await sleep(0.5); + expect(email.sendTxn).to.be.calledOnce; let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); @@ -231,6 +226,8 @@ describe.skip('Group Model', () => { await party.startQuest(participatingMember); + await sleep(0.5); + expect(email.sendTxn).to.be.calledOnce; let memberIds = _.pluck(email.sendTxn.args[0][0], '_id'); @@ -240,21 +237,62 @@ describe.skip('Group Model', () => { 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); - expect(Q.allSettled).to.be.calledOnce; + let members = [questLeader._id, participatingMember._id]; - let savePromises = Q.allSettled.args[0][0]; - expect(savePromises).to.have.a.lengthOf(2); + expect(User.update).to.be.calledWith( + { _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); - expect(Q.allSettled).to.be.calledOnce; - let savePromises = Q.allSettled.args[0][0]; - expect(savePromises).to.have.a.lengthOf(1); + expect(User.update).to.be.calledWith( + { _id: questLeader._id }, + { + $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); }); }); }); diff --git a/website/src/models/group.js b/website/src/models/group.js index 23524646d1..4046b7d5ee 100644 --- a/website/src/models/group.js +++ b/website/src/models/group.js @@ -210,23 +210,16 @@ schema.methods.startQuest = async function startQuest (user) { 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) { user.items.quests[this.quest.key] -= 1; user.markModified('items.quests'); } else { // another user is starting the quest, update the leader separately 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: { [`items.quests.${this.quest.key}`]: -1, }, }).exec(); - removeFromArray(nonUserQuestMembers, this.quest.leader); } // update the remaining users