mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
correctly stub methods and test errors
This commit is contained in:
@@ -107,7 +107,18 @@ api.ops = {
|
|||||||
sleep,
|
sleep,
|
||||||
allocate,
|
allocate,
|
||||||
};
|
};
|
||||||
api.fns = {};
|
|
||||||
|
import handleTwoHanded from './fns/handleTwoHanded';
|
||||||
|
import predictableRandom from './fns/predictableRandom';
|
||||||
|
import randomVal from './fns/randomVal';
|
||||||
|
import ultimateGear from './fns/ultimateGear';
|
||||||
|
|
||||||
|
api.fns = {
|
||||||
|
handleTwoHanded,
|
||||||
|
predictableRandom,
|
||||||
|
randomVal,
|
||||||
|
ultimateGear,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -17,18 +17,20 @@ describe('shared.ops.allocate', () => {
|
|||||||
|
|
||||||
it('throws an error if an invalid attribute is supplied', () => {
|
it('throws an error if an invalid attribute is supplied', () => {
|
||||||
try {
|
try {
|
||||||
expect(allocate(user, {
|
allocate(user, {
|
||||||
query: {stat: 'notValid'},
|
query: {stat: 'notValid'},
|
||||||
})).to.throw(BadRequest);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(BadRequest);
|
||||||
expect(err.message).to.equal(i18n.t('invalidAttribute', {attr: 'notValid'}));
|
expect(err.message).to.equal(i18n.t('invalidAttribute', {attr: 'notValid'}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws an error if the user doesn\'t have attribute points', () => {
|
it('throws an error if the user doesn\'t have attribute points', () => {
|
||||||
try {
|
try {
|
||||||
expect(allocate(user)).to.throw(NotAuthorized);
|
allocate(user);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('notEnoughAttrPoints'));
|
expect(err.message).to.equal(i18n.t('notEnoughAttrPoints'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,8 +6,7 @@ import {
|
|||||||
} from '../../helpers/common.helper';
|
} from '../../helpers/common.helper';
|
||||||
import count from '../../../common/script/count';
|
import count from '../../../common/script/count';
|
||||||
import buy from '../../../common/script/ops/buy';
|
import buy from '../../../common/script/ops/buy';
|
||||||
import predictableRandom from '../../../common/script/fns/predictableRandom';
|
import shared from '../../../common/script';
|
||||||
import randomVal from '../../../common/script/fns/randomVal';
|
|
||||||
import content from '../../../common/script/content/index';
|
import content from '../../../common/script/content/index';
|
||||||
import {
|
import {
|
||||||
NotAuthorized,
|
NotAuthorized,
|
||||||
@@ -16,10 +15,6 @@ import i18n from '../../../common/script/i18n';
|
|||||||
|
|
||||||
describe('shared.ops.buy', () => {
|
describe('shared.ops.buy', () => {
|
||||||
let user;
|
let user;
|
||||||
let fns = {
|
|
||||||
predictableRandom,
|
|
||||||
randomVal,
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
user = generateUser({
|
user = generateUser({
|
||||||
@@ -36,13 +31,13 @@ describe('shared.ops.buy', () => {
|
|||||||
stats: { gp: 200 },
|
stats: { gp: 200 },
|
||||||
});
|
});
|
||||||
|
|
||||||
sinon.stub(fns, 'randomVal');
|
sinon.stub(shared.fns, 'randomVal');
|
||||||
sinon.stub(fns, 'predictableRandom');
|
sinon.stub(shared.fns, 'predictableRandom');
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
fns.randomVal.restore();
|
shared.fns.randomVal.restore();
|
||||||
fns.predictableRandom.restore();
|
shared.fns.predictableRandom.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Potion', () => {
|
context('Potion', () => {
|
||||||
@@ -69,8 +64,9 @@ describe('shared.ops.buy', () => {
|
|||||||
user.stats.hp = 45;
|
user.stats.hp = 45;
|
||||||
user.stats.gp = 5;
|
user.stats.gp = 5;
|
||||||
try {
|
try {
|
||||||
expect(buy(user, {params: {key: 'potion'}})).to.throw(NotAuthorized);
|
buy(user, {params: {key: 'potion'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,8 +144,9 @@ describe('shared.ops.buy', () => {
|
|||||||
user.stats.gp = 20;
|
user.stats.gp = 20;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
expect(buy(user, {params: {key: 'armor_warrior_1'}})).to.throw(NotAuthorized);
|
buy(user, {params: {key: 'armor_warrior_1'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,12 +178,13 @@ describe('shared.ops.buy', () => {
|
|||||||
|
|
||||||
context('failure conditions', () => {
|
context('failure conditions', () => {
|
||||||
it('does not open if user does not have enough gold', () => {
|
it('does not open if user does not have enough gold', () => {
|
||||||
fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
||||||
user.stats.gp = 50;
|
user.stats.gp = 50;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
expect(buy(user, {params: {key: 'armoire'}})).to.throw(NotAuthorized);
|
buy(user, {params: {key: 'armoire'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
|
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
|
||||||
expect(user.items.food).to.be.empty;
|
expect(user.items.food).to.be.empty;
|
||||||
@@ -195,12 +193,13 @@ describe('shared.ops.buy', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not open without Ultimate Gear achievement', () => {
|
it('does not open without Ultimate Gear achievement', () => {
|
||||||
fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
||||||
user.achievements.ultimateGearSets = {healer: false, wizard: false, rogue: false, warrior: false};
|
user.achievements.ultimateGearSets = {healer: false, wizard: false, rogue: false, warrior: false};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
expect(buy(user, {params: {key: 'armoire'}})).to.throw(NotAuthorized);
|
buy(user, {params: {key: 'armoire'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('cannoyBuyItem'));
|
expect(err.message).to.equal(i18n.t('cannoyBuyItem'));
|
||||||
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
|
expect(user.items.gear.owned).to.eql({weapon_warrior_0: true});
|
||||||
expect(user.items.food).to.be.empty;
|
expect(user.items.food).to.be.empty;
|
||||||
@@ -211,7 +210,7 @@ describe('shared.ops.buy', () => {
|
|||||||
|
|
||||||
context('non-gear awards', () => {
|
context('non-gear awards', () => {
|
||||||
it('gives Experience', () => {
|
it('gives Experience', () => {
|
||||||
fns.predictableRandom.returns(YIELD_EXP);
|
shared.fns.predictableRandom.returns(YIELD_EXP);
|
||||||
|
|
||||||
buy(user, {params: {key: 'armoire'}});
|
buy(user, {params: {key: 'armoire'}});
|
||||||
|
|
||||||
@@ -224,8 +223,8 @@ describe('shared.ops.buy', () => {
|
|||||||
it('gives food', () => {
|
it('gives food', () => {
|
||||||
let honey = content.food.Honey;
|
let honey = content.food.Honey;
|
||||||
|
|
||||||
fns.randomVal.returns(honey);
|
shared.fns.randomVal.returns(honey);
|
||||||
fns.predictableRandom.returns(YIELD_FOOD);
|
shared.fns.predictableRandom.returns(YIELD_FOOD);
|
||||||
|
|
||||||
buy(user, {params: {key: 'armoire'}});
|
buy(user, {params: {key: 'armoire'}});
|
||||||
|
|
||||||
@@ -236,7 +235,7 @@ describe('shared.ops.buy', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('does not give equipment if all equipment has been found', () => {
|
it('does not give equipment if all equipment has been found', () => {
|
||||||
fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
||||||
user.items.gear.owned = fullArmoire;
|
user.items.gear.owned = fullArmoire;
|
||||||
user.stats.gp = 150;
|
user.stats.gp = 150;
|
||||||
|
|
||||||
@@ -256,12 +255,12 @@ describe('shared.ops.buy', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
let shield = content.gear.tree.shield.armoire.gladiatorShield;
|
let shield = content.gear.tree.shield.armoire.gladiatorShield;
|
||||||
|
|
||||||
fns.randomVal.returns(shield);
|
shared.fns.randomVal.returns(shield);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('always drops equipment the first time', () => {
|
it('always drops equipment the first time', () => {
|
||||||
delete user.flags.armoireOpened;
|
delete user.flags.armoireOpened;
|
||||||
fns.predictableRandom.returns(YIELD_EXP);
|
shared.fns.predictableRandom.returns(YIELD_EXP);
|
||||||
|
|
||||||
buy(user, {params: {key: 'armoire'}});
|
buy(user, {params: {key: 'armoire'}});
|
||||||
|
|
||||||
@@ -279,7 +278,7 @@ describe('shared.ops.buy', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('gives more equipment', () => {
|
it('gives more equipment', () => {
|
||||||
fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
shared.fns.predictableRandom.returns(YIELD_EQUIPMENT);
|
||||||
user.items.gear.owned = {
|
user.items.gear.owned = {
|
||||||
weapon_warrior_0: true,
|
weapon_warrior_0: true,
|
||||||
head_armoire_hornedIronHelm: true,
|
head_armoire_hornedIronHelm: true,
|
||||||
|
|||||||
@@ -29,8 +29,9 @@ describe('shared.ops.buyMysterySet', () => {
|
|||||||
context('failure conditions', () => {
|
context('failure conditions', () => {
|
||||||
it('does not grant mystery sets without Mystic Hourglasses', () => {
|
it('does not grant mystery sets without Mystic Hourglasses', () => {
|
||||||
try {
|
try {
|
||||||
expect(buyMysterySet(user, {params: {key: '201501'}})).to.throw(NotAuthorized);
|
buyMysterySet(user, {params: {key: '201501'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.eql(i18n.t('notEnoughHourglasses'));
|
expect(err.message).to.eql(i18n.t('notEnoughHourglasses'));
|
||||||
expect(user.items.gear.owned).to.have.property('weapon_warrior_0', true);
|
expect(user.items.gear.owned).to.have.property('weapon_warrior_0', true);
|
||||||
}
|
}
|
||||||
@@ -47,8 +48,9 @@ describe('shared.ops.buyMysterySet', () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
expect(buyMysterySet(user, {params: {key: '301404'}})).to.throw(NotFound);
|
buyMysterySet(user, {params: {key: '301404'}});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotFound);
|
||||||
expect(err.message).to.eql(i18n.t('mysterySetNotFound'));
|
expect(err.message).to.eql(i18n.t('mysterySetNotFound'));
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,12 +31,13 @@ describe('shared.ops.buyQuest', () => {
|
|||||||
it('does not buy Quests without enough Gold', () => {
|
it('does not buy Quests without enough Gold', () => {
|
||||||
user.stats.gp = 1;
|
user.stats.gp = 1;
|
||||||
try {
|
try {
|
||||||
expect(buyQuest(user, {
|
buyQuest(user, {
|
||||||
params: {
|
params: {
|
||||||
key: 'dilatoryDistress1',
|
key: 'dilatoryDistress1',
|
||||||
},
|
},
|
||||||
})).to.throw(NotAuthorized);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
expect(user.items.quests).to.eql({});
|
expect(user.items.quests).to.eql({});
|
||||||
expect(user.stats.gp).to.equal(1);
|
expect(user.stats.gp).to.equal(1);
|
||||||
@@ -46,12 +47,13 @@ describe('shared.ops.buyQuest', () => {
|
|||||||
it('does not buy nonexistent Quests', () => {
|
it('does not buy nonexistent Quests', () => {
|
||||||
user.stats.gp = 9999;
|
user.stats.gp = 9999;
|
||||||
try {
|
try {
|
||||||
expect(buyQuest(user, {
|
buyQuest(user, {
|
||||||
params: {
|
params: {
|
||||||
key: 'snarfblatter',
|
key: 'snarfblatter',
|
||||||
},
|
},
|
||||||
})).to.throw(NotFound);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotFound);
|
||||||
expect(err.message).to.equal(i18n.t('questNotFound', {key: 'snarfblatter'}));
|
expect(err.message).to.equal(i18n.t('questNotFound', {key: 'snarfblatter'}));
|
||||||
expect(user.items.quests).to.eql({});
|
expect(user.items.quests).to.eql({});
|
||||||
expect(user.stats.gp).to.equal(9999);
|
expect(user.stats.gp).to.equal(9999);
|
||||||
@@ -61,12 +63,13 @@ describe('shared.ops.buyQuest', () => {
|
|||||||
it('does not buy Gem-premium Quests', () => {
|
it('does not buy Gem-premium Quests', () => {
|
||||||
user.stats.gp = 9999;
|
user.stats.gp = 9999;
|
||||||
try {
|
try {
|
||||||
expect(buyQuest(user, {
|
buyQuest(user, {
|
||||||
params: {
|
params: {
|
||||||
key: 'kraken',
|
key: 'kraken',
|
||||||
},
|
},
|
||||||
})).to.throw(NotAuthorized);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('questNotGoldPurchasable', {key: 'kraken'}));
|
expect(err.message).to.equal(i18n.t('questNotGoldPurchasable', {key: 'kraken'}));
|
||||||
expect(user.items.quests).to.eql({});
|
expect(user.items.quests).to.eql({});
|
||||||
expect(user.stats.gp).to.equal(9999);
|
expect(user.stats.gp).to.equal(9999);
|
||||||
|
|||||||
@@ -19,20 +19,22 @@ describe('shared.ops.buySpecialSpell', () => {
|
|||||||
|
|
||||||
it('throws an error if params.key is missing', () => {
|
it('throws an error if params.key is missing', () => {
|
||||||
try {
|
try {
|
||||||
expect(buySpecialSpell(user)).to.throw(BadRequest);
|
buySpecialSpell(user);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(BadRequest);
|
||||||
expect(err.message).to.equal(i18n.t('missingKeyParam'));
|
expect(err.message).to.equal(i18n.t('missingKeyParam'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('throws an error if the spell doesn\'t exists', () => {
|
it('throws an error if the spell doesn\'t exists', () => {
|
||||||
try {
|
try {
|
||||||
expect(buySpecialSpell(user, {
|
buySpecialSpell(user, {
|
||||||
params: {
|
params: {
|
||||||
key: 'notExisting',
|
key: 'notExisting',
|
||||||
},
|
},
|
||||||
})).to.throw(NotFound);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotFound);
|
||||||
expect(err.message).to.equal(i18n.t('spellNotFound', {spellId: 'notExisting'}));
|
expect(err.message).to.equal(i18n.t('spellNotFound', {spellId: 'notExisting'}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -40,12 +42,13 @@ describe('shared.ops.buySpecialSpell', () => {
|
|||||||
it('throws an error if the user doesn\'t have enough gold', () => {
|
it('throws an error if the user doesn\'t have enough gold', () => {
|
||||||
user.stats.gp = 1;
|
user.stats.gp = 1;
|
||||||
try {
|
try {
|
||||||
expect(buySpecialSpell(user, {
|
buySpecialSpell(user, {
|
||||||
params: {
|
params: {
|
||||||
key: 'thankyou',
|
key: 'thankyou',
|
||||||
},
|
},
|
||||||
})).to.throw(NotAuthorized);
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
expect(err).to.be.an.instanceof(NotAuthorized);
|
||||||
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
expect(err.message).to.equal(i18n.t('messageNotEnoughGold'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user