From 143b1c8048a811f66d2cc169a6ac06bdd2040b85 Mon Sep 17 00:00:00 2001 From: Hilary Baraka Egesa Date: Mon, 18 Sep 2023 18:11:48 +0300 Subject: [PATCH] Add tests for composable MemberProfileBottomSheetView (#2758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add tests for composable MemberProfileBottomSheetView Co-authored-by: hilpitome * Refactor bottomsheetViewtest. Co-authored-by: Lazarus Co-authored-by: hilpitome " Signed-off-by: Lentumunai-Mark --------- Signed-off-by: Lentumunai-Mark Co-authored-by: L≡ZRS <12814349+LZRS@users.noreply.github.com> Co-authored-by: hilpitome Co-authored-by: Lentumunai-Mark --- .../MemberProfileBottomSheetViewTest.kt | 84 +++++++++++++++++++ .../MemberProfileBottomSheetView.kt | 36 ++++---- 2 files changed, 106 insertions(+), 14 deletions(-) create mode 100644 android/quest/src/androidTest/java/org/smartregister/fhircore/quest/integration/ui/profile/components/MemberProfileBottomSheetViewTest.kt diff --git a/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/integration/ui/profile/components/MemberProfileBottomSheetViewTest.kt b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/integration/ui/profile/components/MemberProfileBottomSheetViewTest.kt new file mode 100644 index 0000000000..af5f1d0041 --- /dev/null +++ b/android/quest/src/androidTest/java/org/smartregister/fhircore/quest/integration/ui/profile/components/MemberProfileBottomSheetViewTest.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2021-2023 Ona Systems, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.smartregister.fhircore.quest.integration.ui.profile.components + +import androidx.compose.material.ExperimentalMaterialApi +import androidx.compose.ui.test.assertHasClickAction +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.junit4.createComposeRule +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.onNodeWithText +import org.hl7.fhir.r4.model.ResourceType +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.smartregister.fhircore.engine.configuration.view.ButtonProperties +import org.smartregister.fhircore.engine.domain.model.ResourceData +import org.smartregister.fhircore.quest.ui.profile.components.ICON_FIELD_TAG +import org.smartregister.fhircore.quest.ui.profile.components.MemberProfileBottomSheetView +import org.smartregister.fhircore.quest.ui.profile.components.TOP_SECTION_ROW +import org.smartregister.fhircore.quest.ui.profile.components.VIEW_PROFILE_TAG + +@OptIn(ExperimentalMaterialApi::class) +class MemberProfileBottomSheetViewTest { + + @get:Rule val composeTestRule = createComposeRule() + + @Before + fun setUp() { + val buttonProperties = + listOf( + ButtonProperties(text = "Issue bednet", status = "OVERDUE"), + ) + composeTestRule.setContent { + MemberProfileBottomSheetView( + title = "John Doe, M, 35y", + buttonProperties = buttonProperties, + onViewProfile = { /*Do nothing*/}, + resourceData = ResourceData("id", ResourceType.Patient, emptyMap()), + ) + } + } + + @Test + fun testTopMemberProfileSectionDisplayedCorrectly() { + composeTestRule.onNodeWithTag(TOP_SECTION_ROW).assertExists().assertIsDisplayed() + } + + @Test + fun testViewProfileIsDisplayedAndHasClickAction() { + composeTestRule + .onNodeWithTag(VIEW_PROFILE_TAG) + .assertExists() + .assertIsDisplayed() + .assertHasClickAction() + } + + @Test + fun testMemberProfileBottomSheetViewIconExistsAndHasClickAction() { + composeTestRule.onNodeWithTag(ICON_FIELD_TAG).assertExists().assertHasClickAction() + } + + @Test + fun testMemberProfileBottomSheetViewButtonsRendersCorrectly() { + composeTestRule + .onNodeWithText("Issue bednet") + .assertExists() + .assertIsDisplayed() + .assertHasClickAction() + } +} diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/profile/components/MemberProfileBottomSheetView.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/profile/components/MemberProfileBottomSheetView.kt index 4dca2fb5b1..5b6cce4f5c 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/profile/components/MemberProfileBottomSheetView.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/profile/components/MemberProfileBottomSheetView.kt @@ -39,6 +39,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign @@ -58,15 +59,19 @@ import org.smartregister.fhircore.engine.util.annotation.PreviewWithBackgroundEx import org.smartregister.fhircore.quest.R import org.smartregister.fhircore.quest.ui.shared.components.ActionableButton +const val VIEW_PROFILE_TAG = "view-profile-tag" +const val TOP_SECTION_ROW = "top-section-row" +const val ICON_FIELD_TAG = "iconTestTag" + @Composable fun MemberProfileBottomSheetView( modifier: Modifier = Modifier, - coroutineScope: CoroutineScope, - bottomSheetScaffoldState: BottomSheetScaffoldState, + coroutineScope: CoroutineScope = rememberCoroutineScope(), + bottomSheetScaffoldState: BottomSheetScaffoldState = rememberBottomSheetScaffoldState(), title: String, buttonProperties: List, - ResourceData: ResourceData, - navController: NavController, + resourceData: ResourceData, + navController: NavController = rememberNavController(), onViewProfile: () -> Unit, ) { Column { @@ -75,7 +80,7 @@ fun MemberProfileBottomSheetView( Row( horizontalArrangement = Arrangement.SpaceBetween, verticalAlignment = Alignment.CenterVertically, - modifier = modifier.fillMaxWidth().padding(horizontal = 16.dp), + modifier = modifier.fillMaxWidth().padding(horizontal = 16.dp).testTag(TOP_SECTION_ROW), ) { Column(modifier = modifier.wrapContentWidth(Alignment.Start)) { Text( @@ -90,13 +95,15 @@ fun MemberProfileBottomSheetView( contentDescription = null, tint = DefaultColor.copy(0.8f), modifier = - modifier.clickable { - coroutineScope.launch { - if (!bottomSheetScaffoldState.bottomSheetState.isCollapsed) { - bottomSheetScaffoldState.bottomSheetState.collapse() + modifier + .clickable { + coroutineScope.launch { + if (!bottomSheetScaffoldState.bottomSheetState.isCollapsed) { + bottomSheetScaffoldState.bottomSheetState.collapse() + } } } - }, + .testTag(ICON_FIELD_TAG), ) } Spacer(modifier = modifier.height(8.dp)) @@ -107,7 +114,7 @@ fun MemberProfileBottomSheetView( buttonProperties.forEach { ActionableButton( buttonProperties = it.copy(buttonType = ButtonType.BIG), - resourceData = ResourceData, + resourceData = resourceData, navController = navController, ) } @@ -123,7 +130,8 @@ fun MemberProfileBottomSheetView( modifier .fillMaxWidth() .clickable { onViewProfile() } - .padding(horizontal = 16.dp, vertical = 16.dp), + .padding(horizontal = 16.dp, vertical = 16.dp) + .testTag(VIEW_PROFILE_TAG), ) Spacer(modifier = modifier.height(16.dp)) } @@ -139,7 +147,7 @@ private fun MemberProfileBottomSheetViewPreview() { buttonProperties = emptyList(), navController = rememberNavController(), onViewProfile = { /*Do nothing*/}, - ResourceData = ResourceData("id", ResourceType.Patient, emptyMap()), + resourceData = ResourceData("id", ResourceType.Patient, emptyMap()), ) } @@ -158,6 +166,6 @@ private fun MemberProfileBottomSheetViewWithFormDataPreview() { ), navController = rememberNavController(), onViewProfile = { /*Do nothing*/}, - ResourceData = ResourceData("id", ResourceType.Patient, emptyMap()), + resourceData = ResourceData("id", ResourceType.Patient, emptyMap()), ) }