🧪 Test PairingCodeEntryViewModel onManualCodeChange (#512)

* Test PairingCodeEntryViewModel onManualCodeChange

Added unit tests to cover the `onManualCodeChange` function in `PairingCodeEntryViewModel`.
The test validates that the function properly updates the state and handles string trimming
as well as error message clearance.

Changes include:
- Created `PairingCodeEntryViewModelTest.kt` in `app/src/test/java/com/m57/hermescontrol/ui/pairing/`
- Implemented `testOnManualCodeChange_updatesStateAndClearsError` to check correct state update logic and verify error clear functionality.
- Implemented `testOnManualCodeChange_trimsInput` to verify input is properly trimmed.
- Added necessary mock setup to deal with Android components (`Application` and `android.util.Log`).

* Fix ktlint format issue in PairingCodeEntryViewModelTest

Ran `ktlint --format` to fix the `standard:no-empty-first-line-in-class-body`
formatting violation introduced in the previous commit.
This commit is contained in:
M57 authored and GitHub committed 2026-07-09 17:37:05 +03:00
1 parent 529ab0bc83
commit fcb25d3b55
1 file changed
+55
@@ -0,0 +1,55 @@
package com.m57.hermescontrol.ui.pairing
import android.app.Application
import android.util.Log
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
class PairingCodeEntryViewModelTest {
private val mockApp: Application = mockk(relaxed = true)
@Before
fun setup() {
mockkStatic(Log::class)
every { Log.d(any(), any()) } returns 0
every { Log.e(any(), any(), any()) } returns 0
}
@After
fun teardown() {
unmockkAll()
}
@Test
fun testOnManualCodeChange_updatesStateAndClearsError() {
val viewModel = PairingCodeEntryViewModel(mockApp)
// Force an error to verify it gets cleared
// using onCodeDetected with invalid input to set the error
viewModel.onCodeDetected("invalid-code")
assertEquals(true, viewModel.uiState.value.errorMessage != null)
viewModel.onManualCodeChange("new-code")
val state = viewModel.uiState.value
assertEquals("new-code", state.manualCode)
assertNull(state.errorMessage)
}
@Test
fun testOnManualCodeChange_trimsInput() {
val viewModel = PairingCodeEntryViewModel(mockApp)
viewModel.onManualCodeChange(" trimmed-code \n")
val state = viewModel.uiState.value
assertEquals("trimmed-code", state.manualCode)
}
}