Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test changemanagingentity opens bottomsheet model #3031

Merged
merged 13 commits into from
Feb 27, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.smartregister.fhircore.quest.ui.profile

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.navigation.NavController
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
Expand All @@ -35,6 +38,8 @@ import javax.inject.Inject
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.hl7.fhir.r4.model.Group
import org.hl7.fhir.r4.model.Patient
import org.hl7.fhir.r4.model.ResourceType
import org.junit.Before
Expand All @@ -52,9 +57,11 @@ import org.smartregister.fhircore.engine.rulesengine.ResourceDataRulesExecutor
import org.smartregister.fhircore.engine.util.DefaultDispatcherProvider
import org.smartregister.fhircore.engine.util.DispatcherProvider
import org.smartregister.fhircore.engine.util.extension.BLACK_COLOR_HEX_CODE
import org.smartregister.fhircore.engine.util.extension.getActivity
import org.smartregister.fhircore.engine.util.fhirpath.FhirPathDataExtractor
import org.smartregister.fhircore.quest.app.fakes.Faker
import org.smartregister.fhircore.quest.robolectric.RobolectricTest
import org.smartregister.fhircore.quest.ui.profile.bottomSheet.ProfileBottomSheetFragment
import org.smartregister.fhircore.quest.ui.profile.model.EligibleManagingEntity
import org.smartregister.fhircore.quest.util.extensions.handleClickEvent

Expand Down Expand Up @@ -101,8 +108,13 @@ class ProfileViewModelTest : RobolectricTest() {
parser = parser,
),
)
coEvery { registerRepository.loadProfileData(any(), any(), paramsList = emptyArray()) } returns
RepositoryResourceData(resource = Faker.buildPatient())
coEvery {
registerRepository.loadProfileData(
any(),
any(),
paramsList = emptyArray(),
)
} returns RepositoryResourceData(resource = Faker.buildPatient())

runBlocking {
configurationRegistry.loadConfigurations(
Expand Down Expand Up @@ -210,4 +222,76 @@ class ProfileViewModelTest : RobolectricTest() {
profileViewModel.onEvent(event)
}
}

@Test
fun testThatManagingEntityProfileBottomSheetIsShownOnActionTriggered() = runTest {
val navController = mockk<NavController>()
val event = mockk<ProfileEvent.OverflowMenuClick>()
val fragmentManager = mockk<FragmentManager>()
val fragmentManagerTransaction = mockk<FragmentTransaction>()
val overflowMenuItemConfig =
OverflowMenuItemConfig(
id = 1,
title = "open profile bottom sheet",
confirmAction = false,
icon = null,
titleColor = BLACK_COLOR_HEX_CODE,
backgroundColor = null,
visible = "true",
showSeparator = false,
enabled = "true",
actions = emptyList(),
)
val group =
Group().apply { managingEntity = managingEntity.apply { reference = "patient/1424251" } }
val managingEntityResource = mockk<Group.GroupMemberComponent>()
val profileBottomSheetFragment = mockk<ProfileBottomSheetFragment>()
val activity = mockk<AppCompatActivity>()

val viewModel =
ProfileViewModel(
registerRepository,
configurationRegistry,
dispatcherProvider,
fhirPathDataExtractor,
resourceDataRulesExecutor,
)

val managingEntityConfig =
ManagingEntityConfig(
nameFhirPathExpression = "name",
eligibilityCriteriaFhirPathExpression = "criteria",
resourceType = ResourceType.Patient,
dialogTitle = "Change Managing Entity",
dialogWarningMessage = "Warning",
dialogContentMessage = "Select a new managing entity",
noMembersErrorMessage = "No members found",
managingEntityReassignedMessage = "Managing entity reassigned",
)

coEvery { registerRepository.loadResource<Group>("group1") } returns group
coEvery { group.member } returns listOf(managingEntityResource)
every { managingEntityResource.id } returns "entity1"
every {
fhirPathDataExtractor.extractValue(
managingEntityResource,
"name",
)
} returns "memebr 1"
every { activity.supportFragmentManager } returns fragmentManager
every { activity.supportFragmentManager.beginTransaction() } returns fragmentManagerTransaction
viewModel.onEvent(
ProfileEvent.OverflowMenuClick(
navController,
resourceData,
overflowMenuItemConfig,
),
)
profileViewModel.changeManagingEntity(event, managingEntityConfig)
verifyAll {
navController.context
activity.getActivity()
profileBottomSheetFragment.show(fragmentManager, ProfileBottomSheetFragment.TAG)
}
}
}
Loading