SearchPresenter extends BasePresenter<SearchContract.View> implements SearchContract.Presenter { @Override public void search(final String searchTerm) { getView().showLoading(); analytics.trackSearchBooks(searchTerm); addSubscription(bookService.searchBooks(searchTerm).subscribeOn(ioScheduler).observeOn(mainScheduler) .subscribe(new Subscriber<List<FireBookDetails>>() { @Override public void onCompleted() { } @Override public void onError(final Throwable e) { getView().hideLoading(); if (e instanceof IOException) { getView().showNoInternetMessage(); return; } analytics.trackSearchError(searchTerm, e.getMessage()); getView().showErrorMessage(e.getMessage()); getView().showRetryButton(); } @Override public void onNext(final List<FireBookDetails> fireBookDetails) { getView().hideLoading(); getView().hideRetryButton(); if (fireBookDetails == null || fireBookDetails.size() == 0) { analytics.trackSearchBooksNoResults(searchTerm); getView().showNoResultsMessage(); return; } analytics.trackSearchBooksSuccess(searchTerm, fireBookDetails.size()); getView().showSearchResults(fireBookDetails); } })); } SearchPresenter(final BookService bookService, final Analytics analytics, Scheduler mainScheduler,
                    Scheduler ioScheduler); @Override void search(final String searchTerm);  }
@Test public void search_ValidSearchTerm_ReturnsValidResults() { when(bookRepository.searchBooks(anyString())).thenReturn(Observable.just(getFakeBooks())); String searchTerm = "hug"; presenter.search(searchTerm); verify(analytics).trackSearchBooks(searchTerm); verify(view).showSearchResults(anyListOf(FireBookDetails.class)); verify(bookRepository).searchBooks(searchTerm); verify(analytics).trackSearchBooksSuccess(searchTerm, getFakeBooks().size()); verify(view, never()).showErrorMessage(anyString()); }
@Test public void search_NoInternet_ShowsNoInternetMsg() { when(bookRepository.searchBooks(anyString())) .thenReturn(Observable.<List<FireBookDetails>>error(new IOException("No Internet"))); String searchTerm = "hug"; presenter.search(searchTerm); verify(view).showLoading(); verify(analytics).trackSearchBooks(searchTerm); verify(view, never()).showSearchResults(anyListOf(FireBookDetails.class)); verify(bookRepository).searchBooks(searchTerm); verify(view).showNoInternetMessage(); verify(view).hideLoading(); }
@Test public void search_NoValidMatches_ShowsNoResultsMsg() { when(bookRepository.searchBooks(anyString())) .thenReturn(Observable.<List<FireBookDetails>>just(new ArrayList<FireBookDetails>())); String searchTerm = "hug"; presenter.search(searchTerm); verify(view).showLoading(); verify(view, never()).showSearchResults(anyListOf(FireBookDetails.class)); verify(bookRepository).searchBooks(searchTerm); verify(analytics).trackSearchBooks(searchTerm); verify(analytics).trackSearchBooksNoResults(searchTerm); verify(view).showNoResultsMessage(); verify(view).hideLoading(); }
@Test public void search_NoSearchTerm_ThrowsError() { final String errorMessage = "This is awkward."; when(bookRepository.searchBooks(anyString())) .thenReturn(Observable.<List<FireBookDetails>>error(new Exception(errorMessage))); String searchTerm = "hug"; presenter.search(searchTerm); verify(view).showLoading(); verify(view, never()).showSearchResults(anyListOf(FireBookDetails.class)); verify(bookRepository).searchBooks(searchTerm); verify(analytics).trackSearchBooks(searchTerm); verify(analytics).trackSearchError(searchTerm, errorMessage); verify(view).showErrorMessage(errorMessage); verify(view).hideLoading(); }