Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jan 19, 2024
1 parent 93eb40e commit 7efda2d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.mockk.MockKAnnotations
import io.mockk.mockk
import io.mockk.verify
import net.mullvad.mullvadvpn.compose.setContentWithTheme
import net.mullvad.mullvadvpn.compose.state.RelayListState
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
import net.mullvad.mullvadvpn.compose.test.CIRCULAR_PROGRESS_INDICATOR
import net.mullvad.mullvadvpn.model.Constraint
Expand Down Expand Up @@ -54,9 +55,12 @@ class SelectLocationScreenTest {
setContentWithTheme {
SelectLocationScreen(
uiState =
SelectLocationUiState.ShowData(
countries = DUMMY_RELAY_COUNTRIES,
selectedRelay = null,
SelectLocationUiState.Data(
relayListState =
RelayListState.RelayList(
countries = DUMMY_RELAY_COUNTRIES,
selectedRelay = null
),
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
Expand Down Expand Up @@ -91,9 +95,12 @@ class SelectLocationScreenTest {
setContentWithTheme {
SelectLocationScreen(
uiState =
SelectLocationUiState.ShowData(
countries = updatedDummyList,
selectedRelay = updatedDummyList[0].cities[0].relays[0],
SelectLocationUiState.Data(
relayListState =
RelayListState.RelayList(
countries = updatedDummyList,
selectedRelay = updatedDummyList[0].cities[0].relays[0]
),
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
Expand All @@ -118,9 +125,12 @@ class SelectLocationScreenTest {
setContentWithTheme {
SelectLocationScreen(
uiState =
SelectLocationUiState.ShowData(
countries = emptyList(),
selectedRelay = null,
SelectLocationUiState.Data(
relayListState =
RelayListState.RelayList(
countries = emptyList(),
selectedRelay = null
),
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = ""
Expand All @@ -146,9 +156,8 @@ class SelectLocationScreenTest {
setContentWithTheme {
SelectLocationScreen(
uiState =
SelectLocationUiState.ShowData(
countries = emptyList(),
selectedRelay = null,
SelectLocationUiState.Data(
relayListState = RelayListState.Empty,
selectedOwnership = null,
selectedProvidersCount = 0,
searchTerm = mockSearchString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package net.mullvad.mullvadvpn.viewmodel
import androidx.lifecycle.viewModelScope
import app.cash.turbine.test
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.runs
import io.mockk.unmockkAll
import io.mockk.verify
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.state.RelayListState
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.lib.common.test.assertLists
Expand Down Expand Up @@ -53,6 +56,7 @@ class SelectLocationViewModelTest {
every { mockRelayListFilterUseCase.selectedProviders() } returns selectedProvider
every { mockRelayListFilterUseCase.availableProviders() } returns allProvider
every { mockRelayListUseCase.relayListWithSelection() } returns relayListWithSelectionFlow
every { mockRelayListUseCase.fetchRelayList() } just runs

mockkStatic(SERVICE_CONNECTION_MANAGER_EXTENSIONS)
mockkStatic(RELAY_LIST_EXTENSIONS)
Expand Down Expand Up @@ -86,9 +90,16 @@ class SelectLocationViewModelTest {
// Act, Assert
viewModel.uiState.test {
val actualState = awaitItem()
assertIs<SelectLocationUiState.ShowData>(actualState)
assertLists(mockCountries, actualState.countries)
assertEquals(selectedRelay, actualState.selectedRelay)
assertIs<SelectLocationUiState.Data>(actualState)
assertIs<RelayListState.RelayList>(actualState.relayListState)
assertLists(
mockCountries,
(actualState.relayListState as RelayListState.RelayList).countries
)
assertEquals(
selectedRelay,
(actualState.relayListState as RelayListState.RelayList).selectedRelay
)
}
}

Expand All @@ -103,9 +114,16 @@ class SelectLocationViewModelTest {
// Act, Assert
viewModel.uiState.test {
val actualState = awaitItem()
assertIs<SelectLocationUiState.ShowData>(actualState)
assertLists(mockCountries, actualState.countries)
assertEquals(selectedRelay, actualState.selectedRelay)
assertIs<SelectLocationUiState.Data>(actualState)
assertIs<RelayListState.RelayList>(actualState.relayListState)
assertLists(
mockCountries,
(actualState.relayListState as RelayListState.RelayList).countries
)
assertEquals(
selectedRelay,
(actualState.relayListState as RelayListState.RelayList).selectedRelay
)
}
}

Expand Down Expand Up @@ -145,16 +163,23 @@ class SelectLocationViewModelTest {
// Act, Assert
viewModel.uiState.test {
// Wait for first data
assertIs<SelectLocationUiState.ShowData>(awaitItem())
assertIs<SelectLocationUiState.Data>(awaitItem())

// Update search string
viewModel.onSearchTermInput(mockSearchString)

// Assert
val actualState = awaitItem()
assertIs<SelectLocationUiState.ShowData>(actualState)
assertLists(mockCountries, actualState.countries)
assertEquals(selectedRelay, actualState.selectedRelay)
assertIs<SelectLocationUiState.Data>(actualState)
assertIs<RelayListState.RelayList>(actualState.relayListState)
assertLists(
mockCountries,
(actualState.relayListState as RelayListState.RelayList).countries
)
assertEquals(
selectedRelay,
(actualState.relayListState as RelayListState.RelayList).selectedRelay
)
}
}

Expand All @@ -172,14 +197,14 @@ class SelectLocationViewModelTest {
// Act, Assert
viewModel.uiState.test {
// Wait for first data
assertIs<SelectLocationUiState.ShowData>(awaitItem())
assertIs<SelectLocationUiState.Data>(awaitItem())

// Update search string
viewModel.onSearchTermInput(mockSearchString)

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

0 comments on commit 7efda2d

Please sign in to comment.