Search in sources :

Example 1 with MockApiClient

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

the class SearchViewModelTest method testFeaturedPopularRefTags.

@Test
public void testFeaturedPopularRefTags() {
    final TestScheduler scheduler = new TestScheduler();
    final List<Project> projects = Arrays.asList(ProjectFactory.allTheWayProject(), ProjectFactory.almostCompletedProject(), ProjectFactory.backedProject());
    final MockApiClient apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<DiscoverEnvelope> fetchProjects(@NonNull final DiscoveryParams params) {
            return Observable.just(DiscoverEnvelopeFactory.discoverEnvelope(projects));
        }
    };
    final Environment env = environment().toBuilder().scheduler(scheduler).apiClient(apiClient).build();
    setUpEnvironment(env);
    // populate search and overcome debounce
    this.vm.inputs.search("");
    scheduler.advanceTimeBy(300, TimeUnit.MILLISECONDS);
    this.vm.inputs.projectClicked(projects.get(0));
    this.goToRefTag.assertValues(RefTag.searchPopularFeatured());
    this.goToProject.assertValues(projects.get(0));
}
Also used : Project(com.kickstarter.models.Project) DiscoverEnvelope(com.kickstarter.services.apiresponses.DiscoverEnvelope) MockApiClient(com.kickstarter.mock.services.MockApiClient) NonNull(androidx.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestScheduler(rx.schedulers.TestScheduler) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Test(org.junit.Test)

Example 2 with MockApiClient

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

the class SearchViewModelTest method testProjectPage_whenFeatureFlagOn_shouldEmitProjectPage.

@Test
public void testProjectPage_whenFeatureFlagOn_shouldEmitProjectPage() {
    final MockCurrentUser user = new MockCurrentUser();
    final MockExperimentsClientType mockExperimentsClientType = new MockExperimentsClientType() {

        @Override
        public boolean isFeatureEnabled(@NotNull final OptimizelyFeature.Key feature) {
            return true;
        }
    };
    final TestScheduler scheduler = new TestScheduler();
    final List<Project> projects = Arrays.asList(ProjectFactory.allTheWayProject(), ProjectFactory.almostCompletedProject(), ProjectFactory.backedProject());
    final MockApiClient apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<DiscoverEnvelope> fetchProjects(@NonNull final DiscoveryParams params) {
            return Observable.just(DiscoverEnvelopeFactory.discoverEnvelope(projects));
        }
    };
    final Environment env = environment().toBuilder().currentUser(user).optimizely(mockExperimentsClientType).scheduler(scheduler).apiClient(apiClient).build();
    setUpEnvironment(env);
    // populate search and overcome debounce
    this.vm.inputs.search("");
    scheduler.advanceTimeBy(300, TimeUnit.MILLISECONDS);
    this.vm.inputs.projectClicked(projects.get(2));
    this.goToProject.assertValues(projects.get(2));
}
Also used : Project(com.kickstarter.models.Project) DiscoverEnvelope(com.kickstarter.services.apiresponses.DiscoverEnvelope) MockApiClient(com.kickstarter.mock.services.MockApiClient) NonNull(androidx.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestScheduler(rx.schedulers.TestScheduler) DiscoveryParams(com.kickstarter.services.DiscoveryParams) MockExperimentsClientType(com.kickstarter.mock.MockExperimentsClientType) NotNull(org.jetbrains.annotations.NotNull) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Test(org.junit.Test)

Example 3 with MockApiClient

use of com.kickstarter.mock.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.ViewModel vm = new SignupViewModel.ViewModel(environment);
    final TestSubscriber<Void> signupSuccessTest = new TestSubscriber<>();
    vm.outputs.signupSuccess().subscribe(signupSuccessTest);
    final TestSubscriber<String> signupErrorTest = new TestSubscriber<>();
    vm.outputs.errorString().subscribe(signupErrorTest);
    final TestSubscriber<Boolean> formSubmittingTest = new TestSubscriber<>();
    vm.outputs.formSubmitting().subscribe(formSubmittingTest);
    vm.inputs.name("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);
    this.segmentTrack.assertValues(EventName.PAGE_VIEWED.getEventName(), EventName.CTA_CLICKED.getEventName());
}
Also used : MockApiClient(com.kickstarter.mock.services.MockApiClient) ApiClientType(com.kickstarter.services.ApiClientType) AccessTokenEnvelope(com.kickstarter.services.apiresponses.AccessTokenEnvelope) NonNull(androidx.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Example 4 with MockApiClient

use of com.kickstarter.mock.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();
    setUpEnvironment(environment);
    final TestSubscriber<User> updateUserSettingsTest = new TestSubscriber<>();
    ((MockApiClient) environment.apiClient()).observable().filter(e -> "update_user_settings".equals(e.getFirst())).map(e -> (User) e.getSecond().get("user")).subscribe(updateUserSettingsTest);
    final Project project = ProjectFactory.project().toBuilder().category(CategoryFactory.tabletopGamesCategory()).build();
    this.vm.intent(new Intent().putExtra(IntentKey.PROJECT, project));
    this.vm.signupToGamesNewsletterClick();
    updateUserSettingsTest.assertValues(user.toBuilder().gamesNewsletter(true).build());
    this.showConfirmGamesNewsletterDialogTest.assertValueCount(0);
}
Also used : RefTag(com.kickstarter.libs.RefTag) Category(com.kickstarter.models.Category) Arrays(java.util.Arrays) NonNull(androidx.annotation.NonNull) ProjectFactory(com.kickstarter.mock.factories.ProjectFactory) MockApiClient(com.kickstarter.mock.services.MockApiClient) Pair(android.util.Pair) RewardFactory(com.kickstarter.mock.factories.RewardFactory) Intent(android.content.Intent) CategoryFactory(com.kickstarter.mock.factories.CategoryFactory) CurrentUserType(com.kickstarter.libs.CurrentUserType) OptimizelyFeature(com.kickstarter.libs.models.OptimizelyFeature) User(com.kickstarter.models.User) ThanksData(com.kickstarter.ui.adapters.data.ThanksData) TestSubscriber(rx.observers.TestSubscriber) ProjectDataFactory(com.kickstarter.mock.factories.ProjectDataFactory) PledgeData(com.kickstarter.ui.data.PledgeData) EventName(com.kickstarter.libs.utils.EventName) MockBooleanPreference(com.kickstarter.libs.preferences.MockBooleanPreference) LocationFactory(com.kickstarter.mock.factories.LocationFactory) UserFactory(com.kickstarter.mock.factories.UserFactory) MockExperimentsClientType(com.kickstarter.mock.MockExperimentsClientType) CheckoutData(com.kickstarter.ui.data.CheckoutData) CheckoutDataFactory(com.kickstarter.mock.factories.CheckoutDataFactory) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Project(com.kickstarter.models.Project) DiscoveryParams(com.kickstarter.services.DiscoveryParams) PledgeFlowContext(com.kickstarter.ui.data.PledgeFlowContext) Test(org.junit.Test) KSRobolectricTestCase(com.kickstarter.KSRobolectricTestCase) Environment(com.kickstarter.libs.Environment) IntentKey(com.kickstarter.ui.IntentKey) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) Project(com.kickstarter.models.Project) User(com.kickstarter.models.User) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) 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 5 with MockApiClient

use of com.kickstarter.mock.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.mock.services.MockApiClient) Request(okhttp3.Request) Intent(android.content.Intent) Update(com.kickstarter.models.Update) ApiClientType(com.kickstarter.services.ApiClientType) NonNull(androidx.annotation.NonNull) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) Test(org.junit.Test)

Aggregations

MockApiClient (com.kickstarter.mock.services.MockApiClient)66 Test (org.junit.Test)64 NonNull (androidx.annotation.NonNull)47 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)33 Environment (com.kickstarter.libs.Environment)29 Intent (android.content.Intent)28 Project (com.kickstarter.models.Project)25 ApiClientType (com.kickstarter.services.ApiClientType)19 User (com.kickstarter.models.User)14 CurrentUserType (com.kickstarter.libs.CurrentUserType)12 DiscoveryParams (com.kickstarter.services.DiscoveryParams)12 MessageThreadEnvelope (com.kickstarter.services.apiresponses.MessageThreadEnvelope)12 Backing (com.kickstarter.models.Backing)9 MessageThread (com.kickstarter.models.MessageThread)9 TestScheduler (rx.schedulers.TestScheduler)8 TestSubscriber (rx.observers.TestSubscriber)7 Update (com.kickstarter.models.Update)6 AccessTokenEnvelope (com.kickstarter.services.apiresponses.AccessTokenEnvelope)6 DiscoverEnvelope (com.kickstarter.services.apiresponses.DiscoverEnvelope)6 ProjectsEnvelope (com.kickstarter.services.apiresponses.ProjectsEnvelope)6