diff --git a/common/script/index.js b/common/script/index.js index e23fe47ab9..1dcc751163 100644 --- a/common/script/index.js +++ b/common/script/index.js @@ -2272,13 +2272,12 @@ api.wrap = function(user, main) { })()]++; }, updateStats: function(stats, req, analytics) { - var tnl; if (stats.hp <= 0) { return user.stats.hp = 0; } user.stats.hp = stats.hp; user.stats.gp = stats.gp >= 0 ? stats.gp : 0; - tnl = api.tnl(user.stats.lvl); + var tnl = api.tnl(user.stats.lvl); if (stats.exp >= tnl) { user.stats.exp = stats.exp; while (stats.exp >= tnl) { diff --git a/test/common/user.fns.updateStats.test.js b/test/common/user.fns.updateStats.test.js index 633078714b..ebf836a8e2 100644 --- a/test/common/user.fns.updateStats.test.js +++ b/test/common/user.fns.updateStats.test.js @@ -6,40 +6,66 @@ describe('user.fns.updateStats', () => { let user; beforeEach(() => { - user = generateUser({ - stats: { - int: 20, - str: 20, - con: 20, - per: 20, - lvl: 20, - }, + user = generateUser({}); + }); + + context('no hp', () => { + it('returns 0 if user\'s hp is 0', () => { + let stats = { + hp: 0, + }; + + expect(user.fns.updateStats(stats)).to.eql(0); + }); + + it('returns 0 if user\'s hp is less than 0', () => { + let stats = { + hp: -5, + }; + + expect(user.fns.updateStats(stats)).to.eql(0); + }); + + it('sets user\'s hp to 0 if it is less than 0', () => { + let stats = { + hp: -5, + }; + + user.fns.updateStats(stats) + + expect(user.stats.hp).to.eql(0); }); }); context('Stat Allocation', () => { - it('adds an attibute point when user\'s stat points are less than max level', () => { - user.stats.exp = 3581; + it('Adds an attibute point when user\'s stat points are less than max level', () => { + let stats = { + exp: 3581, + }; + user.stats.lvl = 99; user.stats.str = 25; user.stats.int = 25; user.stats.con = 25; user.stats.per = 24; - user.fns.updateStats(user.stats); + user.fns.updateStats(stats); expect(user.stats.points).to.eql(1); }); - it('does not add an attibute point when user\'s stat points are equal to max level', () => { - user.stats.exp = 3581; + it('Does not add an attibute point when user\'s stat points are equal to max level', () => { + let stats = { + exp: 3581, + }; + user.stats.lvl = 99; user.stats.str = 25; user.stats.int = 25; user.stats.con = 25; user.stats.per = 25; - user.fns.updateStats(user.stats); + user.fns.updateStats(stats); expect(user.stats.points).to.eql(0); });