|
| 1 | +package org.dhis2.usescases.teiDashboard |
| 2 | + |
| 3 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
| 4 | +import io.reactivex.Observable |
| 5 | +import kotlinx.coroutines.CoroutineDispatcher |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 8 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 9 | +import kotlinx.coroutines.test.setMain |
| 10 | +import org.dhis2.commons.viewmodel.DispatcherProvider |
| 11 | +import org.dhis2.utils.analytics.ACTIVE_FOLLOW_UP |
| 12 | +import org.dhis2.utils.analytics.AnalyticsHelper |
| 13 | +import org.dhis2.utils.analytics.FOLLOW_UP |
| 14 | +import org.hisp.dhis.android.core.common.State |
| 15 | +import org.hisp.dhis.android.core.enrollment.Enrollment |
| 16 | +import org.hisp.dhis.android.core.enrollment.EnrollmentStatus |
| 17 | +import org.junit.Assert.assertTrue |
| 18 | +import org.junit.Before |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | +import org.mockito.kotlin.any |
| 22 | +import org.mockito.kotlin.doReturn |
| 23 | +import org.mockito.kotlin.mock |
| 24 | +import org.mockito.kotlin.verify |
| 25 | +import org.mockito.kotlin.whenever |
| 26 | + |
| 27 | +class DashboardViewModelTest { |
| 28 | + |
| 29 | + @get:Rule |
| 30 | + val instantExecutorRule = InstantTaskExecutorRule() |
| 31 | + |
| 32 | + private val repository: DashboardRepository = mock() |
| 33 | + private val analyticsHelper: AnalyticsHelper = mock() |
| 34 | + private val testingDispatcher = StandardTestDispatcher() |
| 35 | + |
| 36 | + @OptIn(ExperimentalCoroutinesApi::class) |
| 37 | + @Before |
| 38 | + fun setUp() { |
| 39 | + Dispatchers.setMain(testingDispatcher) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun shouldFetchEnrollmentModel() { |
| 44 | + mockEnrollmentModel() |
| 45 | + mockGrouping(true) |
| 46 | + |
| 47 | + val dashboardViewModel = getViewModel() |
| 48 | + |
| 49 | + with(dashboardViewModel) { |
| 50 | + assertTrue(dashboardModel.value == mockedEnrollmentModel) |
| 51 | + assertTrue(showFollowUpBar.value) |
| 52 | + assertTrue(!syncNeeded.value) |
| 53 | + assertTrue(showStatusBar.value == EnrollmentStatus.ACTIVE) |
| 54 | + assertTrue(state.value == State.SYNCED) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun shouldFetchTeiModel() { |
| 60 | + mockTeiModel() |
| 61 | + mockGrouping(false) |
| 62 | + |
| 63 | + val dashboardViewModel = getViewModel() |
| 64 | + |
| 65 | + with(dashboardViewModel) { |
| 66 | + assertTrue(dashboardModel.value == mockedTeiModel) |
| 67 | + assertTrue(!showFollowUpBar.value) |
| 68 | + assertTrue(!syncNeeded.value) |
| 69 | + assertTrue(showStatusBar.value == null) |
| 70 | + assertTrue(state.value == null) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun shouldSetGrouping() { |
| 76 | + mockEnrollmentModel() |
| 77 | + mockGrouping(false) |
| 78 | + |
| 79 | + val dashboardViewModel = getViewModel() |
| 80 | + |
| 81 | + with(dashboardViewModel) { |
| 82 | + assertTrue(groupByStage.value == false) |
| 83 | + setGrouping(true) |
| 84 | + verify(repository).setGrouping(true) |
| 85 | + assertTrue(groupByStage.value == true) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun shouldUpdateEventUid() { |
| 91 | + mockEnrollmentModel() |
| 92 | + mockGrouping(false) |
| 93 | + |
| 94 | + with(getViewModel()) { |
| 95 | + assertTrue(eventUid().value == null) |
| 96 | + updateEventUid("eventUid") |
| 97 | + assertTrue(eventUid().value == "eventUid") |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun shouldSetFollowUpOnEnrollment() { |
| 103 | + mockEnrollmentModel() |
| 104 | + mockGrouping(false) |
| 105 | + |
| 106 | + with(getViewModel()) { |
| 107 | + onFollowUp() |
| 108 | + verify(repository).setFollowUp("enrollmentUid") |
| 109 | + assertTrue(state.value == State.TO_UPDATE) |
| 110 | + verify(analyticsHelper).setEvent(ACTIVE_FOLLOW_UP, "false", FOLLOW_UP) |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + fun shouldUpdateEnrollmentStatus() { |
| 116 | + mockEnrollmentModel() |
| 117 | + mockGrouping(false) |
| 118 | + |
| 119 | + with(getViewModel()) { |
| 120 | + whenever(repository.updateEnrollmentStatus(any(), any())) doReturn Observable.just( |
| 121 | + StatusChangeResultCode.CHANGED, |
| 122 | + ) |
| 123 | + updateEnrollmentStatus(EnrollmentStatus.COMPLETED) |
| 124 | + verify(repository).updateEnrollmentStatus("enrollmentUid", EnrollmentStatus.COMPLETED) |
| 125 | + assertTrue(showStatusBar.value == EnrollmentStatus.COMPLETED) |
| 126 | + assertTrue(syncNeeded.value) |
| 127 | + assertTrue(state.value == State.TO_UPDATE) |
| 128 | + assertTrue(updateEnrollment.value == true) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + fun shouldShowMessageIfErrorWhileUpdatingEnrollmentStatus() { |
| 134 | + mockEnrollmentModel() |
| 135 | + mockGrouping(false) |
| 136 | + |
| 137 | + with(getViewModel()) { |
| 138 | + whenever(repository.updateEnrollmentStatus(any(), any())) doReturn Observable.just( |
| 139 | + StatusChangeResultCode.FAILED, |
| 140 | + ) |
| 141 | + updateEnrollmentStatus(EnrollmentStatus.COMPLETED) |
| 142 | + assertTrue(showStatusErrorMessages.value == StatusChangeResultCode.FAILED) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private fun getViewModel() = DashboardViewModel( |
| 147 | + repository, |
| 148 | + analyticsHelper, |
| 149 | + object : |
| 150 | + DispatcherProvider { |
| 151 | + override fun io(): CoroutineDispatcher { |
| 152 | + return testingDispatcher |
| 153 | + } |
| 154 | + |
| 155 | + override fun computation(): CoroutineDispatcher { |
| 156 | + return testingDispatcher |
| 157 | + } |
| 158 | + |
| 159 | + override fun ui(): CoroutineDispatcher { |
| 160 | + return testingDispatcher |
| 161 | + } |
| 162 | + }, |
| 163 | + ).also { |
| 164 | + testingDispatcher.scheduler.advanceUntilIdle() |
| 165 | + } |
| 166 | + |
| 167 | + private fun mockEnrollmentModel() { |
| 168 | + whenever(repository.getDashboardModel()) doReturn mockedEnrollmentModel |
| 169 | + whenever(mockedEnrollmentModel.currentEnrollment) doReturn mockedEnrollment |
| 170 | + } |
| 171 | + |
| 172 | + private fun mockTeiModel() { |
| 173 | + whenever(repository.getDashboardModel()) doReturn mockedTeiModel |
| 174 | + } |
| 175 | + |
| 176 | + private fun mockGrouping(group: Boolean) { |
| 177 | + whenever(repository.getGrouping()) doReturn group |
| 178 | + } |
| 179 | + |
| 180 | + private val mockedEnrollmentModel: DashboardEnrollmentModel = mock() |
| 181 | + private val mockedTeiModel: DashboardTEIModel = mock() |
| 182 | + private val mockedEnrollment: Enrollment = mock { |
| 183 | + on { uid() } doReturn "enrollmentUid" |
| 184 | + on { followUp() } doReturn true |
| 185 | + on { aggregatedSyncState() } doReturn State.SYNCED |
| 186 | + on { status() } doReturn EnrollmentStatus.ACTIVE |
| 187 | + } |
| 188 | +} |
0 commit comments