Skip to content

Commit

Permalink
✅ Add CoreMalariaProfileActivity tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-on committed May 24, 2021
1 parent e9bea94 commit e3d63f2
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.smartregister.chw.core.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

Expand All @@ -10,25 +15,36 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.chw.core.BaseUnitTest;
import org.smartregister.chw.core.R;
import org.smartregister.chw.core.activity.impl.CoreMalariaProfileActivityImpl;
import org.smartregister.chw.core.activity.impl.FamilyOtherMemberActivityPresenterImpl;
import org.smartregister.chw.core.presenter.CoreFamilyOtherMemberActivityPresenter;
import org.smartregister.chw.core.presenter.CoreMalariaMemberProfilePresenter;
import org.smartregister.chw.core.shadows.UtilsShadowUtil;
import org.smartregister.chw.core.utils.CoreConstants;
import org.smartregister.chw.malaria.domain.MemberObject;
import org.smartregister.chw.malaria.presenter.BaseMalariaProfilePresenter;
import org.smartregister.family.domain.FamilyMetadata;
import org.smartregister.family.util.Constants;
import org.smartregister.family.util.JsonFormUtils;

import timber.log.Timber;


@Config(shadows = {UtilsShadowUtil.class})
public class CoreMalariaProfileActivityTest extends BaseUnitTest {

@Rule
Expand Down Expand Up @@ -81,11 +97,82 @@ public void initNotificationReferralRecyclerViewInitsCorrectLayouts() {
public void initialisingPresenterRefreshesProfileBottomSection() {
activity = Mockito.spy(activity);
profilePresenter = Mockito.spy(Mockito.mock(CoreMalariaMemberProfilePresenter.class));
Mockito.when(activity.getProfilePresenter()).thenReturn((CoreMalariaMemberProfilePresenter)profilePresenter);
Mockito.when(activity.getProfilePresenter()).thenReturn((CoreMalariaMemberProfilePresenter) profilePresenter);
activity.initializePresenter();
Mockito.verify(profilePresenter, Mockito.times(1)).refreshProfileBottom();
}

/*@Test
public void changeCompletedActivityResultStartsFamilyProfileActivity() {
activity = Mockito.spy(activity);
int resultCode = Activity.RESULT_OK;
int requestCode = CoreConstants.ProfileActivityResults.CHANGE_COMPLETED;
Intent data = Mockito.mock(Intent.class);
activity.onActivityResult(requestCode, resultCode, data);
Mockito.verify(activity, Mockito.times(1)).startActivity(ArgumentMatchers.any(Intent.class));
Mockito.verify(activity, Mockito.times(1)).finish();
}*/

@Test
public void getJsonActivityResultUpdatesFamilyIfEncounterIsUpdate() {
activity = Mockito.spy(activity);

int resultCode = Activity.RESULT_OK;
int requestCode = JsonFormUtils.REQUEST_CODE_GET_JSON;

Intent data = Mockito.mock(Intent.class);
String json = "{\"encounter_type\": \"Update Family Member\"}";
Mockito.doReturn(json).when(data).getStringExtra(Constants.JSON_FORM_EXTRA.JSON);

FamilyMetadata metadata = Mockito.mock(FamilyMetadata.class, Mockito.CALLS_REAL_METHODS);
metadata.updateFamilyMemberRegister("family_member_register",
"tableName",
"Register Family Member",
"Update Family Member",
"config",
"familyRelationKey");

UtilsShadowUtil.setMetadata(metadata);

CoreFamilyOtherMemberActivityPresenter memberActivityPresenter = Mockito.spy(Mockito.mock(FamilyOtherMemberActivityPresenterImpl.class));
Mockito.when(activity.presenter()).thenReturn(memberActivityPresenter);

activity.onActivityResult(requestCode, resultCode, data);
Mockito.verify(memberActivityPresenter, Mockito.times(1)).
updateFamilyMember(
ArgumentMatchers.eq(activity),
ArgumentMatchers.eq(json),
ArgumentMatchers.eq(false));
}

@Test
public void menuIsInflatedOnCreateOptionsMenu() {
activity = Mockito.spy(activity);
MenuInflater inflater = Mockito.spy(Mockito.mock(MenuInflater.class));
Mockito.when(activity.getMenuInflater()).thenReturn(inflater);
Menu menu = Mockito.mock(Menu.class);
activity.onCreateOptionsMenu(menu);
Mockito.verify(inflater, Mockito.times(1)).inflate(R.menu.malaria_profile_menu, menu);
}


@Test
public void optionIsHandledOnOptionsItemSelected() {
activity = Mockito.spy(activity);
MenuItem menuItem = Mockito.mock(MenuItem.class);

Mockito.doNothing().when(activity).startFormForEdit(R.string.registration_info,
CoreConstants.JSON_FORM.FAMILY_MEMBER_REGISTER);

Mockito.doReturn(R.id.action_registration).when(menuItem).getItemId();
activity.onOptionsItemSelected(menuItem);
Mockito.verify(activity).startFormForEdit(R.string.registration_info,
CoreConstants.JSON_FORM.FAMILY_MEMBER_REGISTER);

}

@After
public void tearDown() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void removeMember() {
@NonNull
@Override
public CoreFamilyOtherMemberActivityPresenter presenter() {
return Mockito.mock(CoreFamilyOtherMemberActivityPresenter.class);
return Mockito.mock(FamilyOtherMemberActivityPresenterImpl.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.smartregister.chw.core.activity.impl;

import android.content.Context;

import org.smartregister.chw.core.contract.FamilyOtherMemberProfileExtendedContract;
import org.smartregister.chw.core.interactor.CoreFamilyProfileInteractor;
import org.smartregister.chw.core.presenter.CoreFamilyOtherMemberActivityPresenter;
import org.smartregister.family.contract.FamilyOtherMemberContract;
import org.smartregister.family.contract.FamilyProfileContract;

public class FamilyOtherMemberActivityPresenterImpl extends CoreFamilyOtherMemberActivityPresenter {
public FamilyOtherMemberActivityPresenterImpl(FamilyOtherMemberProfileExtendedContract.View view, FamilyOtherMemberContract.Model model, String viewConfigurationIdentifier, String familyBaseEntityId, String baseEntityId, String familyHead, String primaryCaregiver, String villageTown, String familyName) {
super(view, model, viewConfigurationIdentifier, familyBaseEntityId, baseEntityId, familyHead, primaryCaregiver, villageTown, familyName);
}

@Override
protected CoreFamilyProfileInteractor getFamilyProfileInteractor() {
return null;
}

@Override
protected FamilyProfileContract.Model getFamilyProfileModel(String familyName) {
return null;
}

@Override
protected void setProfileInteractor() {
// Do nothing
}

@Override
public void updateFamilyMember(Context context, String jsonString, boolean isIndependent) {
// Do nothing
}
}

0 comments on commit e3d63f2

Please sign in to comment.