Skip to content

Commit

Permalink
Restored fetch soon test
Browse files Browse the repository at this point in the history
Use tap for loadingCount side effects
  • Loading branch information
Liza K committed Jan 12, 2020
1 parent f0ec6a9 commit 1e1c470
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ describe('fetchSoon', () => {
expect(callClient).toBeCalled();
});

test('should delay by 50ms if config is set to batch searches', () => {
const config = getConfigStub({
'courier:batchSearches': true,
});
const request = {};
const options = {};

fetchSoon(request, options, { config } as FetchHandlers);

expect(callClient).not.toBeCalled();
jest.advanceTimersByTime(0);
expect(callClient).not.toBeCalled();
jest.advanceTimersByTime(50);
expect(callClient).toBeCalled();
});

test('should send a batch of requests to callClient', () => {
const config = getConfigStub({
'courier:batchSearches': true,
Expand Down
18 changes: 7 additions & 11 deletions src/plugins/data/public/search/create_app_mount_context_search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { mergeMap } from 'rxjs/operators';
import { mergeMap, tap } from 'rxjs/operators';
import { from, BehaviorSubject } from 'rxjs';
import { ISearchAppMountContext } from './i_search_app_mount_context';
import { ISearchGeneric } from './i_search';
Expand Down Expand Up @@ -50,16 +50,12 @@ export const createAppMountSearchContext = (
return from(strategyPromise).pipe(
mergeMap(strategy => {
loadingCount$.next(loadingCount$.getValue() + 1);
const search$ = strategy.search(request, options);
search$.subscribe({
error: () => {
loadingCount$.next(loadingCount$.getValue() - 1);
},
complete: () => {
loadingCount$.next(loadingCount$.getValue() - 1);
},
});
return search$;
return strategy.search(request, options).pipe(
tap(
error => loadingCount$.next(loadingCount$.getValue() - 1),
complete => loadingCount$.next(loadingCount$.getValue() - 1)
)
);
})
);
};
Expand Down

0 comments on commit 1e1c470

Please sign in to comment.