Skip to content

Commit

Permalink
change: removed unused state objects from Success view model and Opti…
Browse files Browse the repository at this point in the history
…onsSelection view model, adjustments and improvements in TestSuccessViewModel

Signed-off-by: Christos Kaitatzis <[email protected]>
  • Loading branch information
ckaitatzis committed Feb 6, 2025
1 parent 827b77a commit 00ad2bd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import org.koin.core.annotation.InjectedParam
internal data class State(
val isLoading: Boolean = false,
val config: OptionsSelectionUiConfig,
val currentScreenSelectionState: String = QTSP_SELECTION_STATE,

val documentSelectionItem: SelectionOptionUi<Event.ViewDocumentItemPressed>? = null,
val qtspServiceSelectionItem: SelectionOptionUi<Event.RqesServiceSelectionItemPressed>? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ internal fun SuccessScreen(
onEventSend = { viewModel.setEvent(it) },
onNavigationRequested = { navigationEffect ->
when (navigationEffect) {
is Effect.Navigation.SwitchScreen -> navController.navigate(navigationEffect.screenRoute)
is Effect.Navigation.SwitchScreen -> {
navController.navigate(navigationEffect.screenRoute)
}

is Effect.Navigation.Finish -> context.finish()
}
},
Expand All @@ -116,7 +119,9 @@ internal fun SuccessScreen(
state.selectionItem?.let { safeSelectionItem ->
WrapModalBottomSheet(
onDismissRequest = {
viewModel.setEvent(Event.BottomSheet.UpdateBottomSheetState(isOpen = false))
viewModel.setEvent(
Event.BottomSheet.UpdateBottomSheetState(isOpen = false)
)
},
sheetState = bottomSheetState
) {
Expand Down Expand Up @@ -197,7 +202,9 @@ private fun Content(
modalBottomSheetState.hide()
}.invokeOnCompletion {
if (!modalBottomSheetState.isVisible) {
onEventSend(Event.BottomSheet.UpdateBottomSheetState(isOpen = false))
onEventSend(
Event.BottomSheet.UpdateBottomSheetState(isOpen = false)
)
}
}
}
Expand Down Expand Up @@ -262,9 +269,6 @@ private fun SuccessScreenPreview() {
PreviewTheme {
Content(
state = State(
title = "Sign document",
headline = "Success",
subtitle = "You successfully signed your document",
selectionItem = SelectionOptionUi(
mainText = "Document name.PDF",
actionText = "VIEW",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ internal data class State(
val error: ContentErrorConfig? = null,
val isBottomSheetOpen: Boolean = false,
val isBottomBarButtonEnabled: Boolean = false,

val title: String,
val headline: String? = null,
val subtitle: String? = null,
val bottomBarButtonText: String,

val sheetContent: SuccessBottomSheetContent,
) : ViewState

Expand Down Expand Up @@ -126,7 +121,6 @@ internal class SuccessViewModel(

override fun setInitialState(): State {
return State(
title = resourceProvider.getLocalizedString(LocalizableKey.SignDocument),
bottomBarButtonText = resourceProvider.getLocalizedString(LocalizableKey.Close),
sheetContent = SuccessBottomSheetContent.ShareDocument(bottomSheetTextData = getShareDocumentTextData()),
)
Expand Down Expand Up @@ -249,8 +243,6 @@ internal class SuccessViewModel(
}
),
selectionItem = null,
headline = null,
subtitle = null,
isBottomBarButtonEnabled = false,
isLoading = false,
)
Expand All @@ -276,8 +268,6 @@ internal class SuccessViewModel(
copy(
headerConfig = headerConfig,
selectionItem = selectionItem,
headline = resourceProvider.getLocalizedString(LocalizableKey.Success),
subtitle = resourceProvider.getLocalizedString(LocalizableKey.SuccessfullySignedDocument),
isBottomBarButtonEnabled = true,
isLoading = false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* governing permissions and limitations under the Licence.
*/

/*
package eu.europa.ec.eudi.rqesui.presentation.ui.success

import android.net.Uri
Expand All @@ -28,10 +27,16 @@ import eu.europa.ec.eudi.rqesui.domain.serializer.UiSerializer
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.DocumentData
import eu.europa.ec.eudi.rqesui.infrastructure.config.data.QtspData
import eu.europa.ec.eudi.rqesui.infrastructure.provider.ResourceProvider
import eu.europa.ec.eudi.rqesui.infrastructure.theme.values.ThemeColors
import eu.europa.ec.eudi.rqesui.presentation.entities.SelectionOptionUi
import eu.europa.ec.eudi.rqesui.presentation.entities.config.ViewDocumentUiConfig
import eu.europa.ec.eudi.rqesui.presentation.navigation.SdkScreens
import eu.europa.ec.eudi.rqesui.presentation.navigation.helper.generateComposableArguments
import eu.europa.ec.eudi.rqesui.presentation.navigation.helper.generateComposableNavigationLink
import eu.europa.ec.eudi.rqesui.presentation.ui.component.AppIconAndTextData
import eu.europa.ec.eudi.rqesui.presentation.ui.component.AppIcons
import eu.europa.ec.eudi.rqesui.presentation.ui.component.RelyingPartyData
import eu.europa.ec.eudi.rqesui.presentation.ui.component.content.ContentHeaderConfig
import eu.europa.ec.eudi.rqesui.presentation.ui.component.wrap.BottomSheetTextData
import eu.europa.ec.eudi.rqesui.util.CoroutineTestRule
import eu.europa.ec.eudi.rqesui.util.mockedDocumentName
Expand Down Expand Up @@ -96,8 +101,8 @@ class TestSuccessViewModel {
// Case 1
// Function setInitialState() is called to initialize the ViewModel state.
// Case 1 Expected Result:
// 1. The ViewModel's initialState should be correctly initialized with a title, a bottom bar
// and the related wordings for the bottom sheet.
// 1. The ViewModel's initialState should be correctly initialized with a ContentHeader showing
// the app icon, a bottom bar and the related wordings for the bottom sheet.
@Test
fun `Given Case 1, When setInitialState is called, Then the expected result is returned`() {
// Act
Expand All @@ -112,9 +117,14 @@ class TestSuccessViewModel {
negativeButtonText = resourceProvider.getLocalizedString(LocalizableKey.Close),
)
)
val expectedHeaderConfig = ContentHeaderConfig(
appIconAndTextData = AppIconAndTextData(),
description = null,
)

assertEquals(
resourceProvider.getLocalizedString(LocalizableKey.SignDocument),
initialState.title
expectedHeaderConfig,
initialState.headerConfig
)
assertEquals(
resourceProvider.getLocalizedString(LocalizableKey.Close),
Expand All @@ -126,10 +136,11 @@ class TestSuccessViewModel {

//region setEvent
// Case 1
// Function setEvent(Event.Init) is called to initialize the ViewModel with selected file and QTSP data.
// Function setEvent(Event.Initialize) is called to initialize the ViewModel with selected file and QTSP data.
// Case 1 Expected Result:
// 1. The mocked response contains the selected file (document name and Uri) and QTSP data.
// 2. When the Event.Init event is triggered, the ViewModel should emit the Effect.OnSelectedFileAndQtspGot effect.
// 2. When the Event.Initialize event is triggered, the ViewModel should emit the
// Effect.OnSelectedFileAndQtspGot effect.
@Test
fun `Given Case 1, When setEvent is called, Then the expected result is returned`() =
coroutineRule.runTest {
Expand All @@ -138,16 +149,19 @@ class TestSuccessViewModel {
selectedFile = DocumentData(mockedDocumentName, uri = documentFileUri),
selectedQtsp = qtspData
)
mockQTSPData(qtspData)
mockQTSPData(qtspData = qtspData)
mockGetSelectedFileAndQtspCall(response = response)

// Act
viewModel.setEvent(Event.Init)
viewModel.setEvent(Event.Initialize)

// Assert
viewModel.effect.runFlowTest {
val expectedEffect = Effect.OnSelectedFileAndQtspGot(
selectedFile = DocumentData(mockedDocumentName, uri = documentFileUri),
selectedFile = DocumentData(
documentName = mockedDocumentName,
uri = documentFileUri
),
selectedQtsp = qtspData
)
assertEquals(expectedEffect, awaitItem())
Expand All @@ -159,55 +173,65 @@ class TestSuccessViewModel {
// Case 2 Expected Result:
// 1. The mocked response simulates a successfully signed and saved document with a text prefix.
// 2. When the event is triggered, the ViewModel updates its state to reflect success.
// This includes the updated selection item, title, subtitle, bottom bar button text and bottom sheet content.
// This includes the updated selection item, contentHeader, bottom bar button text and
// bottom sheet content.
@Test
fun `Given Case 2, When setEvent is called, Then the expected result is returned`() =
coroutineRule.runTest {
// Arrange
val event = Event.SignAndSaveDocument(mockedDocumentName, mockedQtspName)

val signedDocumentPrefix = "signed_0"
val selectionItem = SelectionOptionUi(
leadingIcon = AppIcons.Verified,
leadingIconTint = ThemeColors.success,
mainText = "${signedDocumentPrefix}_$mockedDocumentName",
actionText = resourceProvider.getLocalizedString(LocalizableKey.View),
event = Event.ViewDocumentItemPressed(
documentData = DocumentData(
documentName = "${signedDocumentPrefix}_$mockedDocumentName",
uri = documentFileUri
)
),
)

val title = resourceProvider.getLocalizedString(LocalizableKey.SharingDocument)
val message = resourceProvider.getLocalizedString(LocalizableKey.CloseSharingMessage)
val signDocument = resourceProvider.getLocalizedString(LocalizableKey.SignDocument)

val signedDocumentPrefix = "signed_0"
val selectionITem = SelectionItemUi(
action = resourceProvider.getLocalizedString(LocalizableKey.View),
documentData = DocumentData(
documentName = "${signedDocumentPrefix}_$mockedDocumentName",
uri = documentFileUri
)
val headerConfig = ContentHeaderConfig(
appIconAndTextData = AppIconAndTextData(),
description = resourceProvider.getLocalizedString(LocalizableKey.SuccessDescription),
relyingPartyData = RelyingPartyData(isVerified = true, name = mockedQtspName)
)

val expectedState = State(
isLoading = false,
headline = resourceProvider.getLocalizedString(LocalizableKey.Success),
selectionItem = selectionITem,
headerConfig = headerConfig,
selectionItem = selectionItem,
error = null,
isBottomSheetOpen = false,
isBottomBarButtonEnabled = true,
title = signDocument,
subtitle = resourceProvider.getLocalizedString(LocalizableKey.SuccessfullySignedDocument),
bottomBarButtonText = resourceProvider.getLocalizedString(LocalizableKey.Close),
sheetContent = SuccessBottomSheetContent.ShareDocument(
bottomSheetTextData = BottomSheetTextData(
title = title,
message = message,
resourceProvider.getLocalizedString(LocalizableKey.Share),
resourceProvider.getLocalizedString(LocalizableKey.Close)
positiveButtonText = resourceProvider.getLocalizedString(LocalizableKey.Share),
negativeButtonText = resourceProvider.getLocalizedString(LocalizableKey.Close)
)
)
)

whenever(successInteractor.signAndSaveDocument(originalDocumentName = mockedDocumentName))
.thenReturn(
SuccessInteractorSignAndSaveDocumentPartialState.Success(
savedDocument = DocumentData(
documentName = "${signedDocumentPrefix}_$mockedDocumentName",
uri = documentFileUri
)
)
val response = SuccessInteractorSignAndSaveDocumentPartialState.Success(
savedDocument = DocumentData(
documentName = "${signedDocumentPrefix}_$mockedDocumentName",
uri = documentFileUri
)
)
mockSignAndSaveDocumentCall(
documentName = mockedDocumentName,
response = response
)

// Act
viewModel.setEvent(event)
Expand All @@ -229,8 +253,7 @@ class TestSuccessViewModel {
val errorResponse = SuccessInteractorSignAndSaveDocumentPartialState.Failure(
error = EudiRQESUiError(mockedPlainFailureMessage)
)
whenever(successInteractor.signAndSaveDocument(mockedDocumentName))
.thenReturn(errorResponse)
mockSignAndSaveDocumentCall(documentName = mockedDocumentName, response = errorResponse)

// Act
viewModel.setEvent(event)
Expand All @@ -255,7 +278,9 @@ class TestSuccessViewModel {
val expectedState = viewModel.viewState.value.copy(isBottomSheetOpen = true)

// Act
viewModel.setEvent(Event.BottomSheet.UpdateBottomSheetState(true))
viewModel.setEvent(
Event.BottomSheet.UpdateBottomSheetState(isOpen = true)
)

// Assert
assertEquals(
Expand All @@ -265,7 +290,7 @@ class TestSuccessViewModel {
}

// Case 5
// Function setEvent(Event.ViewDocument) is called to handle navigation to the ViewDocument screen.
// Function setEvent(Event.ViewDocumentItemPressed) is called to handle navigation to the ViewDocument screen.
// Case 5 Expected Result:
// 1. The correct screen route is generated based on the document data.
// 2. The ViewModel emits an Effect.Navigation.SwitchScreen to navigate to next screen.
Expand All @@ -277,7 +302,7 @@ class TestSuccessViewModel {

// Act
viewModel.setEvent(
Event.ViewDocument(documentData)
Event.ViewDocumentItemPressed(documentData = documentData)
)

// Assert
Expand Down Expand Up @@ -418,6 +443,13 @@ class TestSuccessViewModel {
whenever(successInteractor.getSelectedFileAndQtsp())
.thenReturn(response)
}

private suspend fun mockSignAndSaveDocumentCall(
documentName: String,
response: SuccessInteractorSignAndSaveDocumentPartialState
) {
whenever(successInteractor.signAndSaveDocument(originalDocumentName = documentName))
.thenReturn(response)
}
//endregion
}
*/
}

0 comments on commit 00ad2bd

Please sign in to comment.