From f854d2d02261fc772087f3ad39f6b30e50ed2e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Sat, 4 Jan 2014 00:01:24 -0300 Subject: [PATCH 1/2] Use $window on inventoryCtrl, so it can be mocked on tests --- public/js/controllers/inventoryCtrl.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/public/js/controllers/inventoryCtrl.js b/public/js/controllers/inventoryCtrl.js index dfb6e3715d..d9c70185bd 100644 --- a/public/js/controllers/inventoryCtrl.js +++ b/public/js/controllers/inventoryCtrl.js @@ -1,5 +1,5 @@ -habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', 'User', - function($rootScope, $scope, User) { +habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User', + function($rootScope, $scope, $window, User) { var user = User.user; var Content = $rootScope.Content; @@ -77,7 +77,7 @@ habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', 'User', } $scope.hatch = function(egg, potion){ - if (!confirm('Hatch a ' + potion.key + ' ' + egg.key + '?')) return; + if (!$window.confirm('Hatch a ' + potion.key + ' ' + egg.key + '?')) return; user.ops.hatch({params:{egg:egg.key, hatchingPotion:potion.key}}); $scope.selectedEgg = null; $scope.selectedPotion = null; @@ -91,7 +91,7 @@ habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', 'User', if(gems < item.value) return $rootScope.modals.buyGems = true; var string = (type == 'hatchingPotion') ? 'hatching potion' : type; // give hatchingPotion a space var message = "Buy this " + string + " with " + item.value + " of your " + gems + " Gems?" - if(confirm(message)) + if($window.confirm(message)) User.user.ops.purchase({params:{type:type,key:item.key}}); } @@ -102,8 +102,8 @@ habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', 'User', if ($scope.selectedFood) { var food = $scope.selectedFood if (food.key == 'Saddle') { - if (!confirm('Saddle ' + pet + '?')) return; - } else if (!confirm('Feed ' + pet + ' a ' + food.key + '?')) { + if (!$window.confirm('Saddle ' + pet + '?')) return; + } else if (!$window.confirm('Feed ' + pet + ' a ' + food.key + '?')) { return; } User.user.ops.feed({params:{pet: pet, food: food.key}}); From 24815af9c14f2c62c120f826e4f3cfe7c07d44a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Jureti=C4=87?= Date: Sat, 4 Jan 2014 00:02:51 -0300 Subject: [PATCH 2/2] Added inventoryCtrl tests for hatching and selling --- test/spec/inventoryCtrlSpec.js | 48 +++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/test/spec/inventoryCtrlSpec.js b/test/spec/inventoryCtrlSpec.js index a2551f841b..7ef00ef6e3 100644 --- a/test/spec/inventoryCtrlSpec.js +++ b/test/spec/inventoryCtrlSpec.js @@ -1,14 +1,25 @@ 'use strict'; describe('Inventory Controller', function() { - var scope, ctrl; + var scope, ctrl, user, $rootScope; beforeEach(module('habitrpg')); beforeEach(inject(function($rootScope, $controller){ - var user = {}; + user = { + user: { + stats: {gp: 0}, + items: {eggs: {'Cactus': 1}, hatchingPotions: {'Base': 1}, food: {'Meat': 1}, pets: {}}, + } + }; + window.habitrpgShared.wrap(user.user); + var mockWindow = { + confirm: function(msg){ + return true; + } + }; scope = $rootScope.$new(); $rootScope.Content = window.habitrpgShared.content; - ctrl = $controller('InventoryCtrl', {$scope: scope, User: user}); + ctrl = $controller('InventoryCtrl', {$scope: scope, User: user, $window: mockWindow}); })); it('starts without any item selected', function(){ @@ -26,4 +37,35 @@ describe('Inventory Controller', function() { scope.choosePotion('Base'); expect(scope.selectedPotion.key).to.eql('Base'); }); + + it('hatches a pet', function(){ + scope.chooseEgg('Cactus'); + scope.choosePotion('Base'); + expect(user.user.items.eggs).to.eql({'Cactus': 0}); + expect(user.user.items.hatchingPotions).to.eql({'Base': 0}); + expect(user.user.items.pets).to.eql({'Cactus-Base': 5}); + expect(scope.selectedEgg).to.eql(null); + expect(scope.selectedPotion).to.eql(null); + }); + + it('sells an egg', function(){ + scope.chooseEgg('Cactus'); + scope.sellInventory(); + expect(user.user.items.eggs).to.eql({'Cactus': 0}); + expect(user.user.stats.gp).to.eql(3); + }); + + it('sells a potion', function(){ + scope.choosePotion('Base'); + scope.sellInventory(); + expect(user.user.items.hatchingPotions).to.eql({'Base': 0}); + expect(user.user.stats.gp).to.eql(2); + }); + + it('sells food', function(){ + scope.chooseFood('Meat'); + scope.sellInventory(); + expect(user.user.items.food).to.eql({'Meat': 0}); + expect(user.user.stats.gp).to.eql(1); + }); });