From 5aad806b8f93cf3316d902349ba19c21921ac37f Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Thu, 9 Jul 2026 15:15:57 +0300 Subject: [PATCH] Add test for KeysViewModel state updates (#507) Added unit tests to `KeysViewModelTest.kt` to cover `setNewKeyName` and `setNewKeyValue` functions. This ensures that the state flow correctly updates when key names and values are modified. This increases the test coverage for UI state manipulation within the Keys settings module. --- .../ui/keys/KeysViewModelTest.kt | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app/src/test/java/com/m57/hermescontrol/ui/keys/KeysViewModelTest.kt diff --git a/app/src/test/java/com/m57/hermescontrol/ui/keys/KeysViewModelTest.kt b/app/src/test/java/com/m57/hermescontrol/ui/keys/KeysViewModelTest.kt new file mode 100644 index 00000000..1a132225 --- /dev/null +++ b/app/src/test/java/com/m57/hermescontrol/ui/keys/KeysViewModelTest.kt @@ -0,0 +1,57 @@ +package com.m57.hermescontrol.ui.keys + +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.setMain +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test + +@OptIn(ExperimentalCoroutinesApi::class) +class KeysViewModelTest { + private val testDispatcher = StandardTestDispatcher() + + private fun createViewModel(): KeysViewModel { + val vm = KeysViewModel() + testDispatcher.scheduler.advanceUntilIdle() + return vm + } + + @Before + fun setUp() { + Dispatchers.setMain(testDispatcher) + } + + @After + fun tearDown() { + Dispatchers.resetMain() + } + + @Test + fun `setNewKeyName updates state flow correctly`() { + val viewModel = createViewModel() + + assertEquals("", viewModel.uiState.value.newKeyName) + + viewModel.setNewKeyName("MY_NEW_API_KEY") + testDispatcher.scheduler.advanceUntilIdle() + + assertEquals("MY_NEW_API_KEY", viewModel.uiState.value.newKeyName) + } + + @Test + fun `setNewKeyValue updates state flow correctly`() { + val viewModel = createViewModel() + + assertEquals("", viewModel.uiState.value.newKeyValue) + + viewModel.setNewKeyValue("dummy_token_value_that_is_long_enough") + testDispatcher.scheduler.advanceUntilIdle() + + assertEquals("dummy_token_value_that_is_long_enough", viewModel.uiState.value.newKeyValue) + } +}