Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-mullvad committed Jun 18, 2024
1 parent 42c63d7 commit 6630ce1
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ class InAppNotificationControllerTest {
val newDeviceNotificationUseCase: NewDeviceNotificationUseCase = mockk()
val versionNotificationUseCase: VersionNotificationUseCase = mockk()
val tunnelStateNotificationUseCase: TunnelStateNotificationUseCase = mockk()
every { accountExpiryNotificationUseCase.notifications() } returns
accountExpiryNotifications
every { newDeviceNotificationUseCase.notifications() } returns newDeviceNotifications
every { versionNotificationUseCase.notifications() } returns versionNotifications
every { tunnelStateNotificationUseCase.notifications() } returns tunnelStateNotifications
every { accountExpiryNotificationUseCase.invoke() } returns accountExpiryNotifications
every { newDeviceNotificationUseCase.invoke() } returns newDeviceNotifications
every { versionNotificationUseCase.invoke() } returns versionNotifications
every { tunnelStateNotificationUseCase.invoke() } returns tunnelStateNotifications
job = Job()

inAppNotificationController =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package net.mullvad.mullvadvpn.usecase
import app.cash.turbine.test
import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.mockk
import io.mockk.impl.annotations.MockK
import io.mockk.unmockkAll
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand All @@ -17,14 +17,15 @@ import net.mullvad.mullvadvpn.lib.model.DeviceId
import net.mullvad.mullvadvpn.lib.model.DeviceState
import net.mullvad.mullvadvpn.lib.shared.DeviceRepository
import net.mullvad.mullvadvpn.repository.InAppNotification
import net.mullvad.mullvadvpn.repository.NewDeviceRepository
import org.joda.time.DateTime
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(TestCoroutineRule::class)
class NewDeviceUseNotificationCaseTest {
class NewDeviceNotificationUseCaseTest {

private val deviceName = "Frank Zebra"
private val deviceState =
Expand All @@ -38,16 +39,26 @@ class NewDeviceUseNotificationCaseTest {
)
)
)
private val isNewDeviceState = MutableStateFlow(false)

private lateinit var newDeviceNotificationUseCase: NewDeviceNotificationUseCase

@MockK lateinit var mockDeviceRepository: DeviceRepository

@MockK lateinit var mockNewDeviceRepository: NewDeviceRepository

@BeforeEach
fun setup() {
MockKAnnotations.init(this)

val mockDeviceRepository: DeviceRepository = mockk()
every { mockNewDeviceRepository.isNewDevice } returns isNewDeviceState
every { mockDeviceRepository.deviceState } returns deviceState

newDeviceNotificationUseCase =
NewDeviceNotificationUseCase(deviceRepository = mockDeviceRepository)
NewDeviceNotificationUseCase(
newDeviceRepository = mockNewDeviceRepository,
deviceRepository = mockDeviceRepository
)
}

@AfterEach
Expand All @@ -62,26 +73,27 @@ class NewDeviceUseNotificationCaseTest {
}

@Test
fun `when newDeviceCreated is called notifications should emit NewDevice notification containing device name`() =
runTest {
newDeviceNotificationUseCase().test {
// Arrange, Act
awaitItem()
newDeviceNotificationUseCase.newDeviceCreated()

// Assert
assertEquals(awaitItem(), listOf(InAppNotification.NewDevice(deviceName)))
}
fun `when a new device has been created a device notification should be emitted`() = runTest {
newDeviceNotificationUseCase().test {
// Arrange, Act
awaitItem()
newDeviceNotificationUseCase.invoke()
isNewDeviceState.value = true

// Assert
assertEquals(awaitItem(), listOf(InAppNotification.NewDevice(deviceName)))
}
}

@Test
fun `clearNewDeviceCreatedNotification should clear notifications`() = runTest {
fun `when a device is unmarked as new the device notification should be cleared`() = runTest {
// Arrange
isNewDeviceState.value = true

// Act
newDeviceNotificationUseCase().test {
// Arrange, Act
awaitItem()
newDeviceNotificationUseCase.newDeviceCreated()
awaitItem()
newDeviceNotificationUseCase.clearNewDeviceCreatedNotification()
isNewDeviceState.value = false

// Assert
assertEquals(awaitItem(), emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,15 @@ class ConnectViewModelTest {
every { mockLocation.country } returns "dummy country"

// Flows
every { mockSelectedLocationTitleUseCase.selectedLocationTitle() } returns
selectedRelayItemFlow
every { mockSelectedLocationTitleUseCase() } returns selectedRelayItemFlow

every { outOfTimeUseCase.isOutOfTime } returns outOfTimeViewFlow
viewModel =
ConnectViewModel(
accountRepository = mockAccountRepository,
deviceRepository = mockDeviceRepository,
inAppNotificationController = mockInAppNotificationController,
newDeviceNotificationUseCase = mockk(),
newDeviceRepository = mockk(),
outOfTimeUseCase = outOfTimeUseCase,
paymentUseCase = mockPaymentUseCase,
selectedLocationTitleUseCase = mockSelectedLocationTitleUseCase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ class CreateCustomListDialogViewModelTest {
val expectedResult: Created = mockk()
val customListName = "list"
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Create>())
} returns expectedResult.right()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
expectedResult.right()
every { expectedResult.locationNames } returns listOf("locationName")

// Act, Assert
Expand All @@ -61,9 +60,8 @@ class CreateCustomListDialogViewModelTest {
undo = CustomListAction.Delete(createdId)
)
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Create>())
} returns expectedResult.right()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
expectedResult.right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand All @@ -82,9 +80,8 @@ class CreateCustomListDialogViewModelTest {
val expectedError = CreateWithLocationsError.Create(CustomListAlreadyExists)
val customListName = "list"
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Create>())
} returns expectedError.left()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
Expand All @@ -101,9 +98,8 @@ class CreateCustomListDialogViewModelTest {
val expectedError = CreateWithLocationsError.Create(CustomListAlreadyExists)
val customListName = "list"
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Create>())
} returns expectedError.left()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class CustomListLocationsViewModelTest {
@BeforeEach
fun setup() {
every { mockRelayListRepository.relayList } returns relayListFlow
every { mockCustomListRelayItemsUseCase.getRelayItemLocationsForCustomList(any()) } returns
selectedLocationsFlow
every { mockCustomListRelayItemsUseCase(any()) } returns selectedLocationsFlow
}

@Test
Expand Down Expand Up @@ -185,9 +184,8 @@ class CustomListLocationsViewModelTest {
val customListId = CustomListId("1")
val newList = true
val expectedResult: LocationsChanged = mockk()
coEvery {
mockCustomListUseCase.performAction(any<CustomListAction.UpdateLocations>())
} returns expectedResult.right()
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()
val viewModel = createViewModel(customListId, newList)

// Act, Assert
Expand All @@ -205,9 +203,8 @@ class CustomListLocationsViewModelTest {
val customListId = CustomListId("1")
val newList = false
val expectedResult: LocationsChanged = mockk()
coEvery {
mockCustomListUseCase.performAction(any<CustomListAction.UpdateLocations>())
} returns expectedResult.right()
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()
val viewModel = createViewModel(customListId, newList)

// Act, Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class CustomListsViewModelTest {
viewModel.undoDeleteCustomList(action)

// Assert
coVerify { mockCustomListsActionUseCase.performAction(action) }
coVerify { mockCustomListsActionUseCase(action) }
}

private fun createViewModel() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class DeleteCustomListConfirmationViewModelTest {
// Arrange
val expectedResult: Deleted = mockk()
val viewModel = createViewModel()
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Delete>())
} returns expectedResult.right()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Delete>()) } returns
expectedResult.right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class EditCustomListNameDialogViewModelTest {
val customListId = CustomListId("id")
val customListName = "list"
val viewModel = createViewModel(customListId, customListName)
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Rename>())
} returns expectedResult.right()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedResult.right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand All @@ -51,9 +50,8 @@ class EditCustomListNameDialogViewModelTest {
val customListName = "list2"
val expectedError = RenameError(NameAlreadyExists(customListName))
val viewModel = createViewModel(customListId, customListName)
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Rename>())
} returns expectedError.left()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
Expand All @@ -71,9 +69,8 @@ class EditCustomListNameDialogViewModelTest {
val customListName = "list"
val expectedError = RenameError(NameAlreadyExists(customListName))
val viewModel = createViewModel(customListId, customListName)
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.Rename>())
} returns expectedError.left()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedError.left()

