Skip to content

Commit

Permalink
Merge pull request #495 from OpenSRP/tt-oct-6-2020-jsonformpresenter
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
bennsimon committed Oct 13, 2020
2 parents 8f144de + 9683139 commit 6397294
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
Expand Up @@ -954,7 +954,7 @@ public void onItemSelected(AdapterView<?> parent, View view, int position, long
try {
value = jsonArray.getString(position);
} catch (JSONException e) {
Log.e(TAG, e.toString());
Timber.e(e);
}
}
if (getView() != null) {
Expand Down
Expand Up @@ -6,6 +6,8 @@
import android.os.Bundle;
import android.support.v7.widget.AppCompatSpinner;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -26,6 +28,7 @@
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.FormUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
Expand All @@ -42,6 +45,7 @@
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadows.ShadowToast;
import org.robolectric.util.ReflectionHelpers;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand All @@ -65,6 +69,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
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 @@ -446,5 +451,89 @@ public void testPreLoadRulesShouldInitializeRules() throws JSONException {
verify(rulesEngineFactory).getRulesFromAsset("sample-calculation-rules.yml");
}

@Test
public void testOnCheckedChangedShouldWriteValueToForm() throws InterruptedException {
initWithActualForm();
CheckBox checkBox = new CheckBox(RuntimeEnvironment.application);
checkBox.setTag(R.id.key, "user_check_box");
checkBox.setTag(R.id.childKey, "no");
checkBox.setChecked(true);

presenter.onCheckedChanged(checkBox, true);

verify(formFragment, times(1))
.writeValue(eq("step1"), eq("user_check_box"),
eq(JsonFormConstants.OPTIONS_FIELD_NAME), eq("no"), eq(String.valueOf(true)),
nullable(String.class), nullable(String.class), nullable(String.class), eq(false));
}

@Test
public void testOnCheckedChangedWithExclusiveShouldUncheckAllExceptChildKeyThenWriteValueToForm() throws InterruptedException, JSONException {
initWithActualForm();
CheckBox checkBox = new CheckBox(RuntimeEnvironment.application);
checkBox.setTag(R.id.key, "user_check_box");
checkBox.setTag(R.id.childKey, "none");
checkBox.setChecked(true);

JSONObject form = jsonFormActivity.getmJSONObject();

JSONObject jsonObjectField = FormUtils.getFieldFromForm(form, "user_check_box");
jsonObjectField.put("exclusive", new JSONArray("[\"none\"]"));

presenter.onCheckedChanged(checkBox, true);

verify(formFragment, times(1))
.unCheckAllExcept(eq("user_check_box"), eq("none"), eq(checkBox));

verify(formFragment, times(1))
.writeValue(eq("step1"), eq("user_check_box"),
eq(JsonFormConstants.OPTIONS_FIELD_NAME), eq("none"), eq(String.valueOf(true)),
nullable(String.class), nullable(String.class), nullable(String.class), eq(false));
}


@Test
public void testOnCheckedChangedWithExclusiveShouldUncheckThenWriteValueToForm() throws InterruptedException, JSONException {
initWithActualForm();
CheckBox checkBox = new CheckBox(RuntimeEnvironment.application);
checkBox.setTag(R.id.key, "user_check_box");
checkBox.setTag(R.id.childKey, "none");
checkBox.setChecked(true);

JSONObject form = jsonFormActivity.getmJSONObject();

JSONObject jsonObjectField = FormUtils.getFieldFromForm(form, "user_check_box");
jsonObjectField.put("exclusive", new JSONArray("[\"dont_know\"]"));

presenter.onCheckedChanged(checkBox, true);

verify(formFragment, times(1))
.unCheck(eq("user_check_box"), eq("dont_know"), eq(checkBox));

verify(formFragment, times(1))
.writeValue(eq("step1"), eq("user_check_box"),
eq(JsonFormConstants.OPTIONS_FIELD_NAME), eq("none"), eq(String.valueOf(true)),
nullable(String.class), nullable(String.class), nullable(String.class), eq(false));
}

@Test
public void testOnItemSelectedShouldWriteSelectedValueToForm() throws JSONException {
Spinner adapterView = mock(Spinner.class);
doReturn("user_location").when(adapterView).getTag(R.id.key);
JSONArray jsonArray = new JSONArray("[\"user_option_one\",\"user_option_two\"]");
doReturn(jsonArray).when(adapterView).getTag(R.id.keys);
doReturn("user_option_one").when(adapterView).getItemAtPosition(0);

JsonFormFragmentPresenter spyPresenter = spy(presenter);

ReflectionHelpers.setField(spyPresenter, "mStepName", "step1");

spyPresenter.onItemSelected(adapterView, mock(View.class), 0, 0);

verify(formFragment, times(1))
.writeValue(eq("step1"), eq("user_location"),
eq(jsonArray.getString(0)),
nullable(String.class), nullable(String.class), nullable(String.class), eq(false));
}

}

0 comments on commit 6397294

Please sign in to comment.