mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Remove references to $rootScope.Shared & .Content in non-view code.
Instead, have them request services that return them so tests don’t need to prepare special state.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state',
|
habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'Shared', 'User', 'Challenges', 'Notification', '$compile', 'Groups', '$state',
|
||||||
function($rootScope, $scope, User, Challenges, Notification, $compile, Groups, $state) {
|
function($rootScope, $scope, Shared, User, Challenges, Notification, $compile, Groups, $state) {
|
||||||
|
|
||||||
// FIXME $scope.challenges needs to be resolved first (see app.js)
|
// FIXME $scope.challenges needs to be resolved first (see app.js)
|
||||||
$scope.groups = Groups.Group.query({type:'party,guilds,tavern'});
|
$scope.groups = Groups.Group.query({type:'party,guilds,tavern'});
|
||||||
@@ -133,7 +133,7 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenge
|
|||||||
//------------------------------------------------------------
|
//------------------------------------------------------------
|
||||||
|
|
||||||
$scope.addTask = function(addTo, listDef) {
|
$scope.addTask = function(addTo, listDef) {
|
||||||
var task = $rootScope.Shared.taskDefaults({text: listDef.newTask, type: listDef.type});
|
var task = Shared.taskDefaults({text: listDef.newTask, type: listDef.type});
|
||||||
addTo.unshift(task);
|
addTo.unshift(task);
|
||||||
//User.log({op: "addTask", data: task}); //TODO persist
|
//User.log({op: "addTask", data: task}); //TODO persist
|
||||||
delete listDef.newTask;
|
delete listDef.newTask;
|
||||||
@@ -229,4 +229,4 @@ habitrpg.controller("ChallengesCtrl", ['$rootScope','$scope', 'User', 'Challenge
|
|||||||
$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});
|
||||||
}
|
}
|
||||||
|
|
||||||
}]);
|
}]);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$state',
|
habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Shared', 'Groups', '$http', 'API_URL', '$q', 'User', 'Members', '$state',
|
||||||
function($scope, $rootScope, Groups, $http, API_URL, $q, User, Members, $state) {
|
function($scope, $rootScope, Shared, Groups, $http, API_URL, $q, User, Members, $state) {
|
||||||
|
|
||||||
$scope.isMember = function(user, group){
|
$scope.isMember = function(user, group){
|
||||||
return ~(group.members.indexOf(user._id));
|
return ~(group.members.indexOf(user._id));
|
||||||
@@ -66,7 +66,7 @@ habitrpg.controller("GroupsCtrl", ['$scope', '$rootScope', 'Groups', '$http', 'A
|
|||||||
// We watch Members.selectedMember because it's asynchronously set, so would be a hassle to handle updates here
|
// We watch Members.selectedMember because it's asynchronously set, so would be a hassle to handle updates here
|
||||||
$scope.$watch( function() { return Members.selectedMember; }, function (member) {
|
$scope.$watch( function() { return Members.selectedMember; }, function (member) {
|
||||||
if(member)
|
if(member)
|
||||||
member.petCount = $rootScope.Shared.countPets(null, member.items.pets);
|
member.petCount = Shared.countPets(null, member.items.pets);
|
||||||
$scope.profile = member;
|
$scope.profile = member;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User',
|
habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User', 'Content',
|
||||||
function($rootScope, $scope, $window, User) {
|
function($rootScope, $scope, $window, User, Content) {
|
||||||
|
|
||||||
var user = User.user;
|
var user = User.user;
|
||||||
var Content = $rootScope.Content;
|
|
||||||
|
|
||||||
// convenience vars since these are accessed frequently
|
// convenience vars since these are accessed frequently
|
||||||
|
|
||||||
@@ -135,4 +134,4 @@ habitrpg.controller("InventoryCtrl", ['$rootScope', '$scope', '$window', 'User',
|
|||||||
$scope.closeQuest();
|
$scope.closeQuest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
habitrpg.controller('NotificationCtrl',
|
habitrpg.controller('NotificationCtrl',
|
||||||
['$scope', '$rootScope', 'User', 'Guide', 'Notification', function ($scope, $rootScope, User, Guide, Notification) {
|
['$scope', '$rootScope', 'Shared', 'User', 'Guide', 'Notification', function ($scope, $rootScope, Shared, User, Guide, Notification) {
|
||||||
|
|
||||||
$rootScope.$watch('user.stats.hp', function(after, before) {
|
$rootScope.$watch('user.stats.hp', function(after, before) {
|
||||||
if (after == before) return;
|
if (after == before) return;
|
||||||
@@ -71,7 +71,7 @@ habitrpg.controller('NotificationCtrl',
|
|||||||
|
|
||||||
$rootScope.$watch('user.items.pets', function(after, before){
|
$rootScope.$watch('user.items.pets', function(after, before){
|
||||||
if(_.size(after) === _.size(before) ||
|
if(_.size(after) === _.size(before) ||
|
||||||
$rootScope.Shared.countPets(null, after) < 90) return;
|
Shared.countPets(null, after) < 90) return;
|
||||||
User.user.achievements.beastMaster = true;
|
User.user.achievements.beastMaster = true;
|
||||||
$rootScope.modals.achievements.beastMaster = true;
|
$rootScope.modals.achievements.beastMaster = true;
|
||||||
}, true);
|
}, true);
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
/* Make user and settings available for everyone through root scope.
|
/* Make user and settings available for everyone through root scope.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams', 'Notification', 'Groups',
|
habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$http', '$state', '$stateParams', 'Notification', 'Groups', 'Shared', 'Content',
|
||||||
function($scope, $rootScope, $location, User, $http, $state, $stateParams, Notification, Groups) {
|
function($scope, $rootScope, $location, User, $http, $state, $stateParams, Notification, Groups, Shared, Content) {
|
||||||
var user = User.user;
|
var user = User.user;
|
||||||
|
|
||||||
var initSticky = _.once(function(){
|
var initSticky = _.once(function(){
|
||||||
@@ -20,8 +20,8 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
|
|||||||
$rootScope.moment = window.moment;
|
$rootScope.moment = window.moment;
|
||||||
$rootScope._ = window._;
|
$rootScope._ = window._;
|
||||||
$rootScope.settings = User.settings;
|
$rootScope.settings = User.settings;
|
||||||
$rootScope.Shared = window.habitrpgShared;
|
$rootScope.Shared = Shared;
|
||||||
$rootScope.Content = window.habitrpgShared.content;
|
$rootScope.Content = Content;
|
||||||
|
|
||||||
// Angular UI Router
|
// Angular UI Router
|
||||||
$rootScope.$state = $state;
|
$rootScope.$state = $state;
|
||||||
@@ -45,10 +45,10 @@ habitrpg.controller("RootCtrl", ['$scope', '$rootScope', '$location', 'User', '$
|
|||||||
// count pets, mounts collected totals, etc
|
// count pets, mounts collected totals, etc
|
||||||
$rootScope.countExists = function(items) {return _.reduce(items,function(m,v){return m+(v?1:0)},0)}
|
$rootScope.countExists = function(items) {return _.reduce(items,function(m,v){return m+(v?1:0)},0)}
|
||||||
|
|
||||||
$rootScope.petCount = $rootScope.Shared.countPets(null, User.user.items.pets);
|
$rootScope.petCount = Shared.countPets(null, User.user.items.pets);
|
||||||
|
|
||||||
$rootScope.$watch('user.items.pets', function(pets){
|
$rootScope.$watch('user.items.pets', function(pets){
|
||||||
$rootScope.petCount = $rootScope.Shared.countPets($rootScope.countExists(pets), User.user.items.pets);
|
$rootScope.petCount = Shared.countPets($rootScope.countExists(pets), User.user.items.pets);
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
$scope.safeApply = function(fn) {
|
$scope.safeApply = function(fn) {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','Notification', '$http', 'API_URL', '$timeout',
|
habitrpg.controller("TasksCtrl", ['$scope', '$location', 'User','Notification', '$http', 'API_URL', '$timeout', 'Shared',
|
||||||
function($scope, $rootScope, $location, User, Notification, $http, API_URL, $timeout) {
|
function($scope, $location, User, Notification, $http, API_URL, $timeout, Shared) {
|
||||||
$scope.obj = User.user; // used for task-lists
|
$scope.obj = User.user; // used for task-lists
|
||||||
$scope.user = User.user;
|
$scope.user = User.user;
|
||||||
|
|
||||||
@@ -138,7 +138,7 @@ habitrpg.controller("TasksCtrl", ['$scope', '$rootScope', '$location', 'User','N
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
$scope.$watch('user.items.gear.equipped', function(){
|
$scope.$watch('user.items.gear.equipped', function(){
|
||||||
$scope.itemStore = $rootScope.Shared.updateStore(User.user);
|
$scope.itemStore = Shared.updateStore(User.user);
|
||||||
},true);
|
},true);
|
||||||
|
|
||||||
$scope.buy = function(item) {
|
$scope.buy = function(item) {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$http', '$state', 'Guide',
|
habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$http', '$state', 'Guide', 'Shared',
|
||||||
function($rootScope, $scope, $location, User, $http, $state, Guide) {
|
function($rootScope, $scope, $location, User, $http, $state, Guide, Shared) {
|
||||||
$scope.profile = User.user;
|
$scope.profile = User.user;
|
||||||
$scope.profile.petCount = $rootScope.Shared.countPets(null, $scope.profile.items.pets);
|
$scope.profile.petCount = Shared.countPets(null, $scope.profile.items.pets);
|
||||||
$scope.hideUserAvatar = function() {
|
$scope.hideUserAvatar = function() {
|
||||||
$(".userAvatar").hide();
|
$(".userAvatar").hide();
|
||||||
};
|
};
|
||||||
@@ -25,7 +25,7 @@ habitrpg.controller("UserCtrl", ['$rootScope', '$scope', '$location', 'User', '$
|
|||||||
|
|
||||||
User.user.ops.changeClass({query:{class:klass}});
|
User.user.ops.changeClass({query:{class:klass}});
|
||||||
$scope.selectedClass = undefined;
|
$scope.selectedClass = undefined;
|
||||||
$rootScope.Shared.updateStore(User.user);
|
Shared.updateStore(User.user);
|
||||||
$state.go('options.profile.stats');
|
$state.go('options.profile.stats');
|
||||||
window.setTimeout(Guide.classesTour, 10);
|
window.setTimeout(Guide.classesTour, 10);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
* Services that persists and retrieves user from localStorage.
|
* Services that persists and retrieves user from localStorage.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
angular.module('memberServices', ['ngResource']).
|
angular.module('memberServices', ['ngResource', 'sharedServices']).
|
||||||
factory('Members', ['$rootScope', 'API_URL', '$resource',
|
factory('Members', ['$rootScope', 'Shared', 'API_URL', '$resource',
|
||||||
function($rootScope, API_URL, $resource) {
|
function($rootScope, Shared, API_URL, $resource) {
|
||||||
var members = {};
|
var members = {};
|
||||||
var Member = $resource(API_URL + '/api/v2/members/:uid', {uid:'@_id'});
|
var Member = $resource(API_URL + '/api/v2/members/:uid', {uid:'@_id'});
|
||||||
var memberServices = {
|
var memberServices = {
|
||||||
@@ -65,12 +65,12 @@ angular.module('memberServices', ['ngResource']).
|
|||||||
// and then for guild)
|
// and then for guild)
|
||||||
// and if not, fetch them
|
// and if not, fetch them
|
||||||
if (members[uid] && members[uid].items && members[uid].items.weapon) {
|
if (members[uid] && members[uid].items && members[uid].items.weapon) {
|
||||||
$rootScope.Shared.wrap(members[uid],false);
|
Shared.wrap(members[uid],false);
|
||||||
self.selectedMember = members[uid];
|
self.selectedMember = members[uid];
|
||||||
} else {
|
} else {
|
||||||
Member.get({uid: uid}, function(member){
|
Member.get({uid: uid}, function(member){
|
||||||
self.populate(member); // lazy load for later
|
self.populate(member); // lazy load for later
|
||||||
$rootScope.Shared.wrap(member,false);
|
Shared.wrap(member,false);
|
||||||
self.selectedMember = members[member._id];
|
self.selectedMember = members[member._id];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
15
public/js/services/sharedServices.js
Normal file
15
public/js/services/sharedServices.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Services that expose habitrpg-shared
|
||||||
|
*/
|
||||||
|
|
||||||
|
angular.module('sharedServices', []).
|
||||||
|
factory('Shared', [function () {
|
||||||
|
return window.habitrpgShared;
|
||||||
|
}
|
||||||
|
]).
|
||||||
|
factory('Content', ['Shared', function (Shared) {
|
||||||
|
return Shared.content;
|
||||||
|
}
|
||||||
|
]);
|
||||||
@@ -32,6 +32,7 @@
|
|||||||
"bower_components/habitrpg-shared/dist/habitrpg-shared.js",
|
"bower_components/habitrpg-shared/dist/habitrpg-shared.js",
|
||||||
|
|
||||||
"js/app.js",
|
"js/app.js",
|
||||||
|
"js/services/sharedServices.js",
|
||||||
"js/services/authServices.js",
|
"js/services/authServices.js",
|
||||||
"js/services/notificationServices.js",
|
"js/services/notificationServices.js",
|
||||||
"bower_components/habitrpg-shared/script/userServices.js",
|
"bower_components/habitrpg-shared/script/userServices.js",
|
||||||
@@ -87,4 +88,4 @@
|
|||||||
"static.css"
|
"static.css"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,9 @@ describe('Filters Controller', function() {
|
|||||||
var scope, user;
|
var scope, user;
|
||||||
|
|
||||||
beforeEach(module('habitrpg'));
|
beforeEach(module('habitrpg'));
|
||||||
beforeEach(inject(function($rootScope, $controller) {
|
beforeEach(inject(function($rootScope, $controller, Shared) {
|
||||||
user = {filters: {}};
|
user = {filters: {}};
|
||||||
window.habitrpgShared.wrap(user);
|
Shared.wrap(user);
|
||||||
scope = $rootScope.$new();
|
scope = $rootScope.$new();
|
||||||
$controller('FiltersCtrl', {$scope: scope, User: {user: user}});
|
$controller('FiltersCtrl', {$scope: scope, User: {user: user}});
|
||||||
}));
|
}));
|
||||||
@@ -18,11 +18,11 @@ describe('Filters Controller', function() {
|
|||||||
expect(user.tags[0]).to.have.property('id');
|
expect(user.tags[0]).to.have.property('id');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('toggles tag filtering', function(){
|
it('toggles tag filtering', inject(function(Shared){
|
||||||
var tag = {id: window.habitrpgShared.uuid(), name: 'myTag'};
|
var tag = {id: Shared.uuid(), name: 'myTag'};
|
||||||
scope.toggleFilter(tag);
|
scope.toggleFilter(tag);
|
||||||
expect(user.filters[tag.id]).to.eql(true);
|
expect(user.filters[tag.id]).to.eql(true);
|
||||||
scope.toggleFilter(tag);
|
scope.toggleFilter(tag);
|
||||||
expect(user.filters[tag.id]).to.eql(false);
|
expect(user.filters[tag.id]).to.eql(false);
|
||||||
})
|
}))
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,20 +4,19 @@ describe('Inventory Controller', function() {
|
|||||||
var scope, ctrl, user, $rootScope;
|
var scope, ctrl, user, $rootScope;
|
||||||
|
|
||||||
beforeEach(module('habitrpg'));
|
beforeEach(module('habitrpg'));
|
||||||
beforeEach(inject(function($rootScope, $controller){
|
beforeEach(inject(function($rootScope, $controller, Shared){
|
||||||
user = {
|
user = {
|
||||||
balance: 4,
|
balance: 4,
|
||||||
stats: {gp: 0},
|
stats: {gp: 0},
|
||||||
items: {eggs: {Cactus: 1}, hatchingPotions: {Base: 1}, food: {Meat: 1}, pets: {}},
|
items: {eggs: {Cactus: 1}, hatchingPotions: {Base: 1}, food: {Meat: 1}, pets: {}},
|
||||||
};
|
};
|
||||||
window.habitrpgShared.wrap(user);
|
Shared.wrap(user);
|
||||||
var mockWindow = {
|
var mockWindow = {
|
||||||
confirm: function(msg){
|
confirm: function(msg){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
scope = $rootScope.$new();
|
scope = $rootScope.$new();
|
||||||
$rootScope.Content = window.habitrpgShared.content;
|
|
||||||
ctrl = $controller('InventoryCtrl', {$scope: scope, User: {user: user}, $window: mockWindow});
|
ctrl = $controller('InventoryCtrl', {$scope: scope, User: {user: user}, $window: mockWindow});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -74,9 +73,9 @@ describe('Inventory Controller', function() {
|
|||||||
expect(user.items.currentPet).to.eql('Cactus-Base');
|
expect(user.items.currentPet).to.eql('Cactus-Base');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('purchases an egg', function(){
|
it('purchases an egg', inject(function(Content){
|
||||||
scope.purchase('eggs', window.habitrpgShared.content.eggs['Wolf']);
|
scope.purchase('eggs', Content.eggs['Wolf']);
|
||||||
expect(user.balance).to.eql(3.25);
|
expect(user.balance).to.eql(3.25);
|
||||||
expect(user.items.eggs).to.eql({Cactus: 1, Wolf: 1})
|
expect(user.items.eggs).to.eql({Cactus: 1, Wolf: 1})
|
||||||
});
|
}));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,16 +6,10 @@ describe('memberServices', function() {
|
|||||||
beforeEach(module('memberServices'));
|
beforeEach(module('memberServices'));
|
||||||
beforeEach(module('habitrpg'));
|
beforeEach(module('habitrpg'));
|
||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(inject(function (_$httpBackend_, Members) {
|
||||||
inject(function(_$httpBackend_, $rootScope){
|
$httpBackend = _$httpBackend_;
|
||||||
$httpBackend = _$httpBackend_;
|
members = Members;
|
||||||
$rootScope.Shared = window.habitrpgShared;
|
}));
|
||||||
});
|
|
||||||
// $rootScope.Shared is set, so now we can inject Members
|
|
||||||
inject(function(Members){
|
|
||||||
members = Members;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('has no members at the beginning', function() {
|
it('has no members at the beginning', function() {
|
||||||
expect(members.members).to.be.an('object');
|
expect(members.members).to.be.an('object');
|
||||||
@@ -43,4 +37,4 @@ describe('memberServices', function() {
|
|||||||
expect(members.members).to.have.property(uid);
|
expect(members.members).to.have.property(uid);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ describe('userServices', function() {
|
|||||||
|
|
||||||
beforeEach(function(){
|
beforeEach(function(){
|
||||||
module(function($provide){
|
module(function($provide){
|
||||||
var habitrpgShared = {helpers: {newUser: sinon.spy()}};
|
$window = {href: '', alert: sinon.spy(), location: {search: '', pathname: ''}};
|
||||||
$window = {href: '', alert: sinon.spy(), location: {search: '', pathname: ''}, habitrpgShared: habitrpgShared};
|
|
||||||
$provide.value('$window', $window);
|
$provide.value('$window', $window);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -48,4 +47,4 @@ describe('userServices', function() {
|
|||||||
//TODO where does that null comes from?
|
//TODO where does that null comes from?
|
||||||
expect(user.settings.sync.queue).to.eql([null, {}]);
|
expect(user.settings.sync.queue).to.eql([null, {}]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user