Search in sources :

Example 1 with MockApiClient

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

the class ThanksViewModelTest method testThanksViewModel_signupToGamesNewsletterOnClick.

@Test
public void testThanksViewModel_signupToGamesNewsletterOnClick() {
    final User user = UserFactory.user().toBuilder().gamesNewsletter(false).build();
    final CurrentUserType currentUser = new MockCurrentUser(user);
    final Environment environment = environment().toBuilder().currentUser(currentUser).build();
    final ThanksViewModel vm = new ThanksViewModel(environment);
    final TestSubscriber<User> updateUserSettingsTest = new TestSubscriber<>();
    ((MockApiClient) environment.apiClient()).observable().filter(e -> "update_user_settings".equals(e.first)).map(e -> (User) e.second.get("user")).subscribe(updateUserSettingsTest);
    final TestSubscriber<Void> showConfirmGamesNewsletterDialogTest = TestSubscriber.create();
    vm.outputs.showConfirmGamesNewsletterDialog().subscribe(showConfirmGamesNewsletterDialogTest);
    final Project project = ProjectFactory.project().toBuilder().category(CategoryFactory.tabletopGamesCategory()).build();
    vm.intent(new Intent().putExtra(IntentKey.PROJECT, project));
    vm.signupToGamesNewsletterClick();
    updateUserSettingsTest.assertValues(user.toBuilder().gamesNewsletter(true).build());
    showConfirmGamesNewsletterDialogTest.assertValueCount(0);
    koalaTest.assertValues("Newsletter Subscribe");
}
Also used : Category(com.kickstarter.models.Category) Arrays(java.util.Arrays) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) CategoryFactory(com.kickstarter.factories.CategoryFactory) Project(com.kickstarter.models.Project) LocationFactory(com.kickstarter.factories.LocationFactory) Pair(android.util.Pair) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Intent(android.content.Intent) Test(org.junit.Test) CurrentUserType(com.kickstarter.libs.CurrentUserType) UserFactory(com.kickstarter.factories.UserFactory) User(com.kickstarter.models.User) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) List(java.util.List) TestSubscriber(rx.observers.TestSubscriber) Environment(com.kickstarter.libs.Environment) MockBooleanPreference(com.kickstarter.libs.preferences.MockBooleanPreference) ProjectFactory(com.kickstarter.factories.ProjectFactory) IntentKey(com.kickstarter.ui.IntentKey) MockApiClient(com.kickstarter.services.MockApiClient) Project(com.kickstarter.models.Project) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) User(com.kickstarter.models.User) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Intent(android.content.Intent) CurrentUserType(com.kickstarter.libs.CurrentUserType) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Test(org.junit.Test)

Example 2 with MockApiClient

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

the class TwoFactorViewModelTest method testTwoFactorViewModel_CodeMismatchError.

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

        @Override
        @NonNull
        public Observable<AccessTokenEnvelope> login(@NonNull final String email, @NonNull final String password, @NonNull final String code) {
            return Observable.error(ApiExceptionFactory.tfaFailed());
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final Intent intent = new Intent();
    intent.putExtra(IntentKey.EMAIL, "gina@kickstarter.com");
    intent.putExtra(IntentKey.PASSWORD, "hello");
    intent.putExtra(IntentKey.FACEBOOK_LOGIN, false);
    intent.putExtra(IntentKey.FACEBOOK_TOKEN, "");
    final TwoFactorViewModel vm = new TwoFactorViewModel(environment);
    vm.intent(intent);
    final TestSubscriber<Void> tfaSuccess = new TestSubscriber<>();
    vm.outputs.tfaSuccess().subscribe(tfaSuccess);
    final TestSubscriber<Boolean> formSubmitting = new TestSubscriber<>();
    vm.outputs.formSubmitting().subscribe(formSubmitting);
    final TestSubscriber<Void> tfaCodeMismatchError = new TestSubscriber<>();
    vm.errors.tfaCodeMismatchError().subscribe(tfaCodeMismatchError);
    vm.inputs.code("88888");
    vm.inputs.loginClick();
    formSubmitting.assertValues(true, false);
    tfaSuccess.assertNoValues();
    tfaCodeMismatchError.assertValueCount(1);
    koalaTest.assertValues("Two-factor Authentication Confirm View", "Errored User Login");
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) Intent(android.content.Intent) ApiClientType(com.kickstarter.services.ApiClientType) AccessTokenEnvelope(com.kickstarter.services.apiresponses.AccessTokenEnvelope) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 3 with MockApiClient

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

the class UpdateViewModelTest method testUpdateViewModel_UpdateSequence.

