Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Dec 7, 2023
1 parent f1a58f2 commit 05b5dcf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ fun MullvadApp() {
LaunchedEffect(Unit) {
serviceVm.sideEffect.collect {
when (it) {
DaemonScreenEvent.Show -> navController.navigate(NoDaemonScreenDestination) {
launchSingleTop = true
}
DaemonScreenEvent.Show ->
navController.navigate(NoDaemonScreenDestination) { launchSingleTop = true }
DaemonScreenEvent.Remove ->
navController.popBackStack(NoDaemonScreenDestination, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionState
import net.mullvad.mullvadvpn.ui.serviceconnection.authTokenCache
import net.mullvad.mullvadvpn.ui.serviceconnection.connectionProxy
import net.mullvad.mullvadvpn.usecase.OutOfTimeUseCase
import net.mullvad.mullvadvpn.usecase.PaymentUseCase
import net.mullvad.talpid.util.EventNotifier
import org.joda.time.DateTime
Expand All @@ -51,6 +52,7 @@ class OutOfTimeViewModelTest {
private val deviceState = MutableStateFlow<DeviceState>(DeviceState.Initial)
private val paymentAvailability = MutableStateFlow<PaymentAvailability?>(null)
private val purchaseResult = MutableStateFlow<PurchaseResult?>(null)
private val outOfTime = MutableStateFlow(true)

// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
Expand All @@ -63,6 +65,7 @@ class OutOfTimeViewModelTest {
private val mockDeviceRepository: DeviceRepository = mockk()
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private val mockPaymentUseCase: PaymentUseCase = mockk(relaxed = true)
private val mockOutOfTimeUseCase: OutOfTimeUseCase = mockk(relaxed = true)

private lateinit var viewModel: OutOfTimeViewModel

Expand All @@ -85,18 +88,24 @@ class OutOfTimeViewModelTest {

coEvery { mockPaymentUseCase.paymentAvailability } returns paymentAvailability

coEvery { mockOutOfTimeUseCase.isOutOfTime() } returns outOfTime

viewModel =
OutOfTimeViewModel(
accountRepository = mockAccountRepository,
serviceConnectionManager = mockServiceConnectionManager,
deviceRepository = mockDeviceRepository,
paymentUseCase = mockPaymentUseCase,
outOfTimeUseCase = mockOutOfTimeUseCase,
pollAccountExpiry = false
)

viewModel.start()
}

@After
fun tearDown() {
viewModel.stop()
viewModel.viewModelScope.coroutineContext.cancel()
unmockkAll()
}
Expand Down Expand Up @@ -145,7 +154,7 @@ class OutOfTimeViewModelTest {

// Act, Assert
viewModel.uiSideEffect.test {
accountExpiryState.value = AccountExpiry.Available(mockExpiryDate)
outOfTime.value = false
val action = awaitItem()
assertIs<OutOfTimeViewModel.UiSideEffect.OpenConnectScreen>(action)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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.authTokenCache
import net.mullvad.mullvadvpn.usecase.OutOfTimeUseCase
import net.mullvad.mullvadvpn.usecase.PaymentUseCase
import net.mullvad.talpid.util.EventNotifier
import org.joda.time.DateTime
Expand All @@ -50,6 +51,7 @@ class WelcomeViewModelTest {
private val accountExpiryState = MutableStateFlow<AccountExpiry>(AccountExpiry.Missing)
private val purchaseResult = MutableStateFlow<PurchaseResult?>(null)
private val paymentAvailability = MutableStateFlow<PaymentAvailability?>(null)
private val outOfTime = MutableStateFlow(true)

// Service connections
private val mockServiceConnectionContainer: ServiceConnectionContainer = mockk()
Expand All @@ -62,6 +64,7 @@ class WelcomeViewModelTest {
private val mockDeviceRepository: DeviceRepository = mockk()
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private val mockPaymentUseCase: PaymentUseCase = mockk(relaxed = true)
private val mockOutOfTimeUseCase: OutOfTimeUseCase = mockk(relaxed = true)

private lateinit var viewModel: WelcomeViewModel

Expand All @@ -84,19 +87,24 @@ class WelcomeViewModelTest {

coEvery { mockPaymentUseCase.paymentAvailability } returns paymentAvailability

coEvery { mockOutOfTimeUseCase.isOutOfTime() } returns outOfTime

viewModel =
WelcomeViewModel(
accountRepository = mockAccountRepository,
deviceRepository = mockDeviceRepository,
serviceConnectionManager = mockServiceConnectionManager,
paymentUseCase = mockPaymentUseCase,
outOfTimeUseCase = mockOutOfTimeUseCase,
pollAccountExpiry = false
)
viewModel.start()
}

@After
fun tearDown() {
viewModel.viewModelScope.coroutineContext.cancel()
viewModel.stop()
unmockkAll()
}

Expand Down Expand Up @@ -166,7 +174,7 @@ class WelcomeViewModelTest {

// Act, Assert
viewModel.uiSideEffect.test {
accountExpiryState.value = AccountExpiry.Available(mockExpiryDate)
outOfTime.value = false
val action = awaitItem()
assertIs<WelcomeViewModel.UiSideEffect.OpenConnectScreen>(action)
}
Expand Down

0 comments on commit 05b5dcf

Please sign in to comment.