From 7297bc52b6d25bb798767a85557355c8f0f29acc Mon Sep 17 00:00:00 2001
From: Hamza Ahmed Khan <hamza.khan@venturedive.com>
Date: Wed, 17 May 2023 15:12:15 +0500
Subject: [PATCH] Increase code coverage

---
 .../HeightMonitoringFragmentTest.java         | 28 +++++++++++++++++
 ...WeightForHeightMonitoringFragmentTest.java |  4 ++-
 .../WeightMonitoringFragmentTest.java         | 31 ++++++++++++++++++-
 3 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java
index 8b73ffe..2287b4f 100644
--- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java
+++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java
@@ -4,6 +4,7 @@
 import androidx.fragment.app.FragmentActivity;
 import android.view.View;
 
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -11,10 +12,16 @@
 import org.mockito.MockitoAnnotations;
 import org.opensrp.api.constants.Gender;
 import org.robolectric.Robolectric;
+import org.robolectric.util.ReflectionHelpers;
 import org.smartregister.growthmonitoring.BaseUnitTest;
+import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
 import org.smartregister.growthmonitoring.R;
+import org.smartregister.growthmonitoring.domain.HeightZScore;
+import org.smartregister.growthmonitoring.repository.HeightZScoreRepository;
 import org.smartregister.view.customcontrols.CustomFontTextView;
 
+import java.util.ArrayList;
+
 /**
  * Created by ndegwamartin on 2020-04-15.
  */
@@ -34,7 +41,16 @@ public void setUp() {
 
     @Test
     public void testThatHeightForAgeViewIsCreated() throws InterruptedException {
+        ArrayList<HeightZScore> heightZScores = new ArrayList<>();
+        heightZScores.add(getHeightZScore(56));
+        heightZScores.add(getHeightZScore(44));
+//
+        HeightZScoreRepository heightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().heightZScoreRepository());
+        Mockito.doReturn(heightZScores).when(heightZScoreRepository).findByGender(Mockito.any());
+        ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", heightZScoreRepository);
+
         HeightMonitoringFragment fragment = Mockito.spy(HeightMonitoringFragment.createInstance("2018-09-12", Gender.FEMALE, getHeights()));
+        Mockito.doReturn(true).when(fragment).isVisible();
         activity.getSupportFragmentManager().beginTransaction().add(fragment, "Height-for-Age-Boys").commitNow();
         View view = fragment.getView();
         Assert.assertNotNull(view);
@@ -46,4 +62,16 @@ public void testThatHeightForAgeViewIsCreated() throws InterruptedException {
         Assert.assertEquals(constraintLayout.getContext().getString(R.string.height), ((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText());
         Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText());
     }
+
+    private HeightZScore getHeightZScore(int age) {
+        HeightZScore heightZScore = new HeightZScore();
+        heightZScore.setGender(Gender.MALE);
+        heightZScore.setMonth(age);
+        return heightZScore;
+    }
+
+    @After
+    public void destroy(){
+        ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", null);
+    }
 }
\ No newline at end of file
diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java
index f4c4fb3..94e9f6a 100644
--- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java
+++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java
@@ -8,6 +8,7 @@
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.opensrp.api.constants.Gender;
 import org.robolectric.Robolectric;
@@ -31,7 +32,8 @@ public void setUp() {
 
     @Test
     public void testThatWeightForHeightViewIsCreated() throws InterruptedException {
-        WeightForHeightMonitoringFragment fragment = WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights());
+        WeightForHeightMonitoringFragment fragment = Mockito.spy(WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights()));
+        Mockito.doReturn(true).when(fragment).isVisible();
         activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Height-Boys").commitNow();
         View view = fragment.getView();
         Assert.assertNotNull(view);
diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java
index 89d063c..21a1055 100644
--- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java
+++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java
@@ -5,6 +5,7 @@
 
 import android.view.View;
 
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -12,10 +13,17 @@
 import org.mockito.MockitoAnnotations;
 import org.opensrp.api.constants.Gender;
 import org.robolectric.Robolectric;
+import org.robolectric.util.ReflectionHelpers;
 import org.smartregister.growthmonitoring.BaseUnitTest;
+import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
 import org.smartregister.growthmonitoring.R;
+import org.smartregister.growthmonitoring.domain.WeightZScore;
+import org.smartregister.growthmonitoring.repository.WeightZScoreRepository;
 import org.smartregister.view.customcontrols.CustomFontTextView;
 
+import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
+
 /**
  * Created by ndegwamartin on 2020-04-15.
  */
@@ -35,17 +43,38 @@ public void setUp() {
 
     @Test
     public void testThatWeightForAgeViewIsCreated() throws InterruptedException {
+        ArrayList<WeightZScore> weightZScores = new ArrayList<>();
+        weightZScores.add(getWeightZScore(56));
+        weightZScores.add(getWeightZScore(44));
+//
+        WeightZScoreRepository  weightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().weightZScoreRepository());
+        Mockito.doReturn(weightZScores).when(weightZScoreRepository).findByGender(Mockito.any());
+        ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", weightZScoreRepository);
+
         WeightMonitoringFragment fragment = Mockito.spy(WeightMonitoringFragment.createInstance("2018-09-12", Gender.MALE, getWeights()));
+        Mockito.doReturn(true).when(fragment).isVisible();
         activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Age-Boys").commitNow();
         View view = fragment.getView();
         Assert.assertNotNull(view);
         Assert.assertTrue(view instanceof ConstraintLayout);
         ConstraintLayout constraintLayout = (ConstraintLayout) view;
-        Thread.sleep(3000);
+        TimeUnit.SECONDS.toMillis(20);
         Assert.assertEquals(3, constraintLayout.getChildCount());
         Assert.assertEquals(constraintLayout.getContext().getString(R.string.age), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_one_metric)).getText());
         Assert.assertEquals(constraintLayout.getContext().getString(R.string.weight), ((CustomFontTextView) constraintLayout.findViewById(R.id.metric_label)).getText());
         Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_three_metric)).getText());
     }
 
+    private WeightZScore getWeightZScore(int age) {
+        WeightZScore weightZScore = new WeightZScore();
+        weightZScore.setGender(Gender.MALE);
+        weightZScore.setMonth(age);
+        return weightZScore;
+    }
+
+    @After
+    public void destroy(){
+        ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", null);
+    }
+
 }
\ No newline at end of file