DownloadsPresenter extends BasePresenter<DownloadsContract.View> implements DownloadsContract.Presenter { @Override public void deleteDownload(final FireBookDetails bookDetail) { getView().showLoading(true); addSubscription( downloadService.deleteDownload(bookDetail).subscribeOn(computationScheduler).observeOn(mainScheduler) .subscribe(new Subscriber<Boolean>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { analytics.trackDeleteBookFailed(bookDetail, e.getMessage()); getView().showLoading(false); getView().showSnackBarError(e.getMessage()); } @Override public void onNext(Boolean aBoolean) { analytics.trackDeleteBook(bookDetail); loadListDownloads(); } })); } DownloadsPresenter(BookService bookService, DownloadService downloadService, Analytics analytics, Scheduler io,
                       Scheduler main, Scheduler compScheduler); void loadListDownloads(); @Override void deleteDownload(final FireBookDetails bookDetail);  }
@Test public void deleteDownload_RemovesDownloadFromList() { downloadsPresenter.attachView(downloadsView); FireBookDetails sampleBook = new FireBookDetails("Test Book", "test_url", "cover_url_test", true, "description", language, System.currentTimeMillis()); FireBookDetails sampleBook2 = new FireBookDetails("Test Book2", "test_url2", "cover_url_test2", true, "description2", language, System.currentTimeMillis()); BOOKS.add(sampleBook2); when(downloadService.deleteDownload(sampleBook)).thenReturn(Observable.just(true)); when(bookService.getDownloadedBooks()).thenReturn(Observable.just(BOOKS)); downloadsPresenter.deleteDownload(sampleBook); verify(downloadsView, times(2)).showLoading(true); verify(downloadsView).showLoading(false); verify(downloadsView).showDownloadedBooks(BOOKS); verify(downloadService).deleteDownload(sampleBook); verify(analytics).trackDeleteBook(sampleBook); }
@Test public void deleteDownload_ThrowsError_NotifiesUserOfError() { downloadsPresenter.attachView(downloadsView); FireBookDetails sampleBook = new FireBookDetails("Test Book", "test_url", "cover_url_test", true, "description", language, System.currentTimeMillis()); String errorMessage = "Failed to delete"; when(downloadService.deleteDownload(sampleBook)) .thenReturn(Observable.<Boolean>error(new Exception(errorMessage))); downloadsPresenter.deleteDownload(sampleBook); verify(downloadsView).showLoading(true); verify(downloadsView).showLoading(false); verify(downloadsView, never()).showDownloadedBooks(BOOKS); verify(downloadService).deleteDownload(sampleBook); verify(downloadsView).showSnackBarError(errorMessage); verify(analytics).trackDeleteBookFailed(sampleBook, errorMessage); }