mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-05-22 13:48:46 +00:00
* Don't unpin non-gear items Assumes that multiple of bundles, quests, eggs, potions can be bought * Added tests * Changed type checking and made variables global * Lint fix
This commit is contained in:
parent
c12ae9ea25
commit
5449652bd2
2 changed files with 22 additions and 4 deletions
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue