Search in sources :

Example 16 with MockApiClient

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

the class DiscoveryIntentMapperTest method emitsFromParamsExtra.

@Test
public void emitsFromParamsExtra() {
    final DiscoveryParams params = DiscoveryParams.builder().build();
    final Intent intent = new Intent().putExtra(IntentKey.DISCOVERY_PARAMS, params);
    final TestSubscriber<DiscoveryParams> resultTest = TestSubscriber.create();
    DiscoveryIntentMapper.params(intent, new MockApiClient()).subscribe(resultTest);
    resultTest.assertValues(params);
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) Intent(android.content.Intent) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Test(org.junit.Test)

Example 17 with MockApiClient

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

the class DiscoveryIntentMapperTest method emitsFromAdvancedCategoryIdAndLocationIdUri.

@Test
public void emitsFromAdvancedCategoryIdAndLocationIdUri() {
    final Uri uri = Uri.parse("https://www.kickstarter.com/discover/advanced?category_id=1&location_id=1");
    final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    final TestSubscriber<DiscoveryParams> resultTest = TestSubscriber.create();
    DiscoveryIntentMapper.params(intent, new MockApiClient()).subscribe(resultTest);
    resultTest.assertValueCount(1);
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) Intent(android.content.Intent) DiscoveryParams(com.kickstarter.services.DiscoveryParams) Uri(android.net.Uri) Test(org.junit.Test)

Example 18 with MockApiClient

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

the class ActivityFeedViewModelTest method testLoginFlow.

@Test
public void testLoginFlow() {
    final ApiClientType apiClient = new MockApiClient();
    final CurrentUserType currentUser = new MockCurrentUser();
    final Environment environment = environment().toBuilder().apiClient(apiClient).currentUser(currentUser).build();
    final ActivityFeedViewModel.ViewModel vm = new ActivityFeedViewModel.ViewModel(environment);
    final TestSubscriber<List<Activity>> activities = new TestSubscriber<>();
    vm.outputs.activities().subscribe(activities);
    final TestSubscriber<Boolean> loggedOutEmptyStateIsVisible = new TestSubscriber<>();
    vm.outputs.loggedOutEmptyStateIsVisible().subscribe(loggedOutEmptyStateIsVisible);
    final TestSubscriber<Boolean> loggedInEmptyStateIsVisible = new TestSubscriber<>();
    vm.outputs.loggedInEmptyStateIsVisible().subscribe(loggedInEmptyStateIsVisible);
    final TestSubscriber<Void> goToLogin = new TestSubscriber<>();
    vm.outputs.goToLogin().subscribe(goToLogin);
    // Empty activity feed with login button should be shown.
    loggedOutEmptyStateIsVisible.assertValue(true);
    // Login.
    vm.inputs.emptyActivityFeedLoginClicked(null);
    goToLogin.assertValueCount(1);
    currentUser.refresh(UserFactory.user());
    // Empty states are not shown when activities emit on successful login.
    activities.assertValueCount(1);
    loggedOutEmptyStateIsVisible.assertValues(true, false);
    loggedInEmptyStateIsVisible.assertValue(false);
}
Also used : MockApiClient(com.kickstarter.services.MockApiClient) ApiClientType(com.kickstarter.services.ApiClientType) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) CurrentUserType(com.kickstarter.libs.CurrentUserType) Test(org.junit.Test)

Example 19 with MockApiClient

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

the class CommentsViewModelTest method testCommentsViewModel_postCommentError.

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

        @Override
        @NonNull
        public Observable<Comment> postComment(@NonNull final Project project, @NonNull final String body) {
            return Observable.error(ApiExceptionFactory.badRequestException());
        }
    };
    final Environment env = environment().toBuilder().apiClient(apiClient).build();
    final CommentsViewModel vm = new CommentsViewModel(env);
    final TestSubscriber<String> showPostCommentErrorToast = new TestSubscriber<>();
    vm.outputs.showPostCommentErrorToast().subscribe(showPostCommentErrorToast);
    final TestSubscriber<Void> showCommentPostedToast = new TestSubscriber<>();
    vm.outputs.showCommentPostedToast().subscribe(showCommentPostedToast);
    // Start the view model with a project.
    vm.intent(new Intent().putExtra(IntentKey.PROJECT, ProjectFactory.backedProject()));
    // Click the comment button and write a comment.
    vm.inputs.commentButtonClicked();
    vm.inputs.commentBodyChanged("Mic check mic check.");
    // Post comment. Error should be shown. Comment posted toast should not be shown.
    vm.inputs.postCommentClicked();
    showPostCommentErrorToast.assertValueCount(1);
    showCommentPostedToast.assertNoValues();
}
Also used : Comment(com.kickstarter.models.Comment) MockApiClient(com.kickstarter.services.MockApiClient) Intent(android.content.Intent) 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 20 with MockApiClient

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

the class DiscoveryFragmentViewModelTest method testShowHeaderViews.

@Test
public void testShowHeaderViews() {
    final CurrentUserType currentUser = new MockCurrentUser();
    final Activity activity = ActivityFactory.activity();
    final ApiClientType apiClient = new MockApiClient() {

        @Override
        @NonNull
        public Observable<ActivityEnvelope> fetchActivities() {
            return Observable.just(ActivityEnvelopeFactory.activityEnvelope(Collections.singletonList(activity)));
        }
    };
    final MockIntPreference activitySamplePreference = new MockIntPreference(987654321);
    final Environment environment = environment().toBuilder().activitySamplePreference(activitySamplePreference).apiClient(apiClient).currentUser(currentUser).build();
    final DiscoveryFragmentViewModel vm = new DiscoveryFragmentViewModel(environment);
    final TestSubscriber<Activity> activityTest = new TestSubscriber<>();
    vm.outputs.activity().subscribe(activityTest);
    final TestSubscriber<Boolean> shouldShowOnboardingViewTest = new TestSubscriber<>();
    vm.outputs.shouldShowOnboardingView().subscribe(shouldShowOnboardingViewTest);
    // Initial home all projects params.
    vm.inputs.paramsFromActivity(DiscoveryParams.builder().sort(DiscoveryParams.Sort.HOME).build());
    vm.inputs.rootCategories(CategoryFactory.rootCategories());
    // Should show onboarding view.
    shouldShowOnboardingViewTest.assertValues(true);
    activityTest.assertValue(null);
    // Change params. Onboarding view should not be shown.
    vm.inputs.paramsFromActivity(DiscoveryParams.builder().build());
    shouldShowOnboardingViewTest.assertValues(true, false);
    activityTest.assertValues(null, null);
    // Login.
    currentUser.refresh(UserFactory.user());
    // Activity sampler should be shown rather than onboarding view.
    shouldShowOnboardingViewTest.assertValues(true, false, false);
    activityTest.assertValues(null, null, activity);
    // Change params. Activity sampler should not be shown.
    vm.inputs.paramsFromActivity(DiscoveryParams.builder().build());
    activityTest.assertValues(null, null, activity, null);
}
Also used : MockIntPreference(com.kickstarter.libs.preferences.MockIntPreference) MockApiClient(com.kickstarter.services.MockApiClient) Activity(com.kickstarter.models.Activity) ApiClientType(com.kickstarter.services.ApiClientType) MockCurrentUser(com.kickstarter.libs.MockCurrentUser) ActivityEnvelope(com.kickstarter.services.apiresponses.ActivityEnvelope) Environment(com.kickstarter.libs.Environment) TestSubscriber(rx.observers.TestSubscriber) CurrentUserType(com.kickstarter.libs.CurrentUserType) 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