// Act, Assert
viewModel.uiState.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class FilterViewModelTest {
@BeforeEach
fun setup() {
every { mockRelayListFilterRepository.selectedOwnership } returns selectedOwnership
every { mockAvailableProvidersUseCase.availableProviders() } returns
flowOf(dummyListOfAllProviders)
every { mockAvailableProvidersUseCase() } returns flowOf(dummyListOfAllProviders)
every { mockRelayListFilterRepository.selectedProviders } returns
MutableStateFlow(
Constraint.Only(Providers(mockSelectedProviders.map { it.providerId }.toSet()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import net.mullvad.mullvadvpn.lib.model.AccountNumber
import net.mullvad.mullvadvpn.lib.model.LoginAccountError
import net.mullvad.mullvadvpn.lib.shared.AccountRepository
import net.mullvad.mullvadvpn.usecase.InternetAvailableUseCase
import net.mullvad.mullvadvpn.usecase.NewDeviceNotificationUseCase
import org.joda.time.DateTime
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
Expand All @@ -40,7 +39,6 @@ class LoginViewModelTest {

@MockK private lateinit var connectivityUseCase: InternetAvailableUseCase
@MockK private lateinit var mockedAccountRepository: AccountRepository
@MockK private lateinit var mockedNewDeviceNotificationUseCase: NewDeviceNotificationUseCase

private lateinit var loginViewModel: LoginViewModel

Expand All @@ -49,13 +47,12 @@ class LoginViewModelTest {
Dispatchers.setMain(UnconfinedTestDispatcher())
MockKAnnotations.init(this, relaxUnitFun = true)
every { connectivityUseCase() } returns true
every { mockedNewDeviceNotificationUseCase.newDeviceCreated() } returns Unit
coEvery { mockedAccountRepository.fetchAccountHistory() } returns null

loginViewModel =
LoginViewModel(
accountRepository = mockedAccountRepository,
newDeviceNotificationUseCase = mockedNewDeviceNotificationUseCase,
newDeviceRepository = mockk(relaxUnitFun = true),
internetAvailableUseCase = connectivityUseCase,
UnconfinedTestDispatcher()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,10 @@ class SelectLocationViewModelTest {

every { mockRelayListFilterRepository.selectedOwnership } returns selectedOwnership
every { mockRelayListFilterRepository.selectedProviders } returns selectedProviders
every { mockAvailableProvidersUseCase.availableProviders() } returns allProviders
every { mockAvailableProvidersUseCase() } returns allProviders
every { mockRelayListRepository.selectedLocation } returns selectedRelayItemFlow
every { mockFilteredRelayListUseCase.filteredRelayList() } returns filteredRelayList
every { mockCustomListsRelayItemUseCase.relayItemCustomLists() } returns
customRelayListItems
every { mockFilteredRelayListUseCase() } returns filteredRelayList
every { mockCustomListsRelayItemUseCase() } returns customRelayListItems

mockkStatic(RELAY_LIST_EXTENSIONS)
mockkStatic(RELAY_ITEM_EXTENSIONS)
Expand Down Expand Up @@ -247,7 +246,7 @@ class SelectLocationViewModelTest {
viewModel.performAction(action)

// Assert
coVerify { mockCustomListActionUseCase.performAction(action) }
coVerify { mockCustomListActionUseCase(action) }
}

@Test
Expand All @@ -265,9 +264,8 @@ class SelectLocationViewModelTest {
locations = emptyList(),
expanded = false
)
coEvery {
mockCustomListActionUseCase.performAction(any<CustomListAction.UpdateLocations>())
} returns expectedResult.right()
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ class VpnSettingsViewModelTest {
runTest {
val systemVpnSettingsAvailable = true

every { mockSystemVpnSettingsUseCase.systemVpnSettingsAvailable() } returns
systemVpnSettingsAvailable
every { mockSystemVpnSettingsUseCase() } returns systemVpnSettingsAvailable

viewModel.uiState.test {
assertEquals(systemVpnSettingsAvailable, awaitItem().systemVpnSettingsAvailable)
Expand Down

0 comments on commit 6630ce1

Please sign in to comment.