diff --git a/test/common/ops/buy/buyQuestGems.js b/test/common/ops/buy/buyQuestGems.test.js similarity index 68% rename from test/common/ops/buy/buyQuestGems.js rename to test/common/ops/buy/buyQuestGems.test.js index 94d1bc5697..aa5080a526 100644 --- a/test/common/ops/buy/buyQuestGems.js +++ b/test/common/ops/buy/buyQuestGems.test.js @@ -47,7 +47,7 @@ describe('shared.ops.buyQuestGems', () => { user.pinnedItems.push({ type: 'quests', key: 'gryphon' }); }); - it('successfully purchases quest', async () => { + it('successfully purchases pet quest', async () => { const key = 'gryphon'; await buyQuest(user, { params: { key } }); @@ -55,6 +55,61 @@ describe('shared.ops.buyQuestGems', () => { expect(user.items.quests[key]).to.equal(1); expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); }); + + it('successfully purchases hatching potion quest', async () => { + const key = 'silver'; + + await buyQuest(user, { params: { key } }); + + expect(user.items.quests[key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); + }); + + it('successfully purchases seasonal quest', async () => { + const key = 'evilsanta'; + + await buyQuest(user, { params: { key } }); + + expect(user.items.quests[key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); + }); + + it('errors if the pet quest is not available', async () => { + const key = 'sabretooth'; + + try { + await buyQuest(user, { params: { key } }); + } catch (err) { + expect(err).to.be.an.instanceof(NotAuthorized); + expect(err.message).to.equal(i18n.t('notAvailable')); + expect(user.items.quests[key]).to.eql(undefined); + } + }); + + it('errors if the hatching potion quest is not available', async () => { + const key = 'ruby'; + + try { + await buyQuest(user, { params: { key } }); + } catch (err) { + expect(err).to.be.an.instanceof(NotAuthorized); + expect(err.message).to.equal(i18n.t('notAvailable')); + expect(user.items.quests[key]).to.eql(undefined); + } + }); + + it('errors if the seasonal quest is not available', async () => { + const key = 'egg'; + + try { + await buyQuest(user, { params: { key } }); + } catch (err) { + expect(err).to.be.an.instanceof(NotAuthorized); + expect(err.message).to.equal(i18n.t('notAvailable')); + expect(user.items.quests[key]).to.eql(undefined); + } + }); + it('if a user\'s count of a quest scroll is negative, it will be reset to 0 before incrementing when they buy a new one.', async () => { const key = 'dustbunnies'; user.items.quests[key] = -1; @@ -64,6 +119,7 @@ describe('shared.ops.buyQuestGems', () => { expect(user.items.quests[key]).to.equal(1); expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); }); + it('errors if the user has not completed prerequisite quests', async () => { const key = 'atom3'; user.achievements.quests.atom1 = 1; @@ -76,6 +132,7 @@ describe('shared.ops.buyQuestGems', () => { expect(user.items.quests[key]).to.eql(undefined); } }); + it('successfully purchases quest if user has completed all prerequisite quests', async () => { const key = 'atom3'; user.achievements.quests.atom1 = 1; diff --git a/website/common/script/ops/buy/buyQuestGem.js b/website/common/script/ops/buy/buyQuestGem.js index c35ae8f4b8..7b3399b1cb 100644 --- a/website/common/script/ops/buy/buyQuestGem.js +++ b/website/common/script/ops/buy/buyQuestGem.js @@ -52,8 +52,25 @@ export class BuyQuestWithGemOperation extends AbstractGemItemOperation { // esli } } - const matchers = getScheduleMatchingGroup(`${item.category}Quests`); - if (!matchers.match(item.key)) { + let matchers = []; + if (item.category === 'hatchingPotion') { + matchers = [ + getScheduleMatchingGroup('hatchingPotionQuests'), + ]; + } else if (item.category === 'pet') { + matchers = [ + getScheduleMatchingGroup('seasonalQuests'), + getScheduleMatchingGroup('petQuests'), + ]; + } + let isAvailable = matchers.length === 0; + matchers.forEach(matcher => { + if (matcher.match(item.key)) { + isAvailable = true; + } + }); + + if (!isAvailable) { throw new NotAuthorized(this.i18n('notAvailable', { key: item.key })); }