Skip to content

Commit

Permalink
test: add test for the slider value on change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrish92 committed Dec 19, 2024
1 parent 1f03af9 commit 58e17d8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,7 @@ fun SettingsScreen(
SettingsContainer(testTag = SettingsScreen.SLIDER_CONTAINER) {
SliderMenu(sliderPosition) {
sliderPosition = (it / 100).roundToInt() * 100f

userViewModel.user.value?.let { user ->
val newUser =
User(
name = user.name,
dob = user.dob,
description = user.description,
imageUrl = user.imageUrl,
preferredDistance = sliderPosition.toInt(),
)

userViewModel.saveUser(
newUser,
onSuccess = { Log.d(LOG_SETTINGS_TAG, "User updated successfully") },
onFailure = { Log.d(LOG_SETTINGS_TAG, "Failed to update user") })
}
sliderLogic(sliderPosition, userViewModel)
}

Text(
Expand Down Expand Up @@ -430,3 +415,31 @@ private fun DeleteAccountDialog(
}
}
}

/**
* Function that updates the user's preferred distance when the slider is moved.
*
* @param sliderPosition The position of the slider.
* @param userViewModel The ViewModel that handles user data.
*/
public fun sliderLogic(
sliderPosition: Float,
userViewModel: UserViewModel,
) {

userViewModel.user.value?.let { user ->
val newUser =
User(
name = user.name,
dob = user.dob,
description = user.description,
imageUrl = user.imageUrl,
preferredDistance = sliderPosition.toInt(),
)

userViewModel.saveUser(
newUser,
onSuccess = { Log.d(LOG_SETTINGS_TAG, "User updated successfully") },
onFailure = { Log.d(LOG_SETTINGS_TAG, "Failed to update user") })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,14 @@ class SettingsScreenTest {
verify(navigationActions, never()).navigateTo(any<String>())
verify(navigationActions, never()).navigateTo(any<TopLevelDestination>())
}

@Test
fun sliderLogicTest() {
`when`(userViewModel.saveUser(any(), any(), any())).thenAnswer {
val onSuccess = it.arguments[1] as () -> Unit
onSuccess()
}

sliderLogic(preferredDistance.toFloat(), userViewModel)
}
}

0 comments on commit 58e17d8

Please sign in to comment.