Skip to content

Commit df1732e

Browse files
Pratul KaliaSaket Narayan
authored andcommitted
Add test for auto-focus diastolic field
1 parent 4be148b commit df1732e

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

app/src/main/java/org/simple/clinic/bp/entry/BloodPressureEntrySheetController.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,21 @@ class BloodPressureEntrySheetController @Inject constructor(
2828
val replayedEvents = events.compose(ReportAnalyticsEvents()).replay().refCount()
2929

3030
return Observable.merge(
31-
systolicTextChanges(replayedEvents),
31+
automaticDiastolicFocusChanges(replayedEvents),
3232
validationErrorResets(replayedEvents),
3333
bpValidationsAndSaves(replayedEvents))
3434
}
3535

36-
private fun systolicTextChanges(events: Observable<UiEvent>): Observable<UiChange> {
37-
// TODO: This needs unit tests
38-
return events
39-
.ofType<BloodPressureSystolicTextChanged>()
40-
.distinctUntilChanged()
36+
private fun automaticDiastolicFocusChanges(events: Observable<UiEvent>): Observable<UiChange> {
37+
return events.ofType<BloodPressureSystolicTextChanged>()
4138
.filter { shouldFocusDiastolic(it.systolic) }
39+
.distinctUntilChanged()
4240
.map { { ui: Ui -> ui.changeFocusToDiastolic() } }
4341
}
4442

4543
private fun shouldFocusDiastolic(systolicText: String): Boolean {
46-
return (systolicText.length == 3 && systolicText.matches("^[12].*$".toRegex()))
47-
|| (systolicText.length == 2 && systolicText.matches("^[3-9].*$".toRegex()))
44+
return (systolicText.length == 3 && systolicText.matches("^[123].*$".toRegex()))
45+
|| (systolicText.length == 2 && systolicText.matches("^[789].*$".toRegex()))
4846
}
4947

5048
private fun validationErrorResets(events: Observable<UiEvent>): Observable<UiChange> {

app/src/test/java/org/simple/clinic/bp/entry/BloodPressureEntrySheetControllerTest.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import com.nhaarman.mockito_kotlin.verify
88
import com.nhaarman.mockito_kotlin.whenever
99
import io.reactivex.Single
1010
import io.reactivex.subjects.PublishSubject
11+
import junitparams.JUnitParamsRunner
12+
import junitparams.Parameters
1113
import org.junit.Before
1214
import org.junit.Test
15+
import org.junit.runner.RunWith
1316
import org.simple.clinic.bp.BloodPressureRepository
1417
import org.simple.clinic.patient.PatientMocker
1518
import org.simple.clinic.widgets.UiEvent
1619
import java.util.UUID
1720

21+
@RunWith(JUnitParamsRunner::class)
1822
class BloodPressureEntrySheetControllerTest {
1923

2024
private val sheet = mock<BloodPressureEntrySheet>()
@@ -33,6 +37,24 @@ class BloodPressureEntrySheetControllerTest {
3337
.subscribe { uiChange -> uiChange(sheet) }
3438
}
3539

40+
@Test
41+
@Parameters(value = [
42+
"90|true",
43+
"120|true",
44+
"300|true",
45+
"66|false",
46+
"44|false"
47+
])
48+
fun `when valid systolic value is entered, move cursor to diastolic field automatically`(sampleSystolicBp: String, shouldMove: Boolean) {
49+
uiEvents.onNext(BloodPressureEntrySheetCreated(patientUuid))
50+
uiEvents.onNext(BloodPressureSystolicTextChanged(sampleSystolicBp))
51+
52+
when (shouldMove) {
53+
true -> verify(sheet).changeFocusToDiastolic()
54+
false -> verify(sheet, never()).changeFocusToDiastolic()
55+
}
56+
}
57+
3658
@Test
3759
fun `when systolic is less than diastolic, show error`() {
3860
uiEvents.onNext(BloodPressureEntrySheetCreated(patientUuid))
@@ -90,6 +112,7 @@ class BloodPressureEntrySheetControllerTest {
90112

91113
@Test
92114
fun `when systolic or diastolic values change, hide the error message`() {
115+
uiEvents.onNext(BloodPressureEntrySheetCreated(patientUuid))
93116
uiEvents.onNext(BloodPressureSystolicTextChanged("12"))
94117
uiEvents.onNext(BloodPressureSystolicTextChanged("120"))
95118
uiEvents.onNext(BloodPressureSystolicTextChanged("130"))

0 commit comments

Comments
 (0)