mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-27 13:40:00 +00:00
95 lines
2.3 KiB
JavaScript
95 lines
2.3 KiB
JavaScript
|
|
import pinnedGearUtils from '../../../../website/common/script/ops/pinnedGearUtils';
|
||
|
|
import {
|
||
|
|
NotAuthorized,
|
||
|
|
} from '../../../../website/common/script/libs/errors';
|
||
|
|
import i18n from '../../../../website/common/script/i18n';
|
||
|
|
import {
|
||
|
|
generateUser,
|
||
|
|
} from '../../../helpers/common.helper';
|
||
|
|
import {BuyQuestWithGemOperation} from '../../../../website/common/script/ops/buy/buyQuestGem';
|
||
|
|
|
||
|
|
describe('shared.ops.buyQuestGems', () => {
|
||
|
|
let user;
|
||
|
|
let goldPoints = 40;
|
||
|
|
let analytics = {track () {}};
|
||
|
|
|
||
|
|
function buyQuest (_user, _req, _analytics) {
|
||
|
|
const buyOp = new BuyQuestWithGemOperation(_user, _req, _analytics);
|
||
|
|
|
||
|
|
return buyOp.purchase();
|
||
|
|
}
|
||
|
|
|
||
|
|
before(() => {
|
||
|
|
user = generateUser({'stats.class': 'rogue'});
|
||
|
|
});
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
sinon.stub(analytics, 'track');
|
||
|
|
sinon.spy(pinnedGearUtils, 'removeItemByPath');
|
||
|
|
});
|
||
|
|
|
||
|
|
afterEach(() => {
|
||
|
|
analytics.track.restore();
|
||
|
|
pinnedGearUtils.removeItemByPath.restore();
|
||
|
|
});
|
||
|
|
|
||
|
|
context('successful purchase', () => {
|
||
|
|
let userGemAmount = 10;
|
||
|
|
|
||
|
|
before(() => {
|
||
|
|
user.balance = userGemAmount;
|
||
|
|
user.stats.gp = goldPoints;
|
||
|
|
user.purchased.plan.gemsBought = 0;
|
||
|
|
user.purchased.plan.customerId = 'customer-id';
|
||
|
|
user.pinnedItems.push({type: 'quests', key: 'gryphon'});
|
||
|
|
});
|
||
|
|
|
||
|
|
it('purchases quests', () => {
|
||
|
|
let key = 'gryphon';
|
||
|
|
|
||
|
|
buyQuest(user, {params: {key}});
|
||
|
|
|
||
|
|
expect(user.items.quests[key]).to.equal(1);
|
||
|
|
expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
context('bulk purchase', () => {
|
||
|
|
let userGemAmount = 10;
|
||
|
|
|
||
|
|
beforeEach(() => {
|
||
|
|
user.balance = userGemAmount;
|
||
|
|
user.stats.gp = goldPoints;
|
||
|
|
user.purchased.plan.gemsBought = 0;
|
||
|
|
user.purchased.plan.customerId = 'customer-id';
|
||
|
|
});
|
||
|
|
|
||
|
|
it('errors when user does not have enough gems', (done) => {
|
||
|
|
user.balance = 1;
|
||
|
|
let key = 'gryphon';
|
||
|
|
|
||
|
|
try {
|
||
|
|
buyQuest(user, {
|
||
|
|
params: {key},
|
||
|
|
quantity: 2,
|
||
|
|
});
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||
|
|
expect(err.message).to.equal(i18n.t('notEnoughGems'));
|
||
|
|
done();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
it('makes bulk purchases of quests', () => {
|
||
|
|
let key = 'gryphon';
|
||
|
|
|
||
|
|
buyQuest(user, {
|
||
|
|
params: {key},
|
||
|
|
quantity: 3,
|
||
|
|
});
|
||
|
|
|
||
|
|
expect(user.items.quests[key]).to.equal(4);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|