From fcb25d3b55515955751cd013783d13d071b5bea8 Mon Sep 17 00:00:00 2001 From: M57 <41645758+Hy4ri@users.noreply.github.com> Date: Thu, 9 Jul 2026 17:37:05 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Test=20PairingCodeEntryViewModel?= =?UTF-8?q?=20onManualCodeChange=20(#512)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- .../pairing/PairingCodeEntryViewModelTest.kt | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/src/test/java/com/m57/hermescontrol/ui/pairing/PairingCodeEntryViewModelTest.kt diff --git a/app/src/test/java/com/m57/hermescontrol/ui/pairing/PairingCodeEntryViewModelTest.kt b/app/src/test/java/com/m57/hermescontrol/ui/pairing/PairingCodeEntryViewModelTest.kt new file mode 100644 index 00000000..00ed7270 --- /dev/null +++ b/app/src/test/java/com/m57/hermescontrol/ui/pairing/PairingCodeEntryViewModelTest.kt @@ -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) + } +}