From 8ac47d0b578b54acc7b30f2609bb4638d291cb8d 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 --- .../mullvadvpn/viewmodel/VoucherDialogViewModel.kt | 2 +- .../viewmodel/VoucherDialogViewModelTest.kt | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 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..a0e24c30fffb 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,7 @@ class VoucherDialogViewModel( private val vmState = MutableStateFlow(VoucherDialogState.Default) private val voucherInput = MutableStateFlow(LoginUiState.INITIAL.accountNumberInput) - private lateinit var voucherRedeemer: VoucherRedeemer + 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..000ec4177b05 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,25 @@ import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic import io.mockk.unmockkAll +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.test.runTest 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 +39,9 @@ class VoucherDialogViewModelTest { @Before fun setUp() { mockkStatic(CACHE_EXTENSION_CLASS) - every { mockServiceConnectionManager.connectionState.value.readyContainer() } returns - mockServiceConnectionContainer + every { mockServiceConnectionManager.connectionState } returns serviceConnectionState +// every { mockServiceConnectionManager.connectionState.value.readyContainer() } returns +// mockServiceConnectionContainer every { mockServiceConnectionContainer.voucherRedeemer } returns mockVoucherRedeemer viewModel = @@ -53,11 +57,13 @@ 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 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