Merge "magic-overrides should clone from currentSearch"

This commit is contained in:
Jenkins 2015-09-19 06:32:07 +00:00 committed by Gerrit Code Review
commit bae0bea675
2 changed files with 98 additions and 46 deletions

View File

@ -75,6 +75,7 @@
return directive; return directive;
function MagicOverridesController($element, $scope, $timeout, $window) { function MagicOverridesController($element, $scope, $timeout, $window) {
/** /**
* showMenu and hideMenu depend on Foundation's dropdown. They need * showMenu and hideMenu depend on Foundation's dropdown. They need
* to be modified to work with another dropdown implementation. * to be modified to work with another dropdown implementation.
@ -103,27 +104,39 @@
$scope.initSearch(); $scope.initSearch();
}); });
}); });
$scope.$on('$destroy', function () { $scope.$on('$destroy', function () {
facetsChangedWatcher(); facetsChangedWatcher();
}); });
function getFacets(currentFacets) {
if (angular.isUndefined(currentFacets)) {
var initialFacets = $window.location.search;
if (initialFacets.indexOf('?') === 0) {
initialFacets = initialFacets.slice(1);
}
return initialFacets.split('&');
} else {
return currentFacets.map(function(facet) {
return facet.name;
});
}
}
/** /**
* Override magic_search.js 'initFacets' to fix browser refresh issue * Override magic_search.js 'initFacets' to fix browser refresh issue
* and to emit('checkFacets') to flag facets as 'isServer' * and to emit('checkFacets') to flag facets as 'isServer'
*/ */
$scope.initFacets = function () { $scope.initFacets = function(currentFacets) {
// set facets selected and remove them from 'facetsObj' var facets = getFacets(currentFacets);
var initialFacets = $window.location.search;
if (initialFacets.indexOf('?') === 0) { if (facets.length > 1 || (facets[0] && facets[0].length > 0)) {
initialFacets = initialFacets.slice(1);
}
initialFacets = initialFacets.split('&');
if (initialFacets.length > 1 || initialFacets[0].length > 0) {
$timeout(function () { $timeout(function () {
$scope.strings.prompt = ''; $scope.strings.prompt = '';
}); });
} }
angular.forEach(initialFacets, function (facet) {
angular.forEach(facets, function(facet) {
var facetParts = facet.split('='); var facetParts = facet.split('=');
angular.forEach($scope.facetsObj, function (value) { angular.forEach($scope.facetsObj, function (value) {
if (value.name == facetParts[0]) { if (value.name == facetParts[0]) {
@ -188,12 +201,15 @@
} }
// re-init to restore facets cleanly // re-init to restore facets cleanly
$scope.facetsObj = $scope.copyFacets($scope.facetsSave); $scope.facetsObj = $scope.copyFacets($scope.facetsSave);
var currentSearch = angular.copy($scope.currentSearch);
$scope.currentSearch = []; $scope.currentSearch = [];
$scope.initFacets(); $scope.initFacets(currentSearch);
// broadcast to check facets for server-side // broadcast to check facets for server-side
$scope.$emit('checkFacets', $scope.currentSearch); $scope.$emit('checkFacets', $scope.currentSearch);
}; };
$scope.emitQuery();
} }
} }
})(); })();

View File

@ -149,9 +149,22 @@
expect($magicScope.deleteFacetEntirely).toHaveBeenCalledWith([ 'name', 'myname' ]); expect($magicScope.deleteFacetEntirely).toHaveBeenCalledWith([ 'name', 'myname' ]);
}); });
it('currentSearch should have two items when URL has two search terms', function () { it('currentSearch should have one item when given one search term', function () {
$window.location.search = '?name=myname&status=active'; var currentFacets = [{name: 'name=myname'}];
$magicScope.initFacets(); $magicScope.initFacets(currentFacets);
$timeout.flush();
expect($magicScope.currentSearch.length).toBe(1);
expect($magicScope.currentSearch[0].label).toEqual([ 'Name', 'myname' ]);
expect($magicScope.currentSearch[0].name).toBe('name=myname');
// 'name' facet should be deleted (singleton)
expect($magicScope.deleteFacetEntirely).toHaveBeenCalledWith([ 'name', 'myname' ]);
});
it('currentSearch should have two items when given two search terms', function () {
var currentFacets = [{name: 'name=myname'}, {name: 'status=active'}];
$magicScope.initFacets(currentFacets);
$timeout.flush(); $timeout.flush();
// only 'active' option should be removed from 'status' facet (not singleton) // only 'active' option should be removed from 'status' facet (not singleton)
@ -160,8 +173,8 @@
}); });
it('flavor facet should be removed if search term includes flavor', function () { it('flavor facet should be removed if search term includes flavor', function () {
$window.location.search = '?flavor=m1.tiny'; var currentFacets = [{name: 'flavor=m1.tiny'}];
$magicScope.initFacets(); $magicScope.initFacets(currentFacets);
$timeout.flush(); $timeout.flush();
// entire 'flavor' facet should be removed even if some options left (singleton) // entire 'flavor' facet should be removed even if some options left (singleton)
@ -170,24 +183,27 @@
it('currentSearch should have one item when search is textSearch', function () { it('currentSearch should have one item when search is textSearch', function () {
$magicScope.textSearch = 'test'; $magicScope.textSearch = 'test';
$magicScope.initFacets(); $magicScope.initFacets([]);
$timeout.flush();
expect($magicScope.currentSearch[0].label).toEqual([ 'Text', 'test' ]); expect($magicScope.currentSearch[0].label).toEqual([ 'Text', 'test' ]);
expect($magicScope.currentSearch[0].name).toBe('text=test'); expect($magicScope.currentSearch[0].name).toBe('text=test');
}); });
it('filteredObj should have two remaining items when URL has one search term', function () { it('currentSearch should have textSearch and currentSearch', function () {
$window.location.search = '?name=myname&status=active'; $magicScope.textSearch = 'test';
$magicScope.initFacets(); $magicScope.initFacets([{name: 'flavor=m1.tiny'}]);
$timeout.flush();
// filteredObj should have only unused facets and options expect($magicScope.currentSearch.length).toBe(2);
// 'name' facet is singleton and should have been removed expect($magicScope.currentSearch[0].label).toEqual([ 'Flavor', 'm1.tiny' ]);
// 'status' facet is not single and should remain with one option left expect($magicScope.currentSearch[0].name).toBe('flavor=m1.tiny');
expect($magicScope.filteredObj.length).toBe(2); expect($magicScope.currentSearch[1].label).toEqual([ 'Text', 'test' ]);
expect($magicScope.currentSearch[1].name).toBe('text=test');
}); });
it('should call checkFacets when initFacets called', function () { it('should call checkFacets when initFacets called', function () {
$magicScope.initFacets(); $magicScope.initFacets([]);
expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []);
}); });
@ -195,7 +211,7 @@
describe('removeFacet', function () { describe('removeFacet', function () {
beforeEach(function () { beforeEach(function () {
spyOn($magicScope, 'initFacets'); spyOn($magicScope, 'initFacets').and.callThrough();
}); });
it('should call emitQuery, initFacets and emit checkFacets on removeFacet', function () { it('should call emitQuery, initFacets and emit checkFacets on removeFacet', function () {
@ -208,7 +224,7 @@
expect($magicScope.currentSearch).toEqual([]); expect($magicScope.currentSearch).toEqual([]);
expect($magicScope.emitQuery).toHaveBeenCalledWith('name=myname'); expect($magicScope.emitQuery).toHaveBeenCalledWith('name=myname');
expect($magicScope.initFacets).toHaveBeenCalled(); expect($magicScope.initFacets).toHaveBeenCalledWith([]);
expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []);
expect($magicScope.strings.prompt).toBe('Prompt'); expect($magicScope.strings.prompt).toBe('Prompt');
}); });
@ -223,10 +239,7 @@
expect($magicScope.strings.prompt).toBe(''); expect($magicScope.strings.prompt).toBe('');
}); });
/*eslint-disable max-len */ it('should emit checkFacets on removeFacet if facetSelected', function () {
it('should call resetState, initFacets and emit checkFacets on removeFacet when facet selected',
/*eslint-enable max-len */
function () {
var initialSearch = { var initialSearch = {
name: 'name=myname', name: 'name=myname',
label: [ 'Name', 'myname' ] label: [ 'Name', 'myname' ]
@ -240,10 +253,33 @@
expect($magicScope.currentSearch).toEqual([]); expect($magicScope.currentSearch).toEqual([]);
expect($magicScope.resetState).toHaveBeenCalled(); expect($magicScope.resetState).toHaveBeenCalled();
expect($magicScope.initFacets).toHaveBeenCalled(); expect($magicScope.initFacets).toHaveBeenCalledWith([]);
expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []); expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', []);
} });
);
it('should emit checkFacets and remember state on removeFacet if facetSelected', function () {
var search1 = {
name: 'name=myname',
label: [ 'Name', 'myname' ]
};
var search2 = {
name: 'flavor=m1.tiny',
label: [ 'Flavor', 'm1.tiny' ]
};
$magicScope.currentSearch.push(search1);
$magicScope.currentSearch.push(search2);
$magicScope.facetSelected = {
'name': 'status',
'label': [ 'Status', 'active' ]
};
$magicScope.removeFacet(0);
expect($magicScope.currentSearch).toEqual([search2]);
expect($magicScope.resetState).toHaveBeenCalled();
expect($magicScope.initFacets).toHaveBeenCalledWith([search2]);
expect($magicScope.$emit).toHaveBeenCalledWith('checkFacets', [search2]);
});
}); });
}); });