mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Create new function to check for enough gems, added @crookedneighbor 's tests, and added tests to check for enoughGems when user creates a habitrpg challenge
This commit is contained in:
committed by
Blade Barringer
parent
53c21b07a3
commit
ef9dfac087
192
test/spec/challengeCtrlSpec.js
Normal file
192
test/spec/challengeCtrlSpec.js
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
describe('Challenges Controller', function() {
|
||||||
|
var scope, ctrl, user, Challenges, $rootScope;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
module(function($provide) {
|
||||||
|
$provide.value('User', {});
|
||||||
|
});
|
||||||
|
|
||||||
|
inject(function($rootScope, $controller, _Challenges_){
|
||||||
|
user = specHelper.newUser();
|
||||||
|
user._id = "unique-user-id";
|
||||||
|
|
||||||
|
scope = $rootScope.$new();
|
||||||
|
|
||||||
|
// Load RootCtrl to ensure shared behaviors are loaded
|
||||||
|
$controller('RootCtrl', {$scope: scope, User: {user: user}});
|
||||||
|
|
||||||
|
ctrl = $controller('ChallengesCtrl', {$scope: scope, User: {user: user}});
|
||||||
|
|
||||||
|
Challenges = _Challenges_;
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("enough gems tracking", function() {
|
||||||
|
|
||||||
|
it("set enoughGems to true when user selects habitrpg with enough gems", function() {
|
||||||
|
scope.newChallenge = new Challenges.Challenge({
|
||||||
|
name: 'Challenge without a group',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false
|
||||||
|
});
|
||||||
|
user.balance = 1;
|
||||||
|
scope.newChallenge.group = 'habitrpg';
|
||||||
|
//expect(scope.enoughGems).to.equal(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("save challenge", function() {
|
||||||
|
var alert;
|
||||||
|
|
||||||
|
before(function(){
|
||||||
|
alert = sinon.stub(window, "alert");
|
||||||
|
});
|
||||||
|
|
||||||
|
after(function(){
|
||||||
|
window.alert.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("opens an alert box if challenge.group is habitrpg and user does not have enough gems", function() {
|
||||||
|
scope.create();
|
||||||
|
scope.newChallenge.group = "habitrpg";
|
||||||
|
scope.save(scope.newChallenge);
|
||||||
|
alert.should.have.been.calledWith(window.env.t('challengeNotEnoughGems'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saves the challenge if challenge.group is habitrpg and user has enough gems", function() {
|
||||||
|
var saveWasCalled = false;
|
||||||
|
|
||||||
|
var challenge = new Challenges.Challenge({
|
||||||
|
name: 'Challenge without enough gems',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
group: "habitrpg",
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false,
|
||||||
|
// Mock $save
|
||||||
|
$save: function() {
|
||||||
|
saveWasCalled = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
user.balance = 1;
|
||||||
|
scope.newChallenge = challenge;
|
||||||
|
scope.save(challenge);
|
||||||
|
|
||||||
|
// Not the best test, but :shrug:
|
||||||
|
expect(saveWasCalled).to.be.ok;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("opens an alert box if challenge.group is not specified", function() {
|
||||||
|
var challenge = new Challenges.Challenge({
|
||||||
|
name: 'Challenge without a group',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.save(challenge);
|
||||||
|
|
||||||
|
alert.should.have.been.calledWith(window.env.t('selectGroup'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("opens an alert box if isNew and user does not have enough gems", function() {
|
||||||
|
var challenge = new Challenges.Challenge({
|
||||||
|
name: 'Challenge without enough gems',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
group: "a-group-id",
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.enoughGems = false;
|
||||||
|
scope.save(challenge);
|
||||||
|
|
||||||
|
alert.should.have.been.calledWith(window.env.t('challengeNotEnoughGems'));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saves the challenge if user does not have enough gems, but the challenge is not new", function() {
|
||||||
|
|
||||||
|
var challenge = new Challenges.Challenge({
|
||||||
|
_id: 'challeng-id',
|
||||||
|
name: 'Challenge without enough gems',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
group: "a-group-id",
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false,
|
||||||
|
// Mock $save
|
||||||
|
$save: function(cb) {
|
||||||
|
cb();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.enoughGems = false;
|
||||||
|
scope.save(challenge);
|
||||||
|
|
||||||
|
expect(challenge._locked).to.be.ok;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("saves the challenge if user has enough gems and challenge is new", function() {
|
||||||
|
var saveWasCalled = false;
|
||||||
|
|
||||||
|
var challenge = new Challenges.Challenge({
|
||||||
|
name: 'Challenge without enough gems',
|
||||||
|
description: '',
|
||||||
|
habits: [],
|
||||||
|
dailys: [],
|
||||||
|
todos: [],
|
||||||
|
rewards: [],
|
||||||
|
leader: user._id,
|
||||||
|
group: "a-group-id",
|
||||||
|
timestamp: +(new Date),
|
||||||
|
members: [],
|
||||||
|
official: false,
|
||||||
|
// Mock $save
|
||||||
|
$save: function() {
|
||||||
|
saveWasCalled = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scope.enoughGems = true;
|
||||||
|
scope.save(challenge);
|
||||||
|
|
||||||
|
// Not the best test, but :shrug:
|
||||||
|
expect(saveWasCalled).to.be.ok;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -133,6 +133,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||||||
$scope.save = function(challenge) {
|
$scope.save = function(challenge) {
|
||||||
if (!challenge.group) return alert(window.env.t('selectGroup'));
|
if (!challenge.group) return alert(window.env.t('selectGroup'));
|
||||||
var isNew = !challenge._id;
|
var isNew = !challenge._id;
|
||||||
|
$scope.hasEnoughGems(challenge.group);
|
||||||
if (!$scope.enoughGems && isNew ) return alert(window.env.t('challengeNotEnoughGems'));
|
if (!$scope.enoughGems && isNew ) return alert(window.env.t('challengeNotEnoughGems'));
|
||||||
challenge.$save(function(_challenge){
|
challenge.$save(function(_challenge){
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
@@ -304,17 +305,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||||||
}
|
}
|
||||||
|
|
||||||
$scope.$watch('newChallenge.group', function(gid){
|
$scope.$watch('newChallenge.group', function(gid){
|
||||||
if (!gid) return;
|
$scope.hasEnoughGems(gid);
|
||||||
var group = _.find($scope.groups, {_id:gid});
|
|
||||||
$scope.maxPrize = User.user.balance*4 + ((group && group.balance && group.leader==User.user._id) ? group.balance*4 : 0);
|
|
||||||
if (gid == 'habitrpg') {
|
|
||||||
$scope.newChallenge.prize = 1;
|
|
||||||
//If the usere does not have enough gems for the Habitrpg group, the set our enoughGems var to false
|
|
||||||
if ( $scope.maxPrize <= 0 ) $scope.enoughGems = false;
|
|
||||||
} else {
|
|
||||||
//Reset our enoughGems variable incase the user tried to create a challenge for habitrpg and was unable to
|
|
||||||
$scope.enoughGems = true;
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
$scope.selectAll = function(){
|
$scope.selectAll = function(){
|
||||||
@@ -325,6 +316,28 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||||||
$scope.search.group = _.transform($scope.groups, function(m,g){m[g._id] = false});
|
$scope.search.group = _.transform($scope.groups, function(m,g){m[g._id] = false});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$scope.shouldShow = function(task, list, prefs){
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
//A helper function to dermine if the user has enough gems to create a Habitrpg challenge
|
||||||
|
$scope.hasEnoughGems = function(gid) {
|
||||||
|
if (!gid) return;
|
||||||
|
var groupBalance = 0;
|
||||||
|
var group = _.find($scope.groups, {_id:gid});
|
||||||
|
if (group) { groupBalance = group.balance; }
|
||||||
|
var userBalance = User.user.balance || 0;
|
||||||
|
$scope.maxPrize = userBalance * 4 + ((group && groupBalance && group.leader==User.user._id) ? groupBalance*4 : 0);
|
||||||
|
if (gid == 'habitrpg') {
|
||||||
|
$scope.newChallenge.prize = 1;
|
||||||
|
//If the user does not have enough gems for the Habitrpg group, the set our enoughGems var to false
|
||||||
|
if ( $scope.maxPrize <= 0 ) $scope.enoughGems = false;
|
||||||
|
} else {
|
||||||
|
//Reset our enoughGems variable incase the user tried to create a challenge for habitrpg and was unable to
|
||||||
|
$scope.enoughGems = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$scope.shouldShow = function(task, list, prefs){
|
$scope.shouldShow = function(task, list, prefs){
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -340,4 +353,5 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User',
|
|||||||
|
|
||||||
return groupSelected && checkOwner && checkMember;
|
return groupSelected && checkOwner && checkMember;
|
||||||
}
|
}
|
||||||
|
|
||||||
}]);
|
}]);
|
||||||
|
|||||||
Reference in New Issue
Block a user