From cfe660e0cf3911e13dd8e51e61d45d251579d2ac Mon Sep 17 00:00:00 2001 From: saber safavi Date: Thu, 12 Oct 2023 11:06:37 +0200 Subject: [PATCH] Fix Voucher redeemer test issue --- .../viewmodel/VoucherDialogViewModel.kt | 3 ++- .../viewmodel/VoucherDialogViewModelTest.kt | 23 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt index 07c56ff954c4..02a0f8e98861 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt @@ -33,7 +33,8 @@ class VoucherDialogViewModel( private val vmState = MutableStateFlow(VoucherDialogState.Default) private val voucherInput = MutableStateFlow(LoginUiState.INITIAL.accountNumberInput) - private lateinit var voucherRedeemer: VoucherRedeemer + @Suppress("konsist.ensure public properties use permitted names") + lateinit var voucherRedeemer: VoucherRedeemer private val _shared: SharedFlow = serviceConnectionManager.connectionState .flatMapLatest { state -> diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt index bf87799d876d..6df842933bd2 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt @@ -7,22 +7,27 @@ import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic import io.mockk.unmockkAll +import kotlin.test.assertIs +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest +import net.mullvad.mullvadvpn.compose.state.VoucherDialogState import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule import net.mullvad.mullvadvpn.model.VoucherSubmissionError import net.mullvad.mullvadvpn.model.VoucherSubmissionResult import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionContainer import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager +import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState import net.mullvad.mullvadvpn.ui.serviceconnection.VoucherRedeemer import org.junit.After import org.junit.Before -import org.junit.Ignore import org.junit.Rule import org.junit.Test class VoucherDialogViewModelTest { @get:Rule val testCoroutineRule = TestCoroutineRule() + private val serviceConnectionState = + MutableStateFlow(ServiceConnectionState.Disconnected) private val mockServiceConnectionManager: ServiceConnectionManager = mockk() private val mockVoucherRedeemer: VoucherRedeemer = mockk() private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk() @@ -36,8 +41,7 @@ class VoucherDialogViewModelTest { @Before fun setUp() { mockkStatic(CACHE_EXTENSION_CLASS) - every { mockServiceConnectionManager.connectionState.value.readyContainer() } returns - mockServiceConnectionContainer + every { mockServiceConnectionManager.connectionState } returns serviceConnectionState every { mockServiceConnectionContainer.voucherRedeemer } returns mockVoucherRedeemer viewModel = @@ -53,21 +57,24 @@ class VoucherDialogViewModelTest { } @Test - @Ignore("TODO: Fix this failing test and then enable it again.") - fun test_submit_invalid_voucher() = runTest { - val voucher = DUMMY_VALID_VOUCHER + fun test_submit_voucher() = runTest { + val voucher = DUMMY_INVALID_VOUCHER val dummyStringResource = DUMMY_STRING_RESOURCE // Arrange + serviceConnectionState.value = + ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer) + viewModel.voucherRedeemer = mockVoucherRedeemer every { mockResources.getString(any()) } returns dummyStringResource coEvery { mockVoucherRedeemer.submit(voucher) } returns mockVoucherSubmissionErrorResult // Act, Assert + assertIs(viewModel.uiState.value.voucherViewModelState) viewModel.onRedeem(voucher) coVerify(exactly = 1) { mockVoucherRedeemer.submit(voucher) } } companion object { private const val CACHE_EXTENSION_CLASS = "net.mullvad.mullvadvpn.util.CacheExtensionsKt" - private const val DUMMY_VALID_VOUCHER = "DUMMY_VALID_VOUCHER" - private const val DUMMY_STRING_RESOURCE = "DUMMY_STRING_RESOURCE" + private const val DUMMY_INVALID_VOUCHER = "dummy_invalid_voucher" + private const val DUMMY_STRING_RESOURCE = "dummy_string_resource" } }