Skip to content

Commit

Permalink
add tests (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
bennsimon authored Jun 17, 2021
1 parent 08f022d commit 8385794
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,8 @@ private HeightWrapper getHeightWrapper(Height lastUnsyncedHeight, String childNa
return heightWrapper;
}

private void updateRecordGrowthMonitoringViews(WeightWrapper weightWrapper, HeightWrapper heightWrapper, final boolean isActive) {
@VisibleForTesting
protected void updateRecordGrowthMonitoringViews(WeightWrapper weightWrapper, HeightWrapper heightWrapper, final boolean isActive) {

recordWeightText.setText(R.string.record_growth);
recordWeightText.setTextColor(!isActive ? getResources().getColor(R.color.inactive_text_color) : getResources().getColor(R.color.text_black));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.apache.commons.lang3.tuple.Triple;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -24,6 +27,10 @@
import org.smartregister.commonregistry.CommonPersonObjectClient;
import org.smartregister.domain.Photo;
import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
import org.smartregister.growthmonitoring.domain.Height;
import org.smartregister.growthmonitoring.domain.HeightWrapper;
import org.smartregister.growthmonitoring.domain.Weight;
import org.smartregister.growthmonitoring.domain.WeightWrapper;
import org.smartregister.immunization.ImmunizationLibrary;
import org.smartregister.immunization.repository.VaccineRepository;
import org.smartregister.receiver.SyncStatusBroadcastReceiver;
Expand All @@ -33,7 +40,12 @@
import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -94,6 +106,7 @@ public void testUpdateViewsShouldInvokeUpdateViewTask() throws InterruptedExcept
Thread.sleep(ASYNC_TIMEOUT);
verify(immunizationActivity).startUpdateViewTask();
}

@Test
public void testSetUpFloatingActionButtonShouldShowButtonIfNfcFeatureEnabled() throws Exception {
LinearLayout floatingActionButton = spy(immunizationActivity.findViewById(R.id.fab));
Expand All @@ -109,6 +122,37 @@ public void testSetUpFloatingActionButtonShouldShowButtonIfNfcFeatureEnabled() t
verify(floatingActionButton).setVisibility(eq(View.VISIBLE));
}

@Test
public void testUpdateGrowthViewsShouldUpdateHeightAndWeightViews() throws Exception {
Weight lastUnsyncedWeight = new Weight();
lastUnsyncedWeight.setId(2L);
lastUnsyncedWeight.setKg(5f);
lastUnsyncedWeight.setUpdatedAt(DateTime.now().minusDays(1).getMillis());

Height lastUnsyncedHeight = new Height();
lastUnsyncedHeight.setId(3L);
lastUnsyncedHeight.setCm(20f);
lastUnsyncedHeight.setUpdatedAt(DateTime.now().minusDays(1).getMillis());

View spyRecordGrowth = immunizationActivity.findViewById(R.id.record_growth);
TextView spyRecordWeightText = immunizationActivity.findViewById(R.id.record_growth_text);
ImageButton spyGrowthChartButton = immunizationActivity.findViewById(R.id.growth_chart_button);
ReflectionHelpers.setField(immunizationActivity, "recordGrowth", spyRecordGrowth);
ReflectionHelpers.setField(immunizationActivity, "recordWeightText", spyRecordWeightText);
ReflectionHelpers.setField(immunizationActivity, "growthChartButton", spyGrowthChartButton);

ReflectionHelpers.setField(immunizationActivity, "monitorGrowth", true);

WhiteboxImpl.invokeMethod(immunizationActivity, "updateGrowthViews", lastUnsyncedWeight, lastUnsyncedHeight, true);

verify(immunizationActivity).updateRecordGrowthMonitoringViews(any(WeightWrapper.class), nullable(HeightWrapper.class), eq(true));

assertTrue(spyGrowthChartButton.hasOnClickListeners());
assertEquals("5.0 kg, 20.0 cm", spyRecordWeightText.getText().toString());
assertNotNull(spyRecordGrowth.getTag(R.id.weight_wrapper));
assertNotNull(spyRecordGrowth.getTag(R.id.height_wrapper));
}

@After
public void tearDown() {
ReflectionHelpers.setStaticField(SyncStatusBroadcastReceiver.class, "singleton", null);
Expand Down Expand Up @@ -187,6 +231,9 @@ protected CommonPersonObjectClient getChildDetails(String caseId) {
Map<String, String> clientDetails = new LinkedHashMap<>();
clientDetails.put(Constants.KEY.FIRST_NAME, "John");
clientDetails.put(Constants.KEY.LAST_NAME, "Doe");
clientDetails.put(Constants.KEY.ZEIR_ID, "2045");
clientDetails.put(Constants.KEY.MOTHER_FIRST_NAME, "Jane");
clientDetails.put(Constants.KEY.MOTHER_LAST_NAME, "Doe");
clientDetails.put(Constants.KEY.GENDER, Constants.GENDER.MALE);
clientDetails.put(Constants.KEY.DOB, "2021-01-09");
CommonPersonObjectClient client = new CommonPersonObjectClient("23weq", clientDetails, "John Doe");
Expand Down

0 comments on commit 8385794

Please sign in to comment.