Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jul 31, 2024
1 parent 0ed5b27 commit 7ac0418
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sealed interface CustomListActionResultData : Parcelable {
) : CustomListActionResultData

@Parcelize
data class Renamed(val newName: CustomListName, override val undo: CustomListAction) :
data class Renamed(val newName: CustomListName, override val undo: CustomListAction.Rename) :
CustomListActionResultData

@Parcelize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import kotlin.test.assertIs
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.communication.Created
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
import net.mullvad.mullvadvpn.compose.dialog.CreateCustomListNavArgs
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
import net.mullvad.mullvadvpn.lib.model.CustomListAlreadyExists
Expand All @@ -32,16 +33,27 @@ class CreateCustomListDialogViewModelTest {
fun `when successfully creating a list with locations should emit return with result side effect`() =
runTest {
// Arrange
val expectedResult: Created = mockk()
val customListName = "list"
val mockCreated: Created = mockk()
val mockUndo: CustomListAction.Delete = mockk()
val customListName = CustomListName.fromString("list")
val customListId = CustomListId("1")
val locationNames = listOf("locationName")
val expectedResult = CustomListActionResultData.CreatedWithLocations(
customListName = customListName,
locationNames = locationNames,
undo = mockUndo
)
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
expectedResult.right()
every { expectedResult.locationNames } returns listOf("locationName")
mockCreated.right()
every { mockCreated.locationNames } returns locationNames
every { mockCreated.name } returns customListName
every { mockCreated.id } returns customListId
every { mockCreated.undo } returns mockUndo

// Act, Assert
viewModel.uiSideEffect.test {
viewModel.createCustomList(customListName)
viewModel.createCustomList(customListName.value)
val sideEffect = awaitItem()
assertIs<CreateCustomListDialogSideEffect.ReturnWithResult>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlin.test.assertIs
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.screen.CustomListLocationsNavArgs
import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
Expand Down Expand Up @@ -214,40 +215,57 @@ class CustomListLocationsViewModelTest {
}

@Test
fun `given new list true when saving successfully should emit close screen side effect`() =
fun `given new list true when saving successfully should emit return with result data`() =
runTest {
// Arrange
val customListId = CustomListId("1")
val customListName = CustomListName.fromString("name")
val newList = true
val expectedResult: LocationsChanged = mockk()
val locationChangedMock: LocationsChanged = mockk()
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()
locationChangedMock.right()
every { locationChangedMock.name } returns customListName
every { locationChangedMock.id } returns customListId
val viewModel = createViewModel(customListId, newList)

// Act, Assert
viewModel.uiSideEffect.test {
viewModel.save()
val sideEffect = awaitItem()
assertIs<CustomListLocationsSideEffect.CloseScreen>(sideEffect)
assertIs<CustomListLocationsSideEffect.ReturnWithResultData>(sideEffect)
}
}

@Test
fun `given new list false when saving successfully should emit return with result side effect`() =
fun `given new list false when saving successfully should emit return with result data`() =
runTest {
// Arrange
val customListId = CustomListId("1")
val customListName = CustomListName.fromString("name")
val mockUndo: CustomListAction.UpdateLocations = mockk()
val addedLocations: List<GeoLocationId> = listOf(mockk())
val removedLocations: List<GeoLocationId> = listOf(mockk())
val newList = false
val expectedResult: LocationsChanged = mockk()
val locationsChangedMock: LocationsChanged = mockk()
val expectedResult =
CustomListActionResultData.LocationChanged(
customListName = customListName,
undo = mockUndo
)
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
expectedResult.right()
locationsChangedMock.right()
every { locationsChangedMock.id } returns customListId
every { locationsChangedMock.name } returns customListName
every { locationsChangedMock.addedLocations } returns addedLocations
every { locationsChangedMock.removedLocations } returns removedLocations
every { locationsChangedMock.undo } returns mockUndo
val viewModel = createViewModel(customListId, newList)

// Act, Assert
viewModel.uiSideEffect.test {
viewModel.save()
val sideEffect = awaitItem()
assertIs<CustomListLocationsSideEffect.ReturnWithResult>(sideEffect)
assertIs<CustomListLocationsSideEffect.ReturnWithResultData>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import app.cash.turbine.test
import arrow.core.right
import com.ramcosta.composedestinations.generated.navargs.toSavedStateHandle
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlin.test.assertIs
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
import net.mullvad.mullvadvpn.compose.communication.Deleted
import net.mullvad.mullvadvpn.compose.dialog.DeleteCustomListNavArgs
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
Expand All @@ -25,10 +27,16 @@ class DeleteCustomListConfirmationViewModelTest {
@Test
fun `when successfully deleting a list should emit return with result side effect`() = runTest {
// Arrange
val expectedResult: Deleted = mockk()
val deleted: Deleted = mockk()
val customListName = CustomListName.fromString("name")
val undo: CustomListAction.Create = mockk()
val expectedResult =
CustomListActionResultData.Deleted(customListName = customListName, undo = undo)
every { deleted.name } returns customListName
every { deleted.undo } returns undo
val viewModel = createViewModel()
coEvery { mockCustomListActionUseCase(any<CustomListAction.Delete>()) } returns
expectedResult.right()
deleted.right()

// Act, Assert
viewModel.uiSideEffect.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import arrow.core.left
import arrow.core.right
import com.ramcosta.composedestinations.generated.navargs.toSavedStateHandle
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlin.test.assertIs
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
import net.mullvad.mullvadvpn.compose.communication.Renamed
import net.mullvad.mullvadvpn.compose.dialog.EditCustomListNameNavArgs
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
Expand All @@ -28,16 +30,21 @@ class EditCustomListNameDialogViewModelTest {
@Test
fun `when successfully renamed list should emit return with result side effect`() = runTest {
// Arrange
val expectedResult: Renamed = mockk()
val renamed: Renamed = mockk()
val customListId = CustomListId("id")
val customListName = "list"
val viewModel = createViewModel(customListId, customListName)
val customListName = CustomListName.fromString("list")
val undo: CustomListAction.Rename = mockk()
val expectedResult =
CustomListActionResultData.Renamed(newName = customListName, undo = undo)
every { renamed.name } returns customListName
every { renamed.undo } returns undo
val viewModel = createViewModel(customListId, customListName.value)
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
expectedResult.right()
renamed.right()

// Act, Assert
viewModel.uiSideEffect.test {
viewModel.updateCustomListName(customListName)
viewModel.updateCustomListName(customListName.value)
val sideEffect = awaitItem()
assertIs<EditCustomListNameDialogSideEffect.ReturnWithResult>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runTest
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
import net.mullvad.mullvadvpn.compose.state.RelayListItem
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
Expand Down Expand Up @@ -292,11 +293,13 @@ class SelectLocationViewModelTest {
locations = emptyList(),
)
val expectedResult =
LocationsChanged(
id = customListId,
name = customListName,
locations = listOf(addedLocationsId),
oldLocations = emptyList(),
CustomListActionResultData.LocationAdded(
customListName = customListName,
locationName = location.name,
undo = CustomListAction.UpdateLocations(
id = customListId,
locations = emptyList()
)
)

coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
Expand All @@ -312,8 +315,8 @@ class SelectLocationViewModelTest {
viewModel.uiSideEffect.test {
viewModel.addLocationToList(item = location, customList = customList)
val sideEffect = awaitItem()
assertIs<SelectLocationSideEffect.LocationAddedToCustomList>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
assertIs<SelectLocationSideEffect.CustomListActionToast>(sideEffect)
assertEquals(expectedResult, sideEffect.resultData)
}
}

Expand All @@ -331,11 +334,13 @@ class SelectLocationViewModelTest {
every { descendants() } returns emptyList()
}
val expectedResult =
LocationsChanged(
id = customListId,
name = customListName,
locations = emptyList(),
oldLocations = listOf(removedLocationsId),
CustomListActionResultData.LocationRemoved(
customListName = customListName,
locationName = locationName,
undo = CustomListAction.UpdateLocations(
id = customListId,
locations = listOf(location.id)
)
)
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
LocationsChanged(
Expand All @@ -350,8 +355,8 @@ class SelectLocationViewModelTest {
viewModel.uiSideEffect.test {
viewModel.removeLocationFromList(item = location, customListId = customListId)
val sideEffect = awaitItem()
assertIs<SelectLocationSideEffect.LocationRemovedFromCustomList>(sideEffect)
assertEquals(expectedResult, sideEffect.result)
assertIs<SelectLocationSideEffect.CustomListActionToast>(sideEffect)
assertEquals(expectedResult, sideEffect.resultData)
}
}

Expand Down

0 comments on commit 7ac0418

Please sign in to comment.