Search in sources :

Example 36 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class ProfileViewModelTest method testProfileViewModel_EmitsBackedAndCreatedProjectsData.

@Test
public void testProfileViewModel_EmitsBackedAndCreatedProjectsData() {
    final User user = UserFactory.user().toBuilder().backedProjectsCount(15).createdProjectsCount(2).build();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<User> fetchCurrentUser() {
            return Observable.just(user);
        }
    };
    final Environment env = environment().toBuilder().apiClient(apiClient).build();
    final ProfileViewModel.ViewModel vm = new ProfileViewModel.ViewModel(env);
    final TestSubscriber<Boolean> backedCountTextViewHidden = new TestSubscriber<>();
    vm.outputs.backedCountTextViewHidden().subscribe(backedCountTextViewHidden);
    final TestSubscriber<String> backedCountTextViewText = new TestSubscriber<>();
    vm.outputs.backedCountTextViewText().subscribe(backedCountTextViewText);
    final TestSubscriber<Boolean> backedTextViewHidden = new TestSubscriber<>();
    vm.outputs.backedTextViewHidden().subscribe(backedTextViewHidden);
    final TestSubscriber<Boolean> createdCountTextViewHidden = new TestSubscriber<>();
    vm.outputs.createdCountTextViewHidden().subscribe(createdCountTextViewHidden);
    final TestSubscriber<String> createdCountTextViewText = new TestSubscriber<>();
    vm.outputs.createdCountTextViewText().subscribe(createdCountTextViewText);
    final TestSubscriber<Boolean> createdTextViewHidden = new TestSubscriber<>();
    vm.outputs.createdTextViewHidden().subscribe(createdTextViewHidden);
    final TestSubscriber<Boolean> dividerViewHidden = new TestSubscriber<>();
    vm.outputs.dividerViewHidden().subscribe(dividerViewHidden);
    // Backed text views are displayed.
    backedCountTextViewHidden.assertValues(false);
    backedCountTextViewText.assertValues(NumberUtils.format(user.backedProjectsCount()));
    backedTextViewHidden.assertValues(false);
    // Created text views are displayed.
    createdCountTextViewHidden.assertValues(false);
    createdCountTextViewText.assertValues(NumberUtils.format(user.createdProjectsCount()));
    createdTextViewHidden.assertValues(false);
    // Divider view is displayed.
    dividerViewHidden.assertValues(false);
}
Also used : User(com.kickstarter.models.User) MockApiClient(com.kickstarter.services.MockApiClient) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Example 37 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class ProjectNotificationViewModelTest method testShowUnableToSaveNotificationError.

@Test
public void testShowUnableToSaveNotificationError() {
    final ApiClientType client = new MockApiClient() {

        @Override
        @NonNull
        public Observable<ProjectNotification> updateProjectNotifications(@NonNull final ProjectNotification projectNotification, final boolean checked) {
            return Observable.error(ApiExceptionFactory.badRequestException());
        }
    };
    final Environment environment = environment().toBuilder().apiClient(client).build();
    final ProjectNotificationViewModel vm = new ProjectNotificationViewModel(environment);
    final TestSubscriber<Void> showUnableToSaveNotificationErrorTest = new TestSubscriber<>();
    vm.errors.showUnableToSaveProjectNotificationError().subscribe(showUnableToSaveNotificationErrorTest);
    final TestSubscriber<Boolean> enabledSwitchTest = new TestSubscriber<>();
    vm.outputs.enabledSwitch().subscribe(enabledSwitchTest);
    // Start with a disabled notification.
    final ProjectNotification projectNotification = ProjectNotificationFactory.disabled();
    vm.inputs.projectNotification(projectNotification);
    // Switch should be disabled.
    enabledSwitchTest.assertValue(false);
    // Attempt to toggle the notification to true. This should error, and the switch should still be disabled.
    vm.enabledSwitchClick(true);
    showUnableToSaveNotificationErrorTest.assertValueCount(1);
    enabledSwitchTest.assertValue(false);
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) NonNull(android.support.annotation.NonNull) ProjectNotification(com.kickstarter.models.ProjectNotification) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Example 38 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class SearchViewModelTest method testSearchProjectsWhenEnterSearchTerm.

@Test
public void testSearchProjectsWhenEnterSearchTerm() {
    final TestScheduler scheduler = new TestScheduler();
    final Environment env = environment().toBuilder().scheduler(scheduler).build();
    final SearchViewModel viewModel = new SearchViewModel(env);
    final TestSubscriber<Boolean> searchProjectsPresent = new TestSubscriber<>();
    viewModel.outputs.searchProjects().map(ps -> ps.size() > 0).subscribe(searchProjectsPresent);
    final TestSubscriber<Boolean> popularProjectsPresent = new TestSubscriber<>();
    viewModel.outputs.popularProjects().map(ps -> ps.size() > 0).subscribe(popularProjectsPresent);
    // Popular projects emit immediately.
    popularProjectsPresent.assertValues(true);
    searchProjectsPresent.assertNoValues();
    koalaTest.assertValues("Discover Search");
    // Searching shouldn't emit values immediately
    viewModel.inputs.search("hello");
    searchProjectsPresent.assertNoValues();
    koalaTest.assertValues("Discover Search");
    // Waiting a small amount time shouldn't emit values
    scheduler.advanceTimeBy(200, TimeUnit.MILLISECONDS);
    searchProjectsPresent.assertNoValues();
    koalaTest.assertValues("Discover Search");
    // Waiting the rest of the time makes the search happen
    scheduler.advanceTimeBy(100, TimeUnit.MILLISECONDS);
    searchProjectsPresent.assertValues(false, true);
    koalaTest.assertValues("Discover Search", "Discover Search Results");
    // Typing more search terms doesn't emit more values
    viewModel.inputs.search("hello world!");
    searchProjectsPresent.assertValues(false, true);
    koalaTest.assertValues("Discover Search", "Discover Search Results");
    // Waiting enough time emits search results
    scheduler.advanceTimeBy(300, TimeUnit.MILLISECONDS);
    searchProjectsPresent.assertValues(false, true, false, true);
    koalaTest.assertValues("Discover Search", "Discover Search Results", "Discover Search Results");
    // Clearing search terms brings back popular projects.
    viewModel.inputs.search("");
    searchProjectsPresent.assertValues(false, true, false, true, false);
    popularProjectsPresent.assertValues(true, false, true);
    koalaTest.assertValues("Discover Search", "Discover Search Results", "Discover Search Results");
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) TestSubscriber(rx.observers.TestSubscriber) Environment(com.kickstarter.libs.Environment) Test(org.junit.Test) TestScheduler(rx.schedulers.TestScheduler) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 39 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class SettingsViewModelTest method testSettingsViewModel_sendPromoNewsletter.

@Test
public void testSettingsViewModel_sendPromoNewsletter() {
    final User user = UserFactory.user().toBuilder().promoNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder().currentUser(currentUser).build();
    final SettingsViewModel vm = new SettingsViewModel(environment);
    final TestSubscriber<User> currentUserTest = new TestSubscriber<>();
    currentUser.observable().subscribe(currentUserTest);
    final TestSubscriber<Newsletter> showOptInPromptTest = new TestSubscriber<>();
    vm.outputs.showOptInPrompt().subscribe(showOptInPromptTest);
    currentUserTest.assertValues(user);
    koalaTest.assertValues("Settings View");
    vm.inputs.sendPromoNewsletter(true);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe");
    currentUserTest.assertValues(user, user.toBuilder().promoNewsletter(true).build());
    vm.inputs.sendPromoNewsletter(false);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe", "Newsletter Unsubscribe");
    currentUserTest.assertValues(user, user.toBuilder().promoNewsletter(true).build(), user);
    showOptInPromptTest.assertNoValues();
}
Also used : Newsletter(com.kickstarter.ui.data.Newsletter) User(com.kickstarter.models.User) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) CurrentUserType(com.kickstarter.libs.CurrentUserType) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Test(org.junit.Test)

Example 40 with Environment

use of com.kickstarter.libs.Environment in project android-oss by kickstarter.

the class SettingsViewModelTest method testSettingsViewModel_sendHappeningNewsletter.

@Test
public void testSettingsViewModel_sendHappeningNewsletter() {
    final User user = UserFactory.user().toBuilder().happeningNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder().currentUser(currentUser).build();
    final SettingsViewModel vm = new SettingsViewModel(environment);
    final TestSubscriber<User> currentUserTest = new TestSubscriber<>();
    currentUser.observable().subscribe(currentUserTest);
    final TestSubscriber<Newsletter> showOptInPromptTest = new TestSubscriber<>();
    vm.outputs.showOptInPrompt().subscribe(showOptInPromptTest);
    currentUserTest.assertValues(user);
    koalaTest.assertValues("Settings View");
    vm.inputs.sendHappeningNewsletter(true);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe");
    currentUserTest.assertValues(user, user.toBuilder().happeningNewsletter(true).build());
    vm.inputs.sendHappeningNewsletter(false);
    koalaTest.assertValues("Settings View", "Newsletter Subscribe", "Newsletter Unsubscribe");
    currentUserTest.assertValues(user, user.toBuilder().happeningNewsletter(true).build(), user);
    showOptInPromptTest.assertNoValues();
}
Also used : Newsletter(com.kickstarter.ui.data.Newsletter) User(com.kickstarter.models.User) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) CurrentUserType(com.kickstarter.libs.CurrentUserType) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Test(org.junit.Test)

Aggregations

Environment (com.kickstarter.libs.Environment)50 Test (org.junit.Test)49 TestSubscriber (rx.observers.TestSubscriber)43 Intent (android.content.Intent)23 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)22 MockApiClient (com.kickstarter.services.MockApiClient)21 ApiClientType (com.kickstarter.services.ApiClientType)20 CurrentUserType (com.kickstarter.libs.CurrentUserType)19 Project (com.kickstarter.models.Project)19 User (com.kickstarter.models.User)16 NonNull (android.support.annotation.NonNull)14 KSRobolectricTestCase (com.kickstarter.KSRobolectricTestCase)9 MockBooleanPreference (com.kickstarter.libs.preferences.MockBooleanPreference)8 UserFactory (com.kickstarter.factories.UserFactory)7 AccessTokenEnvelope (com.kickstarter.services.apiresponses.AccessTokenEnvelope)7 ProjectFactory (com.kickstarter.factories.ProjectFactory)6 IntentKey (com.kickstarter.ui.IntentKey)6 List (java.util.List)6 KoalaEvent (com.kickstarter.libs.KoalaEvent)5 ConfigFactory (com.kickstarter.factories.ConfigFactory)4