Add comprehensive tests for loadSettings in SettingsViewModel (#697)

* test: add comprehensive tests for loadSettings in SettingsViewModel

Adds `testLoadSettings_loadsAllProperties` to `SettingsViewModelTest.kt` to thoroughly verify that `loadSettings()` correctly maps all required settings properties from `AuthManager` to the `uiState`. The test utilizes `runTest` and `advanceUntilIdle()` to properly handle testing the coroutine launch in the ViewModel's init block.

* test: add comprehensive tests for loadSettings in SettingsViewModel

Adds `testLoadSettings_loadsAllProperties` to `SettingsViewModelTest.kt` to thoroughly verify that `loadSettings()` correctly maps all required settings properties from `AuthManager` to the `uiState`. The test utilizes `runTest` and `advanceUntilIdle()` to properly handle testing the coroutine launch in the ViewModel's init block. Fixes ktlint formatting issues on imports.
This commit is contained in:
M57 authored and GitHub committed 2026-07-23 13:17:00 +03:00
1 parent 1dd59efaab
commit b766574627
1 file changed
+36
@@ -12,7 +12,9 @@ import io.mockk.verify
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.junit.After
import org.junit.Assert.assertEquals
@@ -105,6 +107,40 @@ class SettingsViewModelTest {
assertEquals("Work", state.renameProfileName)
}
@Test
fun testLoadSettings_loadsAllProperties() =
runTest {
every { AuthManager.getSelectedProfileId() } answers { "prof-2" }
every { AuthManager.getBaseUrl() } returns "https://192.168.1.10:9119/"
every { AuthManager.getToken() } returns "dummy_token"
every { AuthManager.isAutoReconnect() } returns false
every { AuthManager.getThemePreference() } returns ThemePreference.DARK
every { AuthManager.isUseDynamicColors() } returns false
every { AuthManager.getThemePreset() } returns ThemePreset.CATPPUCCIN
every { AuthManager.isTypingEffectEnabled() } returns false
every { AuthManager.getTypingEffectDelayMs() } returns 15
every { AuthManager.getConnectionProfiles() } returns testProfiles
every { AuthManager.getAppLanguage() } returns "fr"
val viewModel = SettingsViewModel(ioDispatcher = testDispatcher)
testDispatcher.scheduler.advanceUntilIdle()
val state = viewModel.uiState.value
assertEquals("https://192.168.1.10:9119/", state.baseUrl)
assertEquals("dummy_token", state.token)
assertEquals(false, state.autoReconnect)
assertEquals(ThemePreference.DARK, state.themePreference)
assertEquals(false, state.useDynamicColors)
assertEquals(ThemePreset.CATPPUCCIN, state.themePreset)
assertEquals(false, state.typingEffectEnabled)
assertEquals(15, state.typingEffectDelayMs)
assertEquals(testProfiles, state.profiles)
assertEquals("prof-2", state.selectedProfileId)
assertEquals("Home", state.renameProfileName)
assertEquals("fr", state.appLanguage)
}
@Test
fun testLoadSettings_noSelectedProfile_renameEmpty() {
every { AuthManager.getConnectionProfiles() } returns testProfiles