mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Refactor, add spec tests for equipment search
This commit is contained in:
@@ -450,4 +450,84 @@ describe('Inventory Controller', function() {
|
|||||||
expect(scope.hasAllTimeTravelerItemsOfType('mounts')).to.eql(true);
|
expect(scope.hasAllTimeTravelerItemsOfType('mounts')).to.eql(true);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Gear search filter', function() {
|
||||||
|
var wrap = function(text) {
|
||||||
|
return {'text': function() {return text;}};
|
||||||
|
}
|
||||||
|
|
||||||
|
var toText = function(list) {
|
||||||
|
return _.map(list, function(ele) { return ele.text(); });
|
||||||
|
}
|
||||||
|
|
||||||
|
var gearByClass, gearByType;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
scope.$digest();
|
||||||
|
gearByClass = {'raw': [wrap('kale'), wrap('sashimi')],
|
||||||
|
'cooked': [wrap('chicken'), wrap('potato')]};
|
||||||
|
|
||||||
|
gearByType = {'veg': [wrap('kale'), wrap('potato')],
|
||||||
|
'not': [wrap('chicken'), wrap('sashimi')]};
|
||||||
|
scope.gearByClass = gearByClass;
|
||||||
|
scope.gearByType = gearByType;
|
||||||
|
scope.equipmentFilterQuery.query = 'a';
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters nothing if equipmentQuery is nothing', function() {
|
||||||
|
scope.equipmentFilterQuery.query = '';
|
||||||
|
scope.$digest();
|
||||||
|
expect(toText(scope.filteredGearByClass['raw'])).to.eql(['kale', 'sashimi']);
|
||||||
|
expect(toText(scope.filteredGearByClass['cooked'])).to.eql(['chicken', 'potato']);
|
||||||
|
expect(toText(scope.filteredGearByType['veg'])).to.eql(['kale', 'potato']);
|
||||||
|
expect(toText(scope.filteredGearByType['not'])).to.eql(['chicken', 'sashimi']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters out gear if class gear changes', function() {
|
||||||
|
scope.$digest();
|
||||||
|
expect(toText(scope.filteredGearByClass['raw'])).to.eql(['kale', 'sashimi']);
|
||||||
|
expect(toText(scope.filteredGearByClass['cooked'])).to.eql(['potato']);
|
||||||
|
|
||||||
|
scope.gearByClass['raw'].push(wrap('zucchini'));
|
||||||
|
scope.gearByClass['cooked'].push(wrap('pizza'));
|
||||||
|
scope.$digest();
|
||||||
|
expect(toText(scope.filteredGearByClass['raw'])).to.eql(['kale', 'sashimi']);
|
||||||
|
expect(toText(scope.filteredGearByClass['cooked'])).to.eql(['potato', 'pizza']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters out gear if typed gear changes', function() {
|
||||||
|
scope.$digest();
|
||||||
|
expect(toText(scope.filteredGearByType['veg'])).to.eql(['kale', 'potato']);
|
||||||
|
expect(toText(scope.filteredGearByType['not'])).to.eql(['sashimi']);
|
||||||
|
|
||||||
|
scope.gearByType['veg'].push(wrap('zucchini'));
|
||||||
|
scope.gearByType['not'].push(wrap('pizza'));
|
||||||
|
|
||||||
|
scope.$digest();
|
||||||
|
expect(toText(scope.filteredGearByType['veg'])).to.eql(['kale', 'potato']);
|
||||||
|
expect(toText(scope.filteredGearByType['not'])).to.eql(['sashimi', 'pizza']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('filters out gear if filter query changes', function() {
|
||||||
|
scope.equipmentFilterQuery.query = 'c';
|
||||||
|
scope.$digest();
|
||||||
|
|
||||||
|
expect(toText(scope.filteredGearByClass['raw'])).to.eql([]);
|
||||||
|
expect(toText(scope.filteredGearByClass['cooked'])).to.eql(['chicken']);
|
||||||
|
expect(toText(scope.filteredGearByType['veg'])).to.eql([]);
|
||||||
|
expect(toText(scope.filteredGearByType['not'])).to.eql(['chicken']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the right filtered gear', function() {
|
||||||
|
var equipment = [wrap('spicy tuna'), wrap('dragon'), wrap('rainbow'), wrap('caterpillar')];
|
||||||
|
expect(toText(scope.equipmentSearch(equipment, 'ra'))).to.eql(['dragon', 'rainbow']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the right filtered gear if the source gear has unicode', function() {
|
||||||
|
// blue hat, red hat, red shield
|
||||||
|
var equipment = [wrap('藍色軟帽'), wrap('紅色軟帽'), wrap('紅色盾牌')];
|
||||||
|
// searching for 'red' gives red hat, red shield
|
||||||
|
expect(toText(scope.equipmentSearch(equipment, '紅色'))).to.eql(['紅色軟帽', '紅色盾牌']);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ habitrpg.controller("InventoryCtrl",
|
|||||||
|
|
||||||
$scope.selectedEgg = null; // {index: 1, name: "Tiger", value: 5}
|
$scope.selectedEgg = null; // {index: 1, name: "Tiger", value: 5}
|
||||||
$scope.selectedPotion = null; // {index: 5, name: "Red", value: 3}
|
$scope.selectedPotion = null; // {index: 5, name: "Red", value: 3}
|
||||||
|
$scope.equipmentFilterQuery = {'query': ''};
|
||||||
|
|
||||||
_updateDropAnimalCount(user.items);
|
_updateDropAnimalCount(user.items);
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ habitrpg.controller("InventoryCtrl",
|
|||||||
})
|
})
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
var equipmentSearch = function(equipment, term) {
|
$scope.equipmentSearch = function(equipment, term) {
|
||||||
if (!equipment) return;
|
if (!equipment) return;
|
||||||
if (!angular.isString(term) || term.length == 0) {
|
if (!angular.isString(term) || term.length == 0) {
|
||||||
return equipment;
|
return equipment;
|
||||||
@@ -101,25 +102,33 @@ habitrpg.controller("InventoryCtrl",
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.$watchGroup(['gearByClass', 'gearByType', 'user.equipmentQuery'], function(updatedVals) {
|
$scope.updateEquipment = function(gearByClass, gearByType, equipmentQuery) {
|
||||||
var gearByClass = updatedVals[0];
|
|
||||||
var gearByType = updatedVals[1];
|
|
||||||
var equipmentQuery = updatedVals[2];
|
|
||||||
$scope.filteredGearByClass = {};
|
$scope.filteredGearByClass = {};
|
||||||
$scope.filteredGearByType = {};
|
$scope.filteredGearByType = {};
|
||||||
angular.forEach(gearByClass, function(value, key) {
|
_.forEach(gearByClass, function(value, key) {
|
||||||
var searchResult = equipmentSearch(value, equipmentQuery);
|
var searchResult = $scope.equipmentSearch(value, equipmentQuery);
|
||||||
if (searchResult.length > 0) {
|
if (searchResult.length > 0) {
|
||||||
$scope.filteredGearByClass[key] = searchResult;
|
$scope.filteredGearByClass[key] = searchResult;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
angular.forEach(gearByType, function(value, key) {
|
_.forEach(gearByType, function(value, key) {
|
||||||
var searchResult = equipmentSearch(value, equipmentQuery);
|
var searchResult = $scope.equipmentSearch(value, equipmentQuery);
|
||||||
if (searchResult.length > 0) {
|
if (searchResult.length > 0) {
|
||||||
$scope.filteredGearByType[key] = searchResult;
|
$scope.filteredGearByType[key] = searchResult;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
$scope.$watch(function(){
|
||||||
|
return ['gearByClass', 'gearByType', 'equipmentFilterQuery'].map(angular.bind($scope, $scope.$eval));
|
||||||
|
}, function(updatedVals) {
|
||||||
|
var gearByClass = updatedVals[0];
|
||||||
|
var gearByType = updatedVals[1];
|
||||||
|
var equipmentFilterQuery = updatedVals[2];
|
||||||
|
$scope.updateEquipment(gearByClass, gearByType, equipmentFilterQuery.query);
|
||||||
|
}, true);
|
||||||
|
|
||||||
|
$scope.updateEquipment($scope.gearByClass, $scope.gearByType, $scope.equipmentFilterQuery.query);
|
||||||
|
|
||||||
$scope.chooseEgg = function(egg){
|
$scope.chooseEgg = function(egg){
|
||||||
if ($scope.selectedEgg && $scope.selectedEgg.key == egg) {
|
if ($scope.selectedEgg && $scope.selectedEgg.key == egg) {
|
||||||
@@ -456,12 +465,12 @@ habitrpg.controller("InventoryCtrl",
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|
||||||
habitrpg.controller("InventorySearchCtrl",
|
habitrpg.controller("InventorySearchCtrl",
|
||||||
['$scope', 'User',
|
['$scope',
|
||||||
function($scope, User) {
|
function($scope) {
|
||||||
|
$scope.equipmentFilterQuery.query = '';
|
||||||
$scope.updateEquipmentQuery = function() {
|
$scope.updateEquipmentQuery = function() {
|
||||||
User.user.equipmentQuery = $scope.equipmentQuery;
|
$scope.equipmentFilterQuery.query = $scope.equipmentQuery;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
|||||||
Reference in New Issue
Block a user