@Test
public void testUpdateViewModel_UpdateSequence() {
    final Update initialUpdate = UpdateFactory.update().toBuilder().sequence(1).build();
    final Update anotherUpdate = UpdateFactory.update().toBuilder().sequence(2).build();
    final Request anotherUpdateRequest = new Request.Builder().url("https://kck.str/projects/param/param/posts/id").build();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<Update> fetchUpdate(@NonNull final String projectParam, @NonNull final String updateParam) {
            return Observable.just(anotherUpdate);
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final UpdateViewModel.ViewModel vm = new UpdateViewModel.ViewModel(environment);
    final TestSubscriber<String> updateSequence = new TestSubscriber<>();
    vm.outputs.updateSequence().subscribe(updateSequence);
    // Start the intent with a project and update.
    vm.intent(new Intent().putExtra(IntentKey.PROJECT, ProjectFactory.project()).putExtra(IntentKey.UPDATE, initialUpdate));
    // Initial update's sequence number emits.
    updateSequence.assertValues(NumberUtils.format(initialUpdate.sequence()));
    vm.inputs.goToUpdateRequest(anotherUpdateRequest);
    // New sequence should emit for new update page.
    updateSequence.assertValues(NumberUtils.format(initialUpdate.sequence()), NumberUtils.format(anotherUpdate.sequence()));
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) Request(okhttp3.Request) Intent(android.content.Intent) Update(com.kickstarter.models.Update) ApiClientType(com.kickstarter.services.ApiClientType) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 4 with MockApiClient

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

the class UpdateViewModelTest method testUpdateViewModel_StartProjectActivity.

@Test
public void testUpdateViewModel_StartProjectActivity() {
    final Update update = UpdateFactory.update().toBuilder().projectId(1234).build();
    final Project project = ProjectFactory.project().toBuilder().id(update.projectId()).build();
    final Request projectRequest = new Request.Builder().url("https://kck.str/projects/param/param").build();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<Project> fetchProject(@NonNull final String param) {
            return Observable.just(project);
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final UpdateViewModel.ViewModel vm = new UpdateViewModel.ViewModel(environment);
    final TestSubscriber<Project> startProjectActivity = new TestSubscriber<>();
    vm.outputs.startProjectActivity().map(pr -> pr.first).subscribe(startProjectActivity);
    // Start the intent with a project and update.
    vm.intent(new Intent().putExtra(IntentKey.PROJECT, project).putExtra(IntentKey.UPDATE, update));
    vm.inputs.goToProjectRequest(projectRequest);
    startProjectActivity.assertValues(project);
}
Also used : Request(okhttp3.Request) Project(com.kickstarter.models.Project) UpdateFactory(com.kickstarter.factories.UpdateFactory) Intent(android.content.Intent) Test(org.junit.Test) NonNull(android.support.annotation.NonNull) KoalaEvent(com.kickstarter.libs.KoalaEvent) Observable(rx.Observable) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) TestSubscriber(rx.observers.TestSubscriber) Update(com.kickstarter.models.Update) ApiClientType(com.kickstarter.services.ApiClientType) Environment(com.kickstarter.libs.Environment) NumberUtils(com.kickstarter.libs.utils.NumberUtils) ProjectFactory(com.kickstarter.factories.ProjectFactory) IntentKey(com.kickstarter.ui.IntentKey) MockApiClient(com.kickstarter.services.MockApiClient) MockApiClient(com.kickstarter.services.MockApiClient) Request(okhttp3.Request) Intent(android.content.Intent) Update(com.kickstarter.models.Update) ApiClientType(com.kickstarter.services.ApiClientType) Project(com.kickstarter.models.Project) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 5 with MockApiClient

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

the class SignupViewModelTest method testSignupViewModel_ApiValidationError.

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

        @Override
        @NonNull
        public Observable<AccessTokenEnvelope> signup(@NonNull final String name, @NonNull final String email, @NonNull final String password, @NonNull final String passwordConfirmation, final boolean sendNewsletters) {
            return Observable.error(ApiExceptionFactory.apiError(ErrorEnvelope.builder().httpCode(422).build()));
        }
    };
    final Environment environment = environment().toBuilder().apiClient(apiClient).build();
    final SignupViewModel vm = new SignupViewModel(environment);
    final TestSubscriber<Void> signupSuccessTest = new TestSubscriber<>();
    vm.outputs.signupSuccess().subscribe(signupSuccessTest);
    final TestSubscriber<String> signupErrorTest = new TestSubscriber<>();
    vm.errors.signupError().subscribe(signupErrorTest);
    final TestSubscriber<Boolean> formSubmittingTest = new TestSubscriber<>();
    vm.outputs.formSubmitting().subscribe(formSubmittingTest);
    vm.inputs.fullName("brandon");
    vm.inputs.email("hello@kickstarter.com");
    vm.inputs.email("incorrect@kickstarter");
    vm.inputs.password("danisawesome");
    vm.inputs.sendNewslettersClick(true);
    vm.inputs.signupClick();
    formSubmittingTest.assertValues(true, false);
    signupSuccessTest.assertValueCount(0);
    signupErrorTest.assertValueCount(1);
    koalaTest.assertValues("User Signup", "Signup Newsletter Toggle", "Errored User Signup");
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) ApiClientType(com.kickstarter.services.ApiClientType) AccessTokenEnvelope(com.kickstarter.services.apiresponses.AccessTokenEnvelope) NonNull(android.support.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Aggregations

MockApiClient (com.kickstarter.services.MockApiClient)31 Test (org.junit.Test)30 Environment (com.kickstarter.libs.Environment)21 TestSubscriber (rx.observers.TestSubscriber)21 ApiClientType (com.kickstarter.services.ApiClientType)20 Intent (android.content.Intent)16 NonNull (android.support.annotation.NonNull)14 Project (com.kickstarter.models.Project)7 DiscoveryParams (com.kickstarter.services.DiscoveryParams)7 AccessTokenEnvelope (com.kickstarter.services.apiresponses.AccessTokenEnvelope)7 Uri (android.net.Uri)6 User (com.kickstarter.models.User)6 CurrentUserType (com.kickstarter.libs.CurrentUserType)3 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)3 Update (com.kickstarter.models.Update)3 List (java.util.List)3 KSRobolectricTestCase (com.kickstarter.KSRobolectricTestCase)2 ProjectFactory (com.kickstarter.factories.ProjectFactory)2 IntentKey (com.kickstarter.ui.IntentKey)2 Request (okhttp3.Request)2