From a98b65286faa60752c7b67b055aa61199b8605f1 Mon Sep 17 00:00:00 2001 From: slhale Date: Tue, 18 Aug 2015 11:28:10 -0700 Subject: [PATCH] [Search] Update tests Updated the SearchController tests for more coverage, due to additions to the controller. Fixed a small logic error in controller.areMore() --- .../src/controllers/SearchController.js | 4 +- .../test/controllers/SearchControllerSpec.js | 63 ++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/platform/search/src/controllers/SearchController.js b/platform/search/src/controllers/SearchController.js index 44b8b03064..792492158e 100644 --- a/platform/search/src/controllers/SearchController.js +++ b/platform/search/src/controllers/SearchController.js @@ -141,14 +141,14 @@ define(function () { // Check to see if any of the not displayed results are of an allowed type for (i = numResults; i < fullResults.hits.length; i += 1) { - if ($scope.ngModel.checked[fullResults.hits[i].object.getModel().type.key]) { + if ($scope.ngModel.checkAll || $scope.ngModel.checked[fullResults.hits[i].object.getModel().type]) { return true; } } // If none of the ones at hand are correct, there still may be more if we // re-search with a larger maxResults - return numResults < fullResults.total; + return fullResults.hits.length < fullResults.total; }, /** diff --git a/platform/search/test/controllers/SearchControllerSpec.js b/platform/search/test/controllers/SearchControllerSpec.js index 6b435e4dfa..39416fab62 100644 --- a/platform/search/test/controllers/SearchControllerSpec.js +++ b/platform/search/test/controllers/SearchControllerSpec.js @@ -29,6 +29,10 @@ define( function (SearchController) { "use strict"; + // These should be the same as the ones on the top of the search controller + var INITIAL_LOAD_NUMBER = 20, + LOAD_INCREMENT = 20; + describe("The search controller", function () { var mockScope, mockSearchService, @@ -57,6 +61,7 @@ define( mockScope.ngModel.input = "test input"; mockScope.ngModel.checked = {}; mockScope.ngModel.checked['mock.type'] = true; + mockScope.ngModel.checkAll = true; mockSearchService = jasmine.createSpyObj( "searchService", @@ -115,11 +120,35 @@ define( expect(mockScope.results.length).toBeLessThan(100); }); + it("detects when there are more results", function () { + mockScope.ngModel.checkAll = false; + + expect(mockPromise.then).toHaveBeenCalledWith(jasmine.any(Function)); + mockPromise.then.mostRecentCall.args[0]({ + hits: bigArray(INITIAL_LOAD_NUMBER + 5), + total: INITIAL_LOAD_NUMBER + 5 + }); + // bigArray gives searchResults of type 'mock.type' + mockScope.ngModel.checked['mock.type'] = false; + mockScope.ngModel.checked['mock.type.2'] = true; + + expect(controller.areMore()).toBeFalsy(); + + mockScope.ngModel.checked['mock.type'] = true; + + expect(controller.areMore()).toBeTruthy(); + }); + it("can load more results", function () { var oldSize; expect(mockPromise.then).toHaveBeenCalled(); - mockPromise.then.mostRecentCall.args[0]({hits: bigArray(100), total: 1000}); + mockPromise.then.mostRecentCall.args[0]({ + hits: bigArray(INITIAL_LOAD_NUMBER + LOAD_INCREMENT + 1), + total: INITIAL_LOAD_NUMBER + LOAD_INCREMENT + 1 + }); + // These hits and total lengths are the case where the controller + // DOES NOT have to re-search to load more results oldSize = mockScope.results.length; expect(controller.areMore()).toBeTruthy(); @@ -128,6 +157,32 @@ define( expect(mockScope.results.length).toBeGreaterThan(oldSize); }); + it("can re-search to load more results", function () { + var oldSize, + oldCallCount; + + expect(mockPromise.then).toHaveBeenCalled(); + mockPromise.then.mostRecentCall.args[0]({ + hits: bigArray(INITIAL_LOAD_NUMBER + LOAD_INCREMENT - 1), + total: INITIAL_LOAD_NUMBER + LOAD_INCREMENT + 1 + }); + // These hits and total lengths are the case where the controller + // DOES have to re-search to load more results + oldSize = mockScope.results.length; + oldCallCount = mockPromise.then.callCount; + expect(controller.areMore()).toBeTruthy(); + + controller.loadMore(); + expect(mockPromise.then).toHaveBeenCalled(); + // Make sure that a NEW call to search has been made + expect(oldCallCount).toBeLessThan(mockPromise.then.callCount); + mockPromise.then.mostRecentCall.args[0]({ + hits: bigArray(INITIAL_LOAD_NUMBER + LOAD_INCREMENT + 1), + total: INITIAL_LOAD_NUMBER + LOAD_INCREMENT + 1 + }); + expect(mockScope.results.length).toBeGreaterThan(oldSize); + }); + it("sets the ngModel.search flag", function () { // Flag should be true with nonempty input expect(mockScope.ngModel.search).toEqual(true); @@ -137,6 +192,12 @@ define( controller.search(); mockPromise.then.mostRecentCall.args[0]({hits: [], total: 0}); expect(mockScope.ngModel.search).toEqual(false); + + // Both the empty string and undefined should be 'empty input' + mockScope.ngModel.input = undefined; + controller.search(); + mockPromise.then.mostRecentCall.args[0]({hits: [], total: 0}); + expect(mockScope.ngModel.search).toEqual(false); }); }); }