ListBooksPresenter extends BasePresenter<ListBooksContract.View> implements ListBooksContract.Presenter { @Override public void loadBooksForLanguagePreference() { getView().showLoading(true); addSubscription(settingsRepository.getLanguagePreference().observeOn(ioScheduler).subscribeOn(mainScheduler) .subscribe(new Subscriber<FireLanguage>() { @Override public void onCompleted() { } @Override public void onError(final Throwable e) { getView().showLoading(false); getView().showErrorScreen(true, e.getMessage(), true); } @Override public void onNext(final FireLanguage fireLanguage) { analytics.setUserLanguage(fireLanguage.getLanguageName()); loadBooksForLanguage(fireLanguage); } })); } ListBooksPresenter(SettingsRepository settingsRepository, BookService bookService, Analytics analytics,
                       Scheduler ioScheduler, Scheduler mainScheduler); @Override void loadLanguages(); @Override void saveSelectedLanguage(final int indexOfLanguage); @Override void loadBooksForLanguagePreference(); @Override void clickOpenLanguagePopover(); @Override void openSearchScreen();  }
@Test public void loadBooksForLanguagePreference_LoadsBooks_ShowsBooks() { BOOKS.add(new FireBookDetails("Test title", "url", "cover_url", true, "description", ENGLISH_LANGUAGE, System.currentTimeMillis())); when(settingsRepository.getLanguagePreference()).thenReturn(Single.just(ENGLISH_LANGUAGE)); when(bookRepository.getBooksForLanguage(ENGLISH_LANGUAGE)).thenReturn(Observable.just(BOOKS)); listBooksPresenter.loadBooksForLanguagePreference(); verify(listBookView).showLoading(true); verify(listBookView).showLoading(false); verify(listBookView).showBooks(BOOKS); }
@Test public void loadBooksForLanguage_ThrowsError_ShowsError() { BOOKS.add(new FireBookDetails("Test title", "url", "cover_url", true, "description", ENGLISH_LANGUAGE, System.currentTimeMillis())); when(settingsRepository.getLanguagePreference()).thenReturn(Single.just(ENGLISH_LANGUAGE)); when(bookRepository.getBooksForLanguage(ENGLISH_LANGUAGE)) .thenReturn(Observable.<List<FireBookDetails>>error(new Exception("Eek!"))); listBooksPresenter.loadBooksForLanguagePreference(); verify(listBookView).showLoading(true); verify(listBookView).showLoading(false); verify(listBookView, never()).showBooks(anyList()); verify(listBookView).showErrorScreen(true, "Eek!", true); }
@Test public void loadBooksForLanguage_LanguageError_ShowsError() { when(settingsRepository.getLanguagePreference()).thenReturn(Single.<FireLanguage>error(new Exception("eek!"))); listBooksPresenter.loadBooksForLanguagePreference(); verify(listBookView).showLoading(true); verify(listBookView).showLoading(false); verify(listBookView, never()).showBooks(anyList()); verify(listBookView).showErrorScreen(true, "eek!", true); }