port buyQuest tests

This commit is contained in:
Matteo Pagliazzi 2016-03-20 13:33:34 +01:00
parent ad8834f4ed
commit 2560096466
4 changed files with 77 additions and 58 deletions

View file

@ -11,7 +11,7 @@ module.exports = function buyQuest (user, req = {}, analytics) {
if (!key) throw new BadRequest(i18n.t('missingKeyParam', req.language));
let item = content.quests[key];
if (!item) throw new NotFound(i18n.t('questNotFound', req.language));
if (!item) throw new NotFound(i18n.t('questNotFound', {key}, req.language));
if (!(item.category === 'gold' && item.goldValue)) {
throw new NotAuthorized(i18n.t('questNotGoldPurchasable', {key}, req.language));

View file

@ -1,6 +1,5 @@
/* eslint-disable camelcase */
import sinon from 'sinon'; // eslint-disable-line no-shadow
import {
generateUser,
} from '../../helpers/common.helper';

View file

@ -0,0 +1,76 @@
import {
generateUser,
} from '../../helpers/common.helper';
import buyQuest from '../../../common/script/ops/buyQuest';
import {
NotAuthorized,
NotFound,
} from '../../../common/script/libs/errors';
import i18n from '../../../common/script/i18n';
import testHelper from '../test_helper';
describe.only('shared.ops.buyQuest', () => {
let user;
beforeEach(() => {
user = generateUser();
});
it('buys a Quest scroll', () => {
user.stats.gp = 205;
buyQuest(user, {
params: {
key: 'dilatoryDistress1',
},
});
expect(user.items.quests).to.eql({
dilatoryDistress1: 1,
});
expect(user.stats.gp).to.equal(5);
});
it('does not buy Quests without enough Gold', () => {
user.stats.gp = 1;
try {
expect(buyQuest(user, {
params: {
key: 'dilatoryDistress1',
},
})).to.throw(NotAuthorized);
} catch (err) {
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(1);
}
});
it('does not buy nonexistent Quests', () => {
user.stats.gp = 9999;
try {
expect(buyQuest(user, {
params: {
key: 'snarfblatter',
},
})).to.throw(NotFound);
} catch (err) {
expect(err.message).to.equal(i18n.t('questNotFound', {key: 'snarfblatter'}));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(9999);
}
});
it('does not buy Gem-premium Quests', () => {
user.stats.gp = 9999;
try {
expect(buyQuest(user, {
params: {
key: 'kraken',
},
})).to.throw(NotAuthorized);
} catch (err) {
expect(err.message).to.equal(i18n.t('questNotGoldPurchasable', {key: 'kraken'}));
expect(user.items.quests).to.eql({});
expect(user.stats.gp).to.equal(9999);
}
});
});

View file

@ -472,62 +472,6 @@ describe('User', () => {
});
});
describe('store', () => {
it('buys a Quest scroll', () => {
let user = generateUser();
user.stats.gp = 205;
user.ops.buyQuest({
params: {
key: 'dilatoryDistress1',
},
});
expect(user.items.quests).to.eql({
dilatoryDistress1: 1,
});
expect(user).toHaveGP(5);
});
it('does not buy Quests without enough Gold', () => {
let user = generateUser();
user.stats.gp = 1;
user.ops.buyQuest({
params: {
key: 'dilatoryDistress1',
},
});
expect(user.items.quests).to.eql({});
expect(user).toHaveGP(1);
});
it('does not buy nonexistent Quests', () => {
let user = generateUser();
user.stats.gp = 9999;
user.ops.buyQuest({
params: {
key: 'snarfblatter',
},
});
expect(user.items.quests).to.eql({});
expect(user).toHaveGP(9999);
});
it('does not buy Gem-premium Quests', () => {
let user = generateUser();
user.stats.gp = 9999;
user.ops.buyQuest({
params: {
key: 'kraken',
},
});
expect(user.items.quests).to.eql({});
expect(user).toHaveGP(9999);
});
});
describe('Gem purchases', () => {
it('does not purchase items without enough Gems', () => {
let user = generateUser();