diff --git a/platform/search/test/services/GenericSearchProviderSpec.js b/platform/search/test/services/GenericSearchProviderSpec.js index aeeef59908..e3ee0a97ba 100644 --- a/platform/search/test/services/GenericSearchProviderSpec.js +++ b/platform/search/test/services/GenericSearchProviderSpec.js @@ -45,6 +45,8 @@ define( mockTopic, mockMutationTopic, mockRoots = ['root1', 'root2'], + mockThrottledFn, + throttledCallCount, provider, mockProviderResults; @@ -58,6 +60,18 @@ define( } } + function resolveThrottledFn() { + if (mockThrottledFn.calls.length > throttledCallCount) { + mockThrottle.mostRecentCall.args[0](); + throttledCallCount = mockThrottledFn.calls.length; + } + } + + function resolveAsyncTasks() { + resolveThrottledFn(); + resolveObjectPromises(); + } + beforeEach(function () { mockQ = jasmine.createSpyObj( "$q", @@ -75,6 +89,8 @@ define( mockQ.defer.andReturn(mockDeferred); mockThrottle = jasmine.createSpy("throttle"); + mockThrottledFn = jasmine.createSpy("throttledFn"); + throttledCallCount = 0; mockObjectService = jasmine.createSpyObj( "objectService", @@ -112,7 +128,13 @@ define( mockDomainObjects[id] = ( jasmine.createSpyObj( "domainObject", - [ "getId", "getModel", "hasCapability", "getCapability", "useCapability" ] + [ + "getId", + "getModel", + "hasCapability", + "getCapability", + "useCapability" + ] ) ); mockDomainObjects[id].getId.andReturn(id); @@ -134,12 +156,9 @@ define( mockTopic.andCallFake(function (key) { return key === 'mutation' && mockMutationTopic; }); - mockThrottle.andCallFake(function (fn) { - return fn; - }); + mockThrottle.andReturn(mockThrottledFn); mockObjectPromise.then.andReturn(mockChainedPromise); - provider = new GenericSearchProvider( mockQ, mockLog, @@ -154,6 +173,8 @@ define( it("indexes tree on initialization", function () { var i; + resolveThrottledFn(); + expect(mockObjectService.getObjects).toHaveBeenCalled(); expect(mockObjectPromise.then).toHaveBeenCalled(); @@ -174,8 +195,8 @@ define( composition: ['a'] }); - resolveObjectPromises(); - resolveObjectPromises(); + resolveAsyncTasks(); + resolveAsyncTasks(); expect(mockWorker.postMessage).toHaveBeenCalledWith({ request: 'index', @@ -190,7 +211,7 @@ define( mockMutationTopic.listen.mostRecentCall .args[0](mockDomainObjects.a); - resolveObjectPromises(); + resolveAsyncTasks(); expect(mockWorker.postMessage).toHaveBeenCalledWith({ request: 'index', @@ -246,7 +267,7 @@ define( }); it("warns when objects are unavailable", function () { - resolveObjectPromises(); + resolveAsyncTasks(); expect(mockLog.warn).not.toHaveBeenCalled(); mockChainedPromise.then.mostRecentCall.args[0]( mockObjectPromise.then.mostRecentCall.args[1]() @@ -254,6 +275,19 @@ define( expect(mockLog.warn).toHaveBeenCalled(); }); + it("throttles the loading of objects to index", function () { + expect(mockObjectService.getObjects).not.toHaveBeenCalled(); + resolveThrottledFn(); + expect(mockObjectService.getObjects).toHaveBeenCalled(); + }); + + it("logs when all objects have been processed", function () { + expect(mockLog.info).not.toHaveBeenCalled(); + resolveAsyncTasks(); + resolveThrottledFn(); + expect(mockLog.info).toHaveBeenCalled(); + }); + }); } );