Search in sources :

Example 16 with ApiClientType

use of com.kickstarter.services.ApiClientType in project android-oss by kickstarter.

the class LoginViewModelTest method testLoginTfaChallenge.

@Test
public void testLoginTfaChallenge() {
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<AccessTokenEnvelope> login(@NonNull final String email, @NonNull final String password) {
            return Observable.error(ApiExceptionFactory.tfaRequired());
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final LoginViewModel vm = new LoginViewModel(environment);
    final TestSubscriber<Void> tfaChallenge = new TestSubscriber<>();
    vm.errors.tfaChallenge().subscribe(tfaChallenge);
    final TestSubscriber<Void> loginSuccess = new TestSubscriber<>();
    vm.outputs.loginSuccess().subscribe(loginSuccess);
    vm.inputs.email("hello@kickstarter.com");
    vm.inputs.password("danisawesome");
    vm.inputs.loginClick();
    loginSuccess.assertNoValues();
    tfaChallenge.assertValueCount(1);
}
Also used : AccessTokenEnvelope(com.kickstarter.services.apiresponses.AccessTokenEnvelope) MockApiClient(com.kickstarter.services.MockApiClient) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) ApiClientType(com.kickstarter.services.ApiClientType) Test(org.junit.Test)

Example 17 with ApiClientType

use of com.kickstarter.services.ApiClientType in project android-oss by kickstarter.

the class ProfileViewModelTest method testProfileViewModel_EmitsUserNameAndAvatar.

@Test
public void testProfileViewModel_EmitsUserNameAndAvatar() {
    final User user = UserFactory.user();
    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<String> avatarImageViewUrl = new TestSubscriber<>();
    vm.outputs.avatarImageViewUrl().subscribe(avatarImageViewUrl);
    final TestSubscriber<String> userNameTextViewText = new TestSubscriber<>();
    vm.outputs.userNameTextViewText().subscribe(userNameTextViewText);
    avatarImageViewUrl.assertValues(user.avatar().medium());
    userNameTextViewText.assertValues(user.name());
}
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 18 with ApiClientType

use of com.kickstarter.services.ApiClientType in project android-oss by kickstarter.

the class ProfileViewModelTest method testProfileViewModel_EmitsCreatedProjectsData.

@Test
public void testProfileViewModel_EmitsCreatedProjectsData() {
    final User user = UserFactory.user().toBuilder().backedProjectsCount(0).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 hidden.
    backedCountTextViewHidden.assertValues(true);
    backedCountTextViewText.assertNoValues();
    backedTextViewHidden.assertValues(true);
    // Created text views are displayed.
    createdCountTextViewHidden.assertValues(false);
    createdCountTextViewText.assertValues(NumberUtils.format(user.createdProjectsCount()));
    createdTextViewHidden.assertValues(false);
    // Divider view is hidden.
    dividerViewHidden.assertValues(true);
}
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 19 with ApiClientType

use of com.kickstarter.services.ApiClientType 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 20 with ApiClientType

use of com.kickstarter.services.ApiClientType 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)

Aggregations

ApiClientType (com.kickstarter.services.ApiClientType)31 Test (org.junit.Test)31 Environment (com.kickstarter.libs.Environment)25 MockApiClient (com.kickstarter.mock.services.MockApiClient)19 TestSubscriber (rx.observers.TestSubscriber)18 Intent (android.content.Intent)16 NonNull (androidx.annotation.NonNull)12 MockApiClient (com.kickstarter.services.MockApiClient)12 User (com.kickstarter.models.User)9 NonNull (android.support.annotation.NonNull)8 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)8 AccessTokenEnvelope (com.kickstarter.services.apiresponses.AccessTokenEnvelope)8 Update (com.kickstarter.models.Update)5 CurrentUserType (com.kickstarter.libs.CurrentUserType)4 Project (com.kickstarter.models.Project)3 Nullable (androidx.annotation.Nullable)2 ProjectNotification (com.kickstarter.models.ProjectNotification)2 MessageThreadsEnvelope (com.kickstarter.services.apiresponses.MessageThreadsEnvelope)2 Mailbox (com.kickstarter.ui.data.Mailbox)2 TestScheduler (rx.schedulers.TestScheduler)2