Skip to content

Commit

Permalink
Fix and add unit tests for custom list location change
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jun 19, 2024
1 parent 71f310c commit a546af1
Showing 1 changed file with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,29 @@ class SelectLocationViewModelTest {
@Test
fun `after adding a location to a list should emit location added side effect`() = runTest {
// Arrange
val expectedResult: LocationsChanged = mockk()
val locationName = "Country"
val customListName = CustomListName.fromString("custom")
val undoAction: CustomListAction.UpdateLocations = mockk()
val location: RelayItem.Location.Country = mockk {
every { id } returns GeoLocationId.Country("se")
every { name } returns locationName
every { descendants() } returns emptyList()
}
val customList =
RelayItem.CustomList(
id = CustomListId("1"),
customListName = CustomListName.fromString("custom"),
customListName = customListName,
locations = emptyList(),
expanded = false
)
val expectedResult =
LocationsChanged(
name = customListName,
locationNamesAdded = listOf(locationName),
undo = undoAction
)
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()
LocationsChanged(name = customListName, undo = undoAction).right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand All @@ -276,6 +285,43 @@ class SelectLocationViewModelTest {
}
}

@Test
fun `after removing a location from a list should emit location removed side effect`() =
runTest {
// Arrange
val locationName = "Country"
val customListName = CustomListName.fromString("custom")
val undoAction: CustomListAction.UpdateLocations = mockk()
val location: RelayItem.Location.Country = mockk {
every { id } returns GeoLocationId.Country("se")
every { name } returns locationName
every { descendants() } returns emptyList()
}
val customList =
RelayItem.CustomList(
id = CustomListId("1"),
customListName = customListName,
locations = emptyList(),
expanded = false
)
val expectedResult =
LocationsChanged(
name = customListName,
locationNamesRemoved = listOf(locationName),
undo = undoAction
)
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
LocationsChanged(name = customListName, undo = undoAction).right()

// Act, Assert
viewModel.uiSideEffect.test {
viewModel.removeLocationFromList(item = location, customList = customList)
val sideEffect = awaitItem()
assertIs<SelectLocationSideEffect.LocationRemovedFromCustomList>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
}
}

companion object {
private const val RELAY_LIST_EXTENSIONS =
"net.mullvad.mullvadvpn.relaylist.RelayListExtensionsKt"
Expand Down

0 comments on commit a546af1

Please sign in to comment.