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

View File

@ -33,11 +33,15 @@ define(function () {
function SearchController($scope, searchService) {
// Starting amount of results to load. Will get increased.
var numResults = INITIAL_LOAD_NUMBER,
loading = false,
fullResults = [];
function search() {
var inputText = $scope.ngModel.input;
// We are starting to load.
loading = true;
// Update whether the file tree should be displayed
if (inputText === '' || inputText === undefined) {
$scope.ngModel.search = false;
@ -52,6 +56,9 @@ define(function () {
searchService.query(inputText).then(function (result) {
fullResults = result.hits;
$scope.results = result.hits.slice(0, numResults);
// Now we are done loading.
loading = false;
});
}
@ -67,7 +74,7 @@ define(function () {
* fully updated.
*/
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(
"searchService",
[ "query", "isLoading" ]
[ "query" ]
);
mockPromise = jasmine.createSpyObj(
"promise",
@ -78,10 +78,16 @@ define(
expect(mockScope.results).toBeDefined();
});
it("checks if the search service is loading", function () {
controller.isLoading();
expect(mockSearchService.isLoading).toHaveBeenCalled();
it("is loading until the service's promise fufills", function () {
// Send query
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 () {
expect(mockPromise.then).toHaveBeenCalledWith(jasmine.any(Function));