Skip to content

Commit 9d98a16

Browse files
committed
Fix unit tests
1 parent fadd033 commit 9d98a16

File tree

6 files changed

+85
-35
lines changed

6 files changed

+85
-35
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListActionResultData.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sealed interface CustomListActionResultData : Parcelable {
2222
) : CustomListActionResultData
2323

2424
@Parcelize
25-
data class Renamed(val newName: CustomListName, override val undo: CustomListAction) :
25+
data class Renamed(val newName: CustomListName, override val undo: CustomListAction.Rename) :
2626
CustomListActionResultData
2727

2828
@Parcelize

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

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlin.test.assertIs
1111
import kotlinx.coroutines.test.runTest
1212
import net.mullvad.mullvadvpn.compose.communication.Created
1313
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
14+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
1415
import net.mullvad.mullvadvpn.compose.dialog.CreateCustomListNavArgs
1516
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
1617
import net.mullvad.mullvadvpn.lib.model.CustomListAlreadyExists
@@ -32,16 +33,27 @@ class CreateCustomListDialogViewModelTest {
3233
fun `when successfully creating a list with locations should emit return with result side effect`() =
3334
runTest {
3435
// Arrange
35-
val expectedResult: Created = mockk()
36-
val customListName = "list"
36+
val mockCreated: Created = mockk()
37+
val mockUndo: CustomListAction.Delete = mockk()
38+
val customListName = CustomListName.fromString("list")
39+
val customListId = CustomListId("1")
40+
val locationNames = listOf("locationName")
41+
val expectedResult = CustomListActionResultData.CreatedWithLocations(
42+
customListName = customListName,
43+
locationNames = locationNames,
44+
undo = mockUndo
45+
)
3746
val viewModel = createViewModelWithLocationCode(GeoLocationId.Country("AB"))
3847
coEvery { mockCustomListActionUseCase(any<CustomListAction.Create>()) } returns
39-
expectedResult.right()
40-
every { expectedResult.locationNames } returns listOf("locationName")
48+
mockCreated.right()
49+
every { mockCreated.locationNames } returns locationNames
50+
every { mockCreated.name } returns customListName
51+
every { mockCreated.id } returns customListId
52+
every { mockCreated.undo } returns mockUndo
4153

4254
// Act, Assert
4355
viewModel.uiSideEffect.test {
44-
viewModel.createCustomList(customListName)
56+
viewModel.createCustomList(customListName.value)
4557
val sideEffect = awaitItem()
4658
assertIs<CreateCustomListDialogSideEffect.ReturnWithResult>(sideEffect)
4759
assertEquals(expectedResult, sideEffect.result)

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

+26-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import kotlin.test.assertIs
1010
import kotlinx.coroutines.flow.MutableStateFlow
1111
import kotlinx.coroutines.test.runTest
1212
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
13+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
1314
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
1415
import net.mullvad.mullvadvpn.compose.screen.CustomListLocationsNavArgs
1516
import net.mullvad.mullvadvpn.compose.state.CustomListLocationsUiState
@@ -180,40 +181,57 @@ class CustomListLocationsViewModelTest {
180181
}
181182

182183
@Test
183-
fun `given new list true when saving successfully should emit close screen side effect`() =
184+
fun `given new list true when saving successfully should emit return with result data`() =
184185
runTest {
185186
// Arrange
186187
val customListId = CustomListId("1")
188+
val customListName = CustomListName.fromString("name")
187189
val newList = true
188-
val expectedResult: LocationsChanged = mockk()
190+
val locationChangedMock: LocationsChanged = mockk()
189191
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
190-
expectedResult.right()
192+
locationChangedMock.right()
193+
every { locationChangedMock.name } returns customListName
194+
every { locationChangedMock.id } returns customListId
191195
val viewModel = createViewModel(customListId, newList)
192196

193197
// Act, Assert
194198
viewModel.uiSideEffect.test {
195199
viewModel.save()
196200
val sideEffect = awaitItem()
197-
assertIs<CustomListLocationsSideEffect.CloseScreen>(sideEffect)
201+
assertIs<CustomListLocationsSideEffect.ReturnWithResultData>(sideEffect)
198202
}
199203
}
200204

201205
@Test
202-
fun `given new list false when saving successfully should emit return with result side effect`() =
206+
fun `given new list false when saving successfully should emit return with result data`() =
203207
runTest {
204208
// Arrange
205209
val customListId = CustomListId("1")
210+
val customListName = CustomListName.fromString("name")
211+
val mockUndo: CustomListAction.UpdateLocations = mockk()
212+
val addedLocations: List<GeoLocationId> = listOf(mockk())
213+
val removedLocations: List<GeoLocationId> = listOf(mockk())
206214
val newList = false
207-
val expectedResult: LocationsChanged = mockk()
215+
val locationsChangedMock: LocationsChanged = mockk()
216+
val expectedResult =
217+
CustomListActionResultData.LocationChanged(
218+
customListName = customListName,
219+
undo = mockUndo
220+
)
208221
coEvery { mockCustomListUseCase(any<CustomListAction.UpdateLocations>()) } returns
209-
expectedResult.right()
222+
locationsChangedMock.right()
223+
every { locationsChangedMock.id } returns customListId
224+
every { locationsChangedMock.name } returns customListName
225+
every { locationsChangedMock.addedLocations } returns addedLocations
226+
every { locationsChangedMock.removedLocations } returns removedLocations
227+
every { locationsChangedMock.undo } returns mockUndo
210228
val viewModel = createViewModel(customListId, newList)
211229

212230
// Act, Assert
213231
viewModel.uiSideEffect.test {
214232
viewModel.save()
215233
val sideEffect = awaitItem()
216-
assertIs<CustomListLocationsSideEffect.ReturnWithResult>(sideEffect)
234+
assertIs<CustomListLocationsSideEffect.ReturnWithResultData>(sideEffect)
217235
assertEquals(expectedResult, sideEffect.result)
218236
}
219237
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import app.cash.turbine.test
44
import arrow.core.right
55
import com.ramcosta.composedestinations.generated.navargs.toSavedStateHandle
66
import io.mockk.coEvery
7+
import io.mockk.every
78
import io.mockk.mockk
89
import kotlin.test.assertIs
910
import kotlinx.coroutines.test.runTest
1011
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
12+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
1113
import net.mullvad.mullvadvpn.compose.communication.Deleted
1214
import net.mullvad.mullvadvpn.compose.dialog.DeleteCustomListNavArgs
1315
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
@@ -25,10 +27,16 @@ class DeleteCustomListConfirmationViewModelTest {
2527
@Test
2628
fun `when successfully deleting a list should emit return with result side effect`() = runTest {
2729
// Arrange
28-
val expectedResult: Deleted = mockk()
30+
val deleted: Deleted = mockk()
31+
val customListName = CustomListName.fromString("name")
32+
val undo: CustomListAction.Create = mockk()
33+
val expectedResult =
34+
CustomListActionResultData.Deleted(customListName = customListName, undo = undo)
35+
every { deleted.name } returns customListName
36+
every { deleted.undo } returns undo
2937
val viewModel = createViewModel()
3038
coEvery { mockCustomListActionUseCase(any<CustomListAction.Delete>()) } returns
31-
expectedResult.right()
39+
deleted.right()
3240

3341
// Act, Assert
3442
viewModel.uiSideEffect.test {

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

+12-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import arrow.core.left
55
import arrow.core.right
66
import com.ramcosta.composedestinations.generated.navargs.toSavedStateHandle
77
import io.mockk.coEvery
8+
import io.mockk.every
89
import io.mockk.mockk
910
import kotlin.test.assertIs
1011
import kotlinx.coroutines.test.runTest
1112
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
13+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
1214
import net.mullvad.mullvadvpn.compose.communication.Renamed
1315
import net.mullvad.mullvadvpn.compose.dialog.EditCustomListNameNavArgs
1416
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
@@ -28,16 +30,21 @@ class EditCustomListNameDialogViewModelTest {
2830
@Test
2931
fun `when successfully renamed list should emit return with result side effect`() = runTest {
3032
// Arrange
31-
val expectedResult: Renamed = mockk()
33+
val renamed: Renamed = mockk()
3234
val customListId = CustomListId("id")
33-
val customListName = "list"
34-
val viewModel = createViewModel(customListId, customListName)
35+
val customListName = CustomListName.fromString("list")
36+
val undo: CustomListAction.Rename = mockk()
37+
val expectedResult =
38+
CustomListActionResultData.Renamed(newName = customListName, undo = undo)
39+
every { renamed.name } returns customListName
40+
every { renamed.undo } returns undo
41+
val viewModel = createViewModel(customListId, customListName.value)
3542
coEvery { mockCustomListActionUseCase(any<CustomListAction.Rename>()) } returns
36-
expectedResult.right()
43+
renamed.right()
3744

3845
// Act, Assert
3946
viewModel.uiSideEffect.test {
40-
viewModel.updateCustomListName(customListName)
47+
viewModel.updateCustomListName(customListName.value)
4148
val sideEffect = awaitItem()
4249
assertIs<EditCustomListNameDialogSideEffect.ReturnWithResult>(sideEffect)
4350
assertEquals(expectedResult, sideEffect.result)

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

+19-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlinx.coroutines.cancel
1515
import kotlinx.coroutines.flow.MutableStateFlow
1616
import kotlinx.coroutines.test.runTest
1717
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
18+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResultData
1819
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
1920
import net.mullvad.mullvadvpn.compose.state.SelectLocationUiState
2021
import net.mullvad.mullvadvpn.lib.common.test.TestCoroutineRule
@@ -268,11 +269,13 @@ class SelectLocationViewModelTest {
268269
expanded = false
269270
)
270271
val expectedResult =
271-
LocationsChanged(
272-
id = customListId,
273-
name = customListName,
274-
locations = listOf(addedLocationsId),
275-
oldLocations = emptyList(),
272+
CustomListActionResultData.LocationAdded(
273+
customListName = customListName,
274+
locationName = location.name,
275+
undo = CustomListAction.UpdateLocations(
276+
id = customListId,
277+
locations = emptyList()
278+
)
276279
)
277280

278281
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
@@ -288,8 +291,8 @@ class SelectLocationViewModelTest {
288291
viewModel.uiSideEffect.test {
289292
viewModel.addLocationToList(item = location, customList = customList)
290293
val sideEffect = awaitItem()
291-
assertIs<SelectLocationSideEffect.LocationAddedToCustomList>(sideEffect)
292-
assertEquals(expectedResult, sideEffect.result)
294+
assertIs<SelectLocationSideEffect.CustomListActionToast>(sideEffect)
295+
assertEquals(expectedResult, sideEffect.resultData)
293296
}
294297
}
295298

@@ -314,11 +317,13 @@ class SelectLocationViewModelTest {
314317
expanded = false
315318
)
316319
val expectedResult =
317-
LocationsChanged(
318-
id = customListId,
319-
name = customListName,
320-
locations = emptyList(),
321-
oldLocations = listOf(removedLocationsId),
320+
CustomListActionResultData.LocationRemoved(
321+
customListName = customListName,
322+
locationName = locationName,
323+
undo = CustomListAction.UpdateLocations(
324+
id = customListId,
325+
locations = listOf(location.id)
326+
)
322327
)
323328
coEvery { mockCustomListActionUseCase(any<CustomListAction.UpdateLocations>()) } returns
324329
LocationsChanged(
@@ -333,8 +338,8 @@ class SelectLocationViewModelTest {
333338
viewModel.uiSideEffect.test {
334339
viewModel.removeLocationFromList(item = location, customList = customList)
335340
val sideEffect = awaitItem()
336-
assertIs<SelectLocationSideEffect.LocationRemovedFromCustomList>(sideEffect)
337-
assertEquals(expectedResult, sideEffect.result)
341+
assertIs<SelectLocationSideEffect.CustomListActionToast>(sideEffect)
342+
assertEquals(expectedResult, sideEffect.resultData)
338343
}
339344
}
340345

0 commit comments

Comments
 (0)