[Search] Moved isLoading

Moved the isLoading function away from
the aggregator. It is now the controller's
responsibility. Corresponding tests
updated.
This commit is contained in:
slhale
2015-08-03 10:25:23 -07:00
parent 19b9668190
commit eb0bdba666
4 changed files with 19 additions and 33 deletions

View File

@ -42,7 +42,6 @@ define(
* aggregated * aggregated
*/ */
function SearchAggregator($q, providers) { function SearchAggregator($q, providers) {
var loading;
// Remove duplicate objects that have the same ID. Modifies the passed // Remove duplicate objects that have the same ID. Modifies the passed
// array, and returns the number that were removed. // array, and returns the number that were removed.
@ -89,9 +88,6 @@ define(
timestamp = Date.now(), timestamp = Date.now(),
resultPromises = []; resultPromises = [];
// We are loading
loading = true;
// Send the query to all the providers // Send the query to all the providers
for (i = 0; i < providers.length; i += 1) { for (i = 0; i < providers.length; i += 1) {
resultPromises.push( resultPromises.push(
@ -114,9 +110,6 @@ define(
orderByScore(results); orderByScore(results);
totalSum -= filterDuplicates(results, totalSum); totalSum -= filterDuplicates(results, totalSum);
// We are done loading
loading = false;
return { return {
hits: results, hits: results,
total: totalSum, total: totalSum,
@ -135,15 +128,7 @@ define(
* *
* @param inputText The text input that is the query. * @param inputText The text input that is the query.
*/ */
query: queryAll, query: queryAll
/**
* Checks to see if we are still waiting for the results to be
* fully updated.
*/
isLoading: function () {
return loading;
}
}; };
} }

View File

@ -33,11 +33,15 @@ define(function () {
function SearchController($scope, searchService) { function SearchController($scope, searchService) {
// Starting amount of results to load. Will get increased. // Starting amount of results to load. Will get increased.
var numResults = INITIAL_LOAD_NUMBER, var numResults = INITIAL_LOAD_NUMBER,
loading = false,
fullResults = []; fullResults = [];
function search() { function search() {
var inputText = $scope.ngModel.input; var inputText = $scope.ngModel.input;
// We are starting to load.
loading = true;
// Update whether the file tree should be displayed // Update whether the file tree should be displayed
if (inputText === '' || inputText === undefined) { if (inputText === '' || inputText === undefined) {
$scope.ngModel.search = false; $scope.ngModel.search = false;
@ -52,6 +56,9 @@ define(function () {
searchService.query(inputText).then(function (result) { searchService.query(inputText).then(function (result) {
fullResults = result.hits; fullResults = result.hits;
$scope.results = result.hits.slice(0, numResults); $scope.results = result.hits.slice(0, numResults);
// Now we are done loading.
loading = false;
}); });
} }
@ -67,7 +74,7 @@ define(function () {
* fully updated. * fully updated.
*/ */
isLoading: function () { isLoading: function () {
return searchService.isLoading(); return loading;
}, },
/** /**

View File

@ -96,18 +96,6 @@ define(
} }
}); });
it("is loading until all the providers' promises fufill", function () {
expect(aggregator.isLoading()).toBeFalsy();
// Send query
aggregator.query();
expect(aggregator.isLoading()).toBeTruthy();
// Then resolve the promises
mockAggregatorResults = mockPromise.then.mostRecentCall.args[0]([]);
expect(aggregator.isLoading()).toBeFalsy();
});
}); });
} }
); );

View File

@ -55,7 +55,7 @@ define(
mockSearchService = jasmine.createSpyObj( mockSearchService = jasmine.createSpyObj(
"searchService", "searchService",
[ "query", "isLoading" ] [ "query" ]
); );
mockPromise = jasmine.createSpyObj( mockPromise = jasmine.createSpyObj(
"promise", "promise",
@ -78,11 +78,17 @@ define(
expect(mockScope.results).toBeDefined(); expect(mockScope.results).toBeDefined();
}); });
it("checks if the search service is loading", function () { it("is loading until the service's promise fufills", function () {
controller.isLoading(); // Send query
expect(mockSearchService.isLoading).toHaveBeenCalled(); controller.search();
expect(controller.isLoading()).toBeTruthy();
// Then resolve the promises
mockPromise.then.mostRecentCall.args[0]({hits: []});
expect(controller.isLoading()).toBeFalsy();
}); });
it("displays only some results when there are many", function () { it("displays only some results when there are many", function () {
expect(mockPromise.then).toHaveBeenCalledWith(jasmine.any(Function)); expect(mockPromise.then).toHaveBeenCalledWith(jasmine.any(Function));
mockPromise.then.mostRecentCall.args[0]({hits: bigArray(100)}); mockPromise.then.mostRecentCall.args[0]({hits: bigArray(100)});