Skip to content

Commit

Permalink
Add select location screen and viewmodel tests
Browse files Browse the repository at this point in the history
Co-Authored-By: Boban Sijuk <[email protected]>
  • Loading branch information
MaryamShaghaghi and Boki91 committed Dec 1, 2023
1 parent b89d7b9 commit 0ef08a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class SelectLocationScreenTest {
uiState =
SelectLocationUiState.ShowData(
countries = DUMMY_RELAY_COUNTRIES,
selectedRelay = null
selectedRelay = null,
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
),
uiCloseAction = MutableSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
Expand Down Expand Up @@ -93,7 +96,10 @@ class SelectLocationScreenTest {
uiState =
SelectLocationUiState.ShowData(
countries = updatedDummyList,
selectedRelay = updatedDummyList[0].cities[0].relays[0]
selectedRelay = updatedDummyList[0].cities[0].relays[0],
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
),
uiCloseAction = MutableSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow()
Expand All @@ -118,7 +124,13 @@ class SelectLocationScreenTest {
composeTestRule.setContentWithTheme {
SelectLocationScreen(
uiState =
SelectLocationUiState.ShowData(countries = emptyList(), selectedRelay = null),
SelectLocationUiState.ShowData(
countries = emptyList(),
selectedRelay = null,
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
),
uiCloseAction = MutableSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow(),
onSearchTermInput = mockedSearchTermInput
Expand All @@ -140,7 +152,14 @@ class SelectLocationScreenTest {
val mockSearchString = "SEARCH"
composeTestRule.setContentWithTheme {
SelectLocationScreen(
uiState = SelectLocationUiState.NoSearchResultFound(searchTerm = mockSearchString),
uiState =
SelectLocationUiState.ShowData(
countries = emptyList(),
selectedRelay = null,
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = mockSearchString
),
uiCloseAction = MutableSharedFlow(),
enterTransitionEndAction = MutableSharedFlow<Unit>().asSharedFlow(),
onSearchTermInput = mockedSearchTermInput
Expand Down Expand Up @@ -187,7 +206,7 @@ class SelectLocationScreenTest {
private val DUMMY_RELAY_COUNTRIES =
RelayList(
arrayListOf(DUMMY_RELAY_COUNTRY_1, DUMMY_RELAY_COUNTRY_2),
DUMMY_WIREGUARD_ENDPOINT_DATA
DUMMY_WIREGUARD_ENDPOINT_DATA,
)
.toRelayCountries(ownership = Constraint.Any(), providers = Constraint.Any())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.lib.common.test.assertLists
import net.mullvad.mullvadvpn.model.Constraint
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint
import net.mullvad.mullvadvpn.model.Ownership
import net.mullvad.mullvadvpn.model.Providers
import net.mullvad.mullvadvpn.relaylist.Provider
import net.mullvad.mullvadvpn.relaylist.RelayCountry
import net.mullvad.mullvadvpn.relaylist.RelayItem
import net.mullvad.mullvadvpn.relaylist.RelayList
import net.mullvad.mullvadvpn.relaylist.filterOnSearchTerm
import net.mullvad.mullvadvpn.ui.serviceconnection.ConnectionProxy
import net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManager
import net.mullvad.mullvadvpn.ui.serviceconnection.connectionProxy
import net.mullvad.mullvadvpn.usecase.RelayListFilterUseCase
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
import org.junit.After
import org.junit.Before
Expand All @@ -32,21 +37,31 @@ import org.junit.Test
class SelectLocationViewModelTest {
@get:Rule val testCoroutineRule = TestCoroutineRule()

private val mockRelayListFilterUseCase: RelayListFilterUseCase = mockk(relaxed = true)
private val mockServiceConnectionManager: ServiceConnectionManager = mockk()
private lateinit var viewModel: SelectLocationViewModel

private val relayListWithSelectionFlow = MutableStateFlow(RelayList(emptyList(), null))

private val mockRelayListUseCase: RelayListUseCase = mockk()
private val selectedOwnership = MutableStateFlow<Constraint<Ownership>>(Constraint.Any())
private val selectedProvider = MutableStateFlow<Constraint<Providers>>(Constraint.Any())
private val allProvider = MutableStateFlow<List<Provider>>(emptyList())

@Before
fun setup() {

every { mockRelayListFilterUseCase.selectedOwnership() } returns selectedOwnership
every { mockRelayListFilterUseCase.selectedProviders() } returns selectedProvider
every { mockRelayListFilterUseCase.availableProviders() } returns allProvider
every { mockRelayListUseCase.relayListWithSelection() } returns relayListWithSelectionFlow

mockkStatic(SERVICE_CONNECTION_MANAGER_EXTENSIONS)
mockkStatic(RELAY_LIST_EXTENSIONS)

viewModel = SelectLocationViewModel(mockServiceConnectionManager, mockRelayListUseCase)
viewModel =
SelectLocationViewModel(
mockServiceConnectionManager,
mockRelayListUseCase,
mockRelayListFilterUseCase
)
}

@After
Expand Down Expand Up @@ -164,11 +179,47 @@ class SelectLocationViewModelTest {

// Assert
val actualState = awaitItem()
assertIs<SelectLocationUiState.NoSearchResultFound>(actualState)
assertIs<SelectLocationUiState.ShowData>(actualState)
assertEquals(mockSearchString, actualState.searchTerm)
}
}

@Test
fun testRemoveOwnerFilter() = runTest {
// Arrange
val mockSelectedProviders: Constraint<Providers> = mockk()
every { mockRelayListFilterUseCase.selectedProviders() } returns
MutableStateFlow(mockSelectedProviders)

// Act
viewModel.removeOwnerFilter()
// Assert
verify {
mockRelayListFilterUseCase.updateOwnershipAndProviderFilter(
any<Constraint.Any<Ownership>>(),
mockSelectedProviders
)
}
}

@Test
fun testRemoveProviderFilter() = runTest {
// Arrange
val mockSelectedOwnership: Constraint<Ownership> = mockk()
every { mockRelayListFilterUseCase.selectedOwnership() } returns
MutableStateFlow(mockSelectedOwnership)

// Act
viewModel.removeProviderFilter()
// Assert
verify {
mockRelayListFilterUseCase.updateOwnershipAndProviderFilter(
mockSelectedOwnership,
any<Constraint.Any<Providers>>()
)
}
}

companion object {
private const val SERVICE_CONNECTION_MANAGER_EXTENSIONS =
"net.mullvad.mullvadvpn.ui.serviceconnection.ServiceConnectionManagerExtensionsKt"
Expand Down

0 comments on commit 0ef08a1

Please sign in to comment.