diff --git a/app/src/main/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandler.kt b/app/src/main/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandler.kt index 3a878180f83..5d5770bc1e5 100644 --- a/app/src/main/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandler.kt +++ b/app/src/main/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandler.kt @@ -33,8 +33,8 @@ class MedicalHistorySummaryEffectHandler @Inject constructor( return ObservableTransformer { effects -> effects .observeOn(schedulers.io()) - .map { - medicalHistoryRepository.historyForPatientOrDefaultImmediate( + .flatMap { + medicalHistoryRepository.historyForPatientOrDefault( defaultHistoryUuid = uuidGenerator.v4(), patientUuid = it.patientUUID ) diff --git a/app/src/test/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandlerTest.kt b/app/src/test/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandlerTest.kt new file mode 100644 index 00000000000..292df4675ac --- /dev/null +++ b/app/src/test/java/org/simple/clinic/summary/medicalhistory/MedicalHistorySummaryEffectHandlerTest.kt @@ -0,0 +1,53 @@ +package org.simple.clinic.summary.medicalhistory + +import io.reactivex.Observable +import org.junit.Test +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import org.simple.clinic.medicalhistory.MedicalHistoryRepository +import org.simple.clinic.mobius.EffectHandlerTestCase +import org.simple.clinic.summary.prescribeddrugs.DrugSummaryEffectHandler +import org.simple.clinic.util.scheduler.TestSchedulersProvider +import org.simple.sharedTestCode.TestData +import org.simple.sharedTestCode.util.TestUtcClock +import org.simple.sharedTestCode.uuid.FakeUuidGenerator +import java.util.UUID + +class MedicalHistorySummaryEffectHandlerTest { + + private val facility = TestData.facility(uuid = UUID.fromString("94db5d90-d483-4755-892a-97fde5a870fe")) + private val medicalHistoryRepository = mock() + private val clock = TestUtcClock() + private val uuidGenerator = FakeUuidGenerator(uuid = UUID.fromString("e78ec5f7-6fe9-4812-a894-9e34e55c670e")) + private val effectHandler = MedicalHistorySummaryEffectHandler( + schedulers = TestSchedulersProvider.trampoline(), + medicalHistoryRepository = medicalHistoryRepository, + clock = clock, + currentFacility = { facility }, + uuidGenerator = uuidGenerator + ) + + private val testCase = EffectHandlerTestCase(effectHandler.build()) + + @Test + fun `when load medical history effect is received, then load medical history`() { + // given + val patientUuid = UUID.fromString("642ba684-25f3-4750-ba52-cd21ebf7cfca") + val medicalHistory = TestData.medicalHistory( + uuid = UUID.fromString("8568fd00-7de8-469e-a869-e92e54eb7f9b"), + patientUuid = patientUuid + ) + + whenever(medicalHistoryRepository.historyForPatientOrDefault( + patientUuid = patientUuid, + defaultHistoryUuid = uuidGenerator.v4() + )) doReturn Observable.just(medicalHistory) + + // when + testCase.dispatch(LoadMedicalHistory(patientUUID = patientUuid)) + + // then + testCase.assertOutgoingEvents(MedicalHistoryLoaded(medicalHistory)) + } +}