-
Notifications
You must be signed in to change notification settings - Fork 0
π§ͺ test(settings): add missing tests for SettingsViewModel #33
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.sayanthrock.rockreleasehub.feature.settings | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class SettingsViewModelTest { | ||
|
|
||
| @Test | ||
| fun `initial state is Success with isDarkMode true`() { | ||
| val viewModel = SettingsViewModel() | ||
| val currentState = viewModel.uiState.value | ||
|
|
||
| assertEquals(SettingsState.Success(isDarkMode = true), currentState) | ||
| } | ||
|
|
||
| @Test | ||
| fun `toggleDarkMode changes isDarkMode from true to false`() { | ||
| val viewModel = SettingsViewModel() | ||
|
|
||
| // Initial state is true | ||
| assertEquals(SettingsState.Success(isDarkMode = true), viewModel.uiState.value) | ||
|
|
||
| // Toggle | ||
| viewModel.toggleDarkMode() | ||
|
|
||
| // State should now be false | ||
| assertEquals(SettingsState.Success(isDarkMode = false), viewModel.uiState.value) | ||
| } | ||
|
|
||
| @Test | ||
| fun `toggleDarkMode changes isDarkMode from false to true`() { | ||
| val viewModel = SettingsViewModel() | ||
|
|
||
| // Toggle to false | ||
| viewModel.toggleDarkMode() | ||
| assertEquals(SettingsState.Success(isDarkMode = false), viewModel.uiState.value) | ||
|
|
||
| // Toggle back to true | ||
| viewModel.toggleDarkMode() | ||
| assertEquals(SettingsState.Success(isDarkMode = true), viewModel.uiState.value) | ||
|
Comment on lines
+17
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: These tests cover only an in-memory Severity Level: Critical π¨- β Dark-mode preference resets after ViewModel recreation.
- β Process recreation loses the user's setting.
- β οΈ Toggle tests provide incomplete feature coverage.Steps of Reproduction β1. Open the Settings screen through the route at
`app/src/main/java/com/sayanthrock/rockreleasehub/AppNavGraph.kt:92`; its switch invokes
`toggleDarkMode()` at
`feature-settings/src/main/java/com/sayanthrock/rockreleasehub/feature/settings/SettingsScreen.kt:28-33`.
2. Toggle the switch and confirm the added tests pass because
`SettingsViewModel.toggleDarkMode()` updates only `_uiState.value` at
`feature-settings/src/main/java/com/sayanthrock/rockreleasehub/feature/settings/SettingsViewModel.kt:15-18`.
3. Recreate the ViewModel, such as after its owning navigation entry or process is
recreated, by constructing a new `SettingsViewModel`; its initializer at
`SettingsViewModel.kt:11-13` always restores `SettingsState.Success(isDarkMode = true)`.
4. Inspect the feature's available persistence dependency at
`feature-settings/build.gradle.kts:42` and the source usage found in the module: no
DataStore read or write is connected to this ViewModel. The tests at lines 17-40 therefore
verify only one transient instance and cannot detect loss of the user's preference or its
missing connection to `RockReleaseHubTheme`.(Use Cmd/Ctrl + Click for best experience) Prompt for AI Agent π€This is a comment left during a code review.
**Path:** feature-settings/src/test/java/com/sayanthrock/rockreleasehub/feature/settings/SettingsViewModelTest.kt
**Line:** 17:40
**Comment:**
*Incomplete Implementation: These tests cover only an in-memory `StateFlow` transition within one ViewModel instance. They do not verify that toggling changes the actual application theme or survives ViewModel/activity recreation, so they provide false confidence for the user-visible dark-mode behavior, which is currently not connected to `RockReleaseHubTheme` or persisted through DataStore.
Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: The test only verifies the ViewModel's hardcoded initial value and does not verify that the setting is consumed by the application's theme.
MainActivitycurrently invokesRockReleaseHubThemewithout passing this value, so this test can remain green while the rendered theme still follows the system setting instead of the settings screen's default. [incomplete implementation]Severity Level: Critical π¨
Steps of Reproduction β
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent π€