Skip to content

Commit

Permalink
Add test for dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
sabercodic committed Oct 10, 2023
1 parent 0a24c8a commit ad5a463
Showing 1 changed file with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package net.mullvad.mullvadvpn.viewmodel

import android.content.res.Resources
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll
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.VoucherRedeemer
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class VoucherDialogViewModelTest {
@get:Rule val testCoroutineRule = TestCoroutineRule()

private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private val mockVoucherRedeemer: VoucherRedeemer = mockk()
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
private val mockResources: Resources = mockk()

private val mockVoucherSubmissionErrorResult: VoucherSubmissionResult =
VoucherSubmissionResult.Error(VoucherSubmissionError.OtherError)

private lateinit var viewModel: VoucherDialogViewModel

@Before
fun setUp() {
mockkStatic(CACHE_EXTENSION_CLASS)
every { mockServiceConnectionManager.connectionState.value.readyContainer() } returns
mockServiceConnectionContainer
every { mockServiceConnectionContainer.voucherRedeemer } returns mockVoucherRedeemer

viewModel =
VoucherDialogViewModel(
serviceConnectionManager = mockServiceConnectionManager,
resources = mockResources
)
}

@After
fun tearDown() {
unmockkAll()
}

@Test
fun test_submit_invalid_voucher() = runTest {
val voucher = DUMMY_VALID_VOUCHER
val dummyStringResource = DUMMY_STRING_RESOURCE
// Arrange
every { mockResources.getString(any()) } returns dummyStringResource
coEvery { mockVoucherRedeemer.submit(voucher) } returns mockVoucherSubmissionErrorResult
// Act, Assert
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"
}
}

0 comments on commit ad5a463

Please sign in to comment.