2019-10-03 14:27:44 +00:00
|
|
|
import * as pinnedGearUtils from '../../../../website/common/script/ops/pinnedGearUtils';
|
2018-06-28 10:37:21 +00:00
|
|
|
import {
|
|
|
|
|
NotAuthorized,
|
|
|
|
|
} from '../../../../website/common/script/libs/errors';
|
|
|
|
|
import i18n from '../../../../website/common/script/i18n';
|
|
|
|
|
import {
|
|
|
|
|
generateUser,
|
|
|
|
|
} from '../../../helpers/common.helper';
|
2019-10-08 18:45:38 +00:00
|
|
|
import { BuyQuestWithGemOperation } from '../../../../website/common/script/ops/buy/buyQuestGem';
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
describe('shared.ops.buyQuestGems', () => {
|
|
|
|
|
let user;
|
2019-10-08 18:45:38 +00:00
|
|
|
const goldPoints = 40;
|
|
|
|
|
const analytics = { track () {} };
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
function buyQuest (_user, _req, _analytics) {
|
|
|
|
|
const buyOp = new BuyQuestWithGemOperation(_user, _req, _analytics);
|
|
|
|
|
|
|
|
|
|
return buyOp.purchase();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
before(() => {
|
2019-10-08 18:45:38 +00:00
|
|
|
user = generateUser({ 'stats.class': 'rogue' });
|
2018-06-28 10:37:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
sinon.stub(analytics, 'track');
|
|
|
|
|
sinon.spy(pinnedGearUtils, 'removeItemByPath');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
analytics.track.restore();
|
|
|
|
|
pinnedGearUtils.removeItemByPath.restore();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('successful purchase', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const userGemAmount = 10;
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
before(() => {
|
|
|
|
|
user.balance = userGemAmount;
|
|
|
|
|
user.stats.gp = goldPoints;
|
|
|
|
|
user.purchased.plan.gemsBought = 0;
|
|
|
|
|
user.purchased.plan.customerId = 'customer-id';
|
2019-10-08 18:45:38 +00:00
|
|
|
user.pinnedItems.push({ type: 'quests', key: 'gryphon' });
|
2018-06-28 10:37:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('purchases quests', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const key = 'gryphon';
|
2018-06-28 10:37:21 +00:00
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
buyQuest(user, { params: { key } });
|
2019-10-06 15:06:02 +00:00
|
|
|
|
|
|
|
|
expect(user.items.quests[key]).to.equal(1);
|
|
|
|
|
expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true);
|
|
|
|
|
});
|
|
|
|
|
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.', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const key = 'dustbunnies';
|
2019-10-06 15:06:02 +00:00
|
|
|
user.items.quests[key] = -1;
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
buyQuest(user, { params: { key } });
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
expect(user.items.quests[key]).to.equal(1);
|
|
|
|
|
expect(pinnedGearUtils.removeItemByPath.notCalled).to.equal(true);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('bulk purchase', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const userGemAmount = 10;
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
user.balance = userGemAmount;
|
|
|
|
|
user.stats.gp = goldPoints;
|
|
|
|
|
user.purchased.plan.gemsBought = 0;
|
|
|
|
|
user.purchased.plan.customerId = 'customer-id';
|
|
|
|
|
});
|
|
|
|
|
|
2019-10-08 18:45:38 +00:00
|
|
|
it('errors when user does not have enough gems', done => {
|
2018-06-28 10:37:21 +00:00
|
|
|
user.balance = 1;
|
2019-10-08 18:45:38 +00:00
|
|
|
const key = 'gryphon';
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
buyQuest(user, {
|
2019-10-08 18:45:38 +00:00
|
|
|
params: { key },
|
2018-06-28 10:37:21 +00:00
|
|
|
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', () => {
|
2019-10-08 18:45:38 +00:00
|
|
|
const key = 'gryphon';
|
2018-06-28 10:37:21 +00:00
|
|
|
|
|
|
|
|
buyQuest(user, {
|
2019-10-08 18:45:38 +00:00
|
|
|
params: { key },
|
2018-06-28 10:37:21 +00:00
|
|
|
quantity: 3,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(user.items.quests[key]).to.equal(4);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|