diff --git a/test/common/ops/buy/purchase.js b/test/common/ops/buy/purchase.js index 609d6d70c4..f8a6c903a0 100644 --- a/test/common/ops/buy/purchase.js +++ b/test/common/ops/buy/purchase.js @@ -1,4 +1,5 @@ import purchase from '../../../../website/common/script/ops/buy/purchase'; +import pinnedGearUtils from '../../../../website/common/script/ops/pinnedGearUtils'; import planGemLimits from '../../../../website/common/script/libs/planGemLimits'; import { BadRequest, @@ -25,10 +26,12 @@ describe('shared.ops.purchase', () => { beforeEach(() => { sinon.stub(analytics, 'track'); + sinon.spy(pinnedGearUtils, 'removeItemByPath'); }); afterEach(() => { analytics.track.restore(); + pinnedGearUtils.removeItemByPath.restore(); }); context('failure conditions', () => { @@ -174,6 +177,12 @@ describe('shared.ops.purchase', () => { user.stats.gp = goldPoints; user.purchased.plan.gemsBought = 0; user.purchased.plan.customerId = 'customer-id'; + user.pinnedItems.push({type: 'eggs', key: 'Wolf'}); + user.pinnedItems.push({type: 'hatchingPotions', key: 'Base'}); + user.pinnedItems.push({type: 'food', key: SEASONAL_FOOD}); + user.pinnedItems.push({type: 'quests', key: 'gryphon'}); + user.pinnedItems.push({type: 'gear', key: 'headAccessory_special_tigerEars'}); + user.pinnedItems.push({type: 'bundles', key: 'featheredFriends'}); }); it('purchases gems', () => { @@ -202,6 +211,7 @@ describe('shared.ops.purchase', () => { purchase(user, {params: {type, key}}, analytics); expect(user.items[type][key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); expect(analytics.track).to.be.calledOnce; }); @@ -212,6 +222,7 @@ describe('shared.ops.purchase', () => { purchase(user, {params: {type, key}}); expect(user.items[type][key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); }); it('purchases food', () => { @@ -221,6 +232,7 @@ describe('shared.ops.purchase', () => { purchase(user, {params: {type, key}}); expect(user.items[type][key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); }); it('purchases quests', () => { @@ -230,6 +242,7 @@ describe('shared.ops.purchase', () => { purchase(user, {params: {type, key}}); expect(user.items[type][key]).to.equal(1); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); }); it('purchases gear', () => { @@ -239,6 +252,7 @@ describe('shared.ops.purchase', () => { purchase(user, {params: {type, key}}); expect(user.items.gear.owned[key]).to.be.true; + expect(pinnedGearUtils.removeItemByPath.calledOnce).to.equal(true); }); it('purchases quest bundles', () => { @@ -261,6 +275,7 @@ describe('shared.ops.purchase', () => { expect(user.balance).to.equal(startingBalance - price); + expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true); clock.restore(); }); }); diff --git a/website/common/script/ops/buy/purchase.js b/website/common/script/ops/buy/purchase.js index 2fa5218c67..22b0803ef5 100644 --- a/website/common/script/ops/buy/purchase.js +++ b/website/common/script/ops/buy/purchase.js @@ -106,6 +106,8 @@ function purchaseItem (user, item, price, type, key) { } } +const acceptedTypes = ['eggs', 'hatchingPotions', 'food', 'quests', 'gear', 'bundles']; +const singlePurchaseTypes = ['gear']; module.exports = function purchase (user, req = {}, analytics) { let type = get(req.params, 'type'); let key = get(req.params, 'key'); @@ -129,8 +131,7 @@ module.exports = function purchase (user, req = {}, analytics) { return gemResponse; } - let acceptedTypes = ['eggs', 'hatchingPotions', 'food', 'quests', 'gear', 'bundles']; - if (acceptedTypes.indexOf(type) === -1) { + if (!acceptedTypes.includes(type)) { throw new NotFound(i18n.t('notAccteptedType', req.language)); } @@ -144,8 +145,10 @@ module.exports = function purchase (user, req = {}, analytics) { throw new NotAuthorized(i18n.t('notEnoughGems', req.language)); } - let itemInfo = getItemInfo(user, type, item); - removeItemByPath(user, itemInfo.path); + if (singlePurchaseTypes.includes(type)) { + let itemInfo = getItemInfo(user, type, item); + removeItemByPath(user, itemInfo.path); + } for (let i = 0; i < quantity; i += 1) { purchaseItem(user, item, price, type, key);