feat(onboarding): Add OnboardingViewModel tests and improve effect handling#670
Merged
Mostafa-alsaygh merged 24 commits intodevelopfrom Aug 21, 2025
Merged
Conversation
…ndling This commit introduces comprehensive unit tests for `OnboardingViewModel`, covering various scenarios like page changes, scrolling, and navigation. Key changes: - Added `OnboardingViewModelTest.kt` with tests for all public methods and state updates. - Refactored `onboardingScreen.kt` to use `Listen` for handling `OnboardingEffect`, simplifying the effect handling logic and removing `LaunchedEffect`. - Removed unnecessary logging in `OnboardingViewModel` for `onboardingFinished`. - Updated `welcomeScreen.kt` to use `onNavigateAsGuest` instead of `onNavigateContinue` for better clarity. - Included `kotlin-test` dependency in `presentation/build.gradle.kts` for unit testing. - Updated `onboardingNavGraph.kt` to reflect the name change from `onNavigateContinue` to `onNavigateAsGuest`.
Contributor
|
[London Bot] Metrics Update Status 📊 PR metrics successfully updated for event: |
Contributor
|
[London Bot] Auto-update Status ✅ Branch successfully updated from |
yusufnasserdev
requested changes
Aug 18, 2025
Collaborator
yusufnasserdev
left a comment
There was a problem hiding this comment.
Kindly check failed test below:
OnboardingViewModelTest > when onboardingFinished is called, preferences service should be invoked FAILED
java.lang.AssertionError at OnboardingViewModelTest.kt:255
...tion/src/main/java/com/london/presentation/feature/welcome/onboarding/OnboardingViewModel.kt
Show resolved
Hide resolved
…ation
This commit renames the `OnboardingEffect.NavigateToWelcome` to `OnboardingEffect.OnWelcomeNavigation` for better clarity.
Additionally, it removes an unused `testImplementation(kotlin("test"))` dependency from the `presentation/build.gradle.kts` file.
# Conflicts: # presentation/src/main/java/com/london/presentation/feature/welcome/onboarding/welcomeScreen.kt
Code Coverage Summary
|
Refactors the `onboardingFinished` and `setOnBoardingShown` functions in `OnboardingViewModel` to use the `tryToExecute` extension function for setting the onboarding shown preference. This improves error handling and logging.
# Conflicts: # presentation/src/main/java/com/london/presentation/feature/welcome/onboarding/OnboardingViewModel.kt
This commit addresses potential flakiness in `OnboardingViewModelTest` by: - Introducing `advanceTimeBy(1000)` and `advanceUntilIdle()` in tests involving `onboardingFinished()` and `navigateToWelcome()`. This ensures that IO operations (specifically, `appPreferencesService.setOnBoardingShown()`) have sufficient time to complete before assertions are made. - Adding an alternative test case (`onboardingFinished is called, preferences service should be invoked - with timeout`) that utilizes `coVerify(timeout = 2000)` as another way to handle asynchronous operations, providing more robust verification. These changes enhance the reliability of the tests by better managing asynchronous operations within the `runTest` environment.
# Conflicts: # app/src/main/java/com/london/app/navigation/graph/onboardingNavGraph.kt # presentation/src/main/java/com/london/presentation/feature/welcome/onboarding/OnboardingScreen.kt
The `scrollNext` function in `OnboardingViewModel` involves a coroutine scope for scrolling the Pager. This commit adds `advanceTimeBy(1000)` to the corresponding test case to ensure the coroutine has enough time to complete, preventing potential flakiness.
…' into add/onboarding-viewmodel-testing
… and `coVerify` with `timeout` This commit refactors tests in `OnboardingViewModelTest` to improve reliability and readability. - `advanceTimeBy(1000)` has been replaced with `withTimeout(2000)` when asserting navigation effects. This provides a more explicit timeout mechanism for operations that might take longer. - For `coVerify` calls that interact with `appPreferencesService`, a `timeout = 2000` parameter has been added directly to the `coVerify` function. This ensures the verification waits for the asynchronous preference setting operation to complete. These changes make the tests more robust by explicitly handling potential timeouts for asynchronous operations, particularly those involving I/O like preference updates and navigation.
# Conflicts: # presentation/src/main/java/com/london/presentation/feature/category/tvshow/TvShowCategoryScreen.kt # presentation/src/main/java/com/london/presentation/feature/details/actor/info/gallery/ActorsGalleryScreen.kt # presentation/src/main/java/com/london/presentation/feature/home/trending/actor/TrendingActorsScreen.kt
...ntation/src/main/java/com/london/presentation/feature/welcome/onboarding/OnboardingEffect.kt
Outdated
Show resolved
Hide resolved
...entation/src/test/java/com/london/presentation/feature/onboarding/OnboardingViewModelTest.kt
Show resolved
Hide resolved
…' into add/onboarding-viewmodel-testing
Abdulrahman-Ragab-01
approved these changes
Aug 21, 2025
yusufnasserdev
approved these changes
Aug 21, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
this PR introduces comprehensive unit tests for
OnboardingViewModel, covering various scenarios like page changes, scrolling, and navigation.Key changes:
OnboardingViewModelTest.ktwith tests for all public methods and state updates.onboardingScreen.ktto useListenfor handlingOnboardingEffect, simplifying the effect handling logic and removingLaunchedEffect.OnboardingViewModelforonboardingFinished.welcomeScreen.ktto useonNavigateAsGuestinstead ofonNavigateContinuefor better clarity.kotlin-testdependency inpresentation/build.gradle.ktsfor unit testing.onboardingNavGraph.ktto reflect the name change fromonNavigateContinuetoonNavigateAsGuest.