Skip to content

Commit 8c384c7

Browse files
committed
Fix some more tests
1 parent c274eb5 commit 8c384c7

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt

+54-37
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io.mockk.mockkStatic
1111
import io.mockk.unmockkAll
1212
import kotlin.test.assertEquals
1313
import kotlin.test.assertIs
14+
import kotlin.test.assertTrue
1415
import kotlinx.coroutines.cancel
1516
import kotlinx.coroutines.flow.MutableStateFlow
1617
import kotlinx.coroutines.test.runTest
@@ -64,8 +65,9 @@ class SelectLocationViewModelTest {
6465
private val selectedProviders = MutableStateFlow<Constraint<Providers>>(Constraint.Any)
6566
private val selectedRelayItemFlow = MutableStateFlow<Constraint<RelayItemId>>(Constraint.Any)
6667
private val filteredRelayList = MutableStateFlow<List<RelayItem.Location.Country>>(emptyList())
67-
private val filteredCustomRelayListItems = MutableStateFlow<List<RelayItem.CustomList>>(emptyList())
68-
private val customListsRelayItem = MutableStateFlow<List<RelayItem.CustomList>>(emptyList())
68+
private val filteredCustomRelayListItems =
69+
MutableStateFlow<List<RelayItem.CustomList>>(emptyList())
70+
private val customListsRelayItem = MutableStateFlow<List<RelayItem.CustomList>>(emptyList())
6971

7072
@BeforeEach
7173
fun setup() {
@@ -78,7 +80,6 @@ class SelectLocationViewModelTest {
7880
every { mockFilteredCustomListRelayItemsUseCase() } returns filteredCustomRelayListItems
7981
every { mockCustomListsRelayItemUseCase() } returns customListsRelayItem
8082

81-
8283
mockkStatic(RELAY_LIST_EXTENSIONS)
8384
mockkStatic(RELAY_ITEM_EXTENSIONS)
8485
mockkStatic(CUSTOM_LIST_EXTENSIONS)
@@ -107,51 +108,50 @@ class SelectLocationViewModelTest {
107108
}
108109

109110
@Test
110-
fun `given relayListWithSelection emits update uiState should contain new update`() = runTest {
111+
fun `given filteredRelayList emits update uiState should contain new update`() = runTest {
111112
// Arrange
112-
val mockCountries = listOf<RelayItem.Location.Country>(mockk(), mockk())
113-
val selectedItem: RelayItemId = mockk()
114-
filteredRelayList.value = mockCountries
115-
selectedRelayItemFlow.value = Constraint.Only(selectedItem)
113+
filteredRelayList.value = testCountries
114+
val selectedId = testCountries.first().id
115+
selectedRelayItemFlow.value = Constraint.Only(selectedId)
116116

117117
// Act, Assert
118118
viewModel.uiState.test {
119-
val loading = awaitItem()
120-
assertIs<SelectLocationUiState.Loading>(loading)
121119
val actualState = awaitItem()
122120
assertIs<SelectLocationUiState.Content>(actualState)
123-
assertLists(mockCountries, actualState.relayListItems.filter { it is RelayListItem.GeoLocationItem })
124-
// assertEquals(selectedItem, actualState.selectedItem)
121+
assertLists(
122+
testCountries.map { it.id },
123+
actualState.relayListItems.mapNotNull { it.relayItemId() }
124+
)
125+
assertTrue(
126+
actualState.relayListItems
127+
.filterIsInstance<RelayListItem.SelectableItem>()
128+
.first { it.relayItemId() == selectedId }
129+
.isSelected
130+
)
125131
}
126132
}
127133

128-
fun RelayListItem.relayItemId() = when(this) {
129-
is RelayListItem.CustomListFooter -> null
130-
RelayListItem.CustomListHeader -> null
131-
RelayListItem.LocationHeader -> null
132-
is RelayListItem.LocationsEmptyText -> null
133-
is RelayListItem.CustomListEntryItem -> item.id
134-
is RelayListItem.CustomListItem -> item.id
135-
is RelayListItem.GeoLocationItem -> item.id
136-
}
137-
138134
@Test
139-
fun `given relayListWithSelection emits update with no selections selectedItem should be null`() =
140-
runTest {
141-
// Arrange
142-
val mockCountries = listOf<RelayItem.Location.Country>(mockk(), mockk())
143-
val selectedItem: RelayItemId? = null
144-
filteredRelayList.value = mockCountries
145-
selectedRelayItemFlow.value = Constraint.Any
146-
147-
// Act, Assert
148-
viewModel.uiState.test {
149-
val actualState = awaitItem()
150-
assertIs<SelectLocationUiState.Content>(actualState)
151-
// assertLists(mockCountries, actualState.countries)
152-
// assertEquals(selectedItem, actualState.selectedItem)
153-
}
135+
fun `given relay is selected all relay items should not be selected`() = runTest {
136+
// Arrange
137+
filteredRelayList.value = testCountries
138+
selectedRelayItemFlow.value = Constraint.Any
139+
140+
// Act, Assert
141+
viewModel.uiState.test {
142+
val actualState = awaitItem()
143+
assertIs<SelectLocationUiState.Content>(actualState)
144+
assertLists(
145+
testCountries.map { it.id },
146+
actualState.relayListItems.mapNotNull { it.relayItemId() }
147+
)
148+
assertTrue(
149+
actualState.relayListItems.filterIsInstance<RelayListItem.SelectableItem>().all {
150+
!it.isSelected
151+
}
152+
)
154153
}
154+
}
155155

156156
@Test
157157
fun `on selectRelay call uiSideEffect should emit CloseScreen and connect`() = runTest {
@@ -296,12 +296,29 @@ class SelectLocationViewModelTest {
296296
}
297297
}
298298

299+
fun RelayListItem.relayItemId() =
300+
when (this) {
301+
is RelayListItem.CustomListFooter -> null
302+
RelayListItem.CustomListHeader -> null
303+
RelayListItem.LocationHeader -> null
304+
is RelayListItem.LocationsEmptyText -> null
305+
is RelayListItem.CustomListEntryItem -> item.id
306+
is RelayListItem.CustomListItem -> item.id
307+
is RelayListItem.GeoLocationItem -> item.id
308+
}
309+
299310
companion object {
300311
private const val RELAY_LIST_EXTENSIONS =
301312
"net.mullvad.mullvadvpn.relaylist.RelayListExtensionsKt"
302313
private const val RELAY_ITEM_EXTENSIONS =
303314
"net.mullvad.mullvadvpn.relaylist.RelayItemExtensionsKt"
304315
private const val CUSTOM_LIST_EXTENSIONS =
305316
"net.mullvad.mullvadvpn.relaylist.CustomListExtensionsKt"
317+
318+
private val testCountries =
319+
listOf<RelayItem.Location.Country>(
320+
RelayItem.Location.Country(id = GeoLocationId.Country("se"), "Sweden", emptyList()),
321+
RelayItem.Location.Country(id = GeoLocationId.Country("no"), "Norway", emptyList())
322+
)
306323
}
307324
}

0 commit comments

Comments
 (0)