Refactor, add spec tests for equipment search

This commit is contained in:
Alexander Lin
2016-12-07 00:04:42 -08:00
parent 679459b83b
commit 3e92bb22fa
2 changed files with 103 additions and 14 deletions

View File

@@ -450,4 +450,84 @@ describe('Inventory Controller', function() {
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(['紅色軟帽', '紅色盾牌']);
});
});
});

View File

@@ -8,6 +8,7 @@ habitrpg.controller("InventoryCtrl",
$scope.selectedEgg = null; // {index: 1, name: "Tiger", value: 5}
$scope.selectedPotion = null; // {index: 5, name: "Red", value: 3}
$scope.equipmentFilterQuery = {'query': ''};
_updateDropAnimalCount(user.items);
@@ -85,7 +86,7 @@ habitrpg.controller("InventoryCtrl",
})
}, true);
var equipmentSearch = function(equipment, term) {
$scope.equipmentSearch = function(equipment, term) {
if (!equipment) return;
if (!angular.isString(term) || term.length == 0) {
return equipment;
@@ -101,25 +102,33 @@ habitrpg.controller("InventoryCtrl",
return result;
};
$scope.$watchGroup(['gearByClass', 'gearByType', 'user.equipmentQuery'], function(updatedVals) {
var gearByClass = updatedVals[0];
var gearByType = updatedVals[1];
var equipmentQuery = updatedVals[2];
$scope.updateEquipment = function(gearByClass, gearByType, equipmentQuery) {
$scope.filteredGearByClass = {};
$scope.filteredGearByType = {};
angular.forEach(gearByClass, function(value, key) {
var searchResult = equipmentSearch(value, equipmentQuery);
_.forEach(gearByClass, function(value, key) {
var searchResult = $scope.equipmentSearch(value, equipmentQuery);
if (searchResult.length > 0) {
$scope.filteredGearByClass[key] = searchResult;
}
});
angular.forEach(gearByType, function(value, key) {
var searchResult = equipmentSearch(value, equipmentQuery);
_.forEach(gearByType, function(value, key) {
var searchResult = $scope.equipmentSearch(value, equipmentQuery);
if (searchResult.length > 0) {
$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){
if ($scope.selectedEgg && $scope.selectedEgg.key == egg) {
@@ -456,12 +465,12 @@ habitrpg.controller("InventoryCtrl",
}
}
]);
habitrpg.controller("InventorySearchCtrl",
['$scope', 'User',
function($scope, User) {
['$scope',
function($scope) {
$scope.equipmentFilterQuery.query = '';
$scope.updateEquipmentQuery = function() {
User.user.equipmentQuery = $scope.equipmentQuery;
$scope.equipmentFilterQuery.query = $scope.equipmentQuery;
}
}
]);