From 7ac04182055f7253fdff0e41cbd34209dd793e60 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Mon, 22 Jul 2024 09:19:14 +0200 Subject: [PATCH] Fix unit tests --- .../CustomListActionResultData.kt | 2 +- .../CreateCustomListDialogViewModelTest.kt | 22 +++++++++--- .../CustomListLocationsViewModelTest.kt | 34 ++++++++++++++----- ...leteCustomListConfirmationViewModelTest.kt | 12 +++++-- .../EditCustomListNameDialogViewModelTest.kt | 17 +++++++--- .../viewmodel/SelectLocationViewModelTest.kt | 33 ++++++++++-------- 6 files changed, 85 insertions(+), 35 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListActionResultData.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListActionResultData.kt index 8de9b616a797..ae9e50202762 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListActionResultData.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListActionResultData.kt @@ -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 diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CreateCustomListDialogViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CreateCustomListDialogViewModelTest.kt index 2ab6e6267bf3..0ff5c9c3984a 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CreateCustomListDialogViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CreateCustomListDialogViewModelTest.kt @@ -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 @@ -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()) } 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(sideEffect) assertEquals(expectedResult, sideEffect.result) diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListLocationsViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListLocationsViewModelTest.kt index f73dbee7a3fd..4f91bc9e9723 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListLocationsViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListLocationsViewModelTest.kt @@ -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 @@ -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()) } 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(sideEffect) + assertIs(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 = listOf(mockk()) + val removedLocations: List = listOf(mockk()) val newList = false - val expectedResult: LocationsChanged = mockk() + val locationsChangedMock: LocationsChanged = mockk() + val expectedResult = + CustomListActionResultData.LocationChanged( + customListName = customListName, + undo = mockUndo + ) coEvery { mockCustomListUseCase(any()) } 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(sideEffect) + assertIs(sideEffect) assertEquals(expectedResult, sideEffect.result) } } diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/DeleteCustomListConfirmationViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/DeleteCustomListConfirmationViewModelTest.kt index 2656d1a4ff38..7f94a7467269 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/DeleteCustomListConfirmationViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/DeleteCustomListConfirmationViewModelTest.kt @@ -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 @@ -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()) } returns - expectedResult.right() + deleted.right() // Act, Assert viewModel.uiSideEffect.test { diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/EditCustomListNameDialogViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/EditCustomListNameDialogViewModelTest.kt index 7b4dcc0b8321..84ace4b32f82 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/EditCustomListNameDialogViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/EditCustomListNameDialogViewModelTest.kt @@ -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 @@ -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()) } returns - expectedResult.right() + renamed.right() // Act, Assert viewModel.uiSideEffect.test { - viewModel.updateCustomListName(customListName) + viewModel.updateCustomListName(customListName.value) val sideEffect = awaitItem() assertIs(sideEffect) assertEquals(expectedResult, sideEffect.result) diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt index a307a982b78d..55caf5ddcb12 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/SelectLocationViewModelTest.kt @@ -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 @@ -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()) } returns @@ -312,8 +315,8 @@ class SelectLocationViewModelTest { viewModel.uiSideEffect.test { viewModel.addLocationToList(item = location, customList = customList) val sideEffect = awaitItem() - assertIs(sideEffect) - assertEquals(expectedResult, sideEffect.result) + assertIs(sideEffect) + assertEquals(expectedResult, sideEffect.resultData) } } @@ -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()) } returns LocationsChanged( @@ -350,8 +355,8 @@ class SelectLocationViewModelTest { viewModel.uiSideEffect.test { viewModel.removeLocationFromList(item = location, customListId = customListId) val sideEffect = awaitItem() - assertIs(sideEffect) - assertEquals(expectedResult, sideEffect.result) + assertIs(sideEffect) + assertEquals(expectedResult, sideEffect.resultData) } }