Adding unit tests for the stop live use case

This commit is contained in:
Maxime NATUREL 2022-06-16 17:45:16 +02:00
parent 8406b2a4eb
commit d50b0fbb6b
4 changed files with 55 additions and 14 deletions

View File

@ -46,15 +46,30 @@ class StopLiveLocationShareUseCaseTest {
@Test @Test
fun `given a room id when calling use case then the current live is stopped with success`() = runTest { fun `given a room id when calling use case then the current live is stopped with success`() = runTest {
fakeLocationSharingServiceConnection.givenStopLiveLocationSharing() fakeLocationSharingServiceConnection.givenStopLiveLocationSharing()
val updateLiveResult = UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
fakeSession.roomService()
.getRoom(A_ROOM_ID)
.locationSharingService()
.givenStopLiveLocationShareReturns(updateLiveResult)
val result = stopLiveLocationShareUseCase.execute(A_ROOM_ID) val result = stopLiveLocationShareUseCase.execute(A_ROOM_ID)
result shouldBeEqualTo UpdateLiveLocationShareResult.Success(AN_EVENT_ID) result shouldBeEqualTo updateLiveResult
fakeLocationSharingServiceConnection.verifyStopLiveLocationSharing(A_ROOM_ID) fakeLocationSharingServiceConnection.verifyStopLiveLocationSharing(A_ROOM_ID)
} }
@Test @Test
fun `given a room id and error during the process when calling use case then result is failure`() = runTest { fun `given a room id and error during the process when calling use case then result is failure`() = runTest {
val error = Throwable()
val updateLiveResult = UpdateLiveLocationShareResult.Failure(error)
fakeSession.roomService()
.getRoom(A_ROOM_ID)
.locationSharingService()
.givenStopLiveLocationShareReturns(updateLiveResult)
val result = stopLiveLocationShareUseCase.execute(A_ROOM_ID)
result shouldBeEqualTo updateLiveResult
fakeLocationSharingServiceConnection.verifyStopLiveLocationSharingNotCalled(A_ROOM_ID)
} }
} }

View File

@ -18,39 +18,47 @@ package im.vector.app.features.location.live.map
import com.airbnb.mvrx.test.MvRxTestRule import com.airbnb.mvrx.test.MvRxTestRule
import im.vector.app.features.location.LocationData import im.vector.app.features.location.LocationData
import im.vector.app.features.location.LocationSharingServiceConnection import im.vector.app.features.location.live.StopLiveLocationShareUseCase
import im.vector.app.test.fakes.FakeLocationSharingServiceConnection
import im.vector.app.test.test import im.vector.app.test.test
import io.mockk.every import io.mockk.every
import io.mockk.just
import io.mockk.mockk import io.mockk.mockk
import io.mockk.runs import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.matrix.android.sdk.api.util.MatrixItem import org.matrix.android.sdk.api.util.MatrixItem
private const val A_ROOM_ID = "room_id"
class LocationLiveMapViewModelTest { class LocationLiveMapViewModelTest {
@get:Rule @get:Rule
val mvrxTestRule = MvRxTestRule() val mvRxTestRule = MvRxTestRule(testDispatcher = UnconfinedTestDispatcher())
private val fakeRoomId = "" private val args = LocationLiveMapViewArgs(roomId = A_ROOM_ID)
private val args = LocationLiveMapViewArgs(roomId = fakeRoomId)
private val getListOfUserLiveLocationUseCase = mockk<GetListOfUserLiveLocationUseCase>() private val getListOfUserLiveLocationUseCase = mockk<GetListOfUserLiveLocationUseCase>()
private val locationServiceConnection = mockk<LocationSharingServiceConnection>() private val locationServiceConnection = FakeLocationSharingServiceConnection()
private val stopLiveLocationShareUseCase = mockk<StopLiveLocationShareUseCase>()
private fun createViewModel(): LocationLiveMapViewModel { private fun createViewModel(): LocationLiveMapViewModel {
return LocationLiveMapViewModel( return LocationLiveMapViewModel(
LocationLiveMapViewState(args), LocationLiveMapViewState(args),
getListOfUserLiveLocationUseCase, getListOfUserLiveLocationUseCase,
locationServiceConnection locationServiceConnection.instance,
stopLiveLocationShareUseCase
) )
} }
@After
fun tearDown() {
unmockkAll()
}
@Test @Test
fun `given the viewModel has been initialized then viewState contains user locations list`() = runTest { fun `given the viewModel has been initialized then viewState contains user locations list`() = runTest {
val userLocations = listOf( val userLocations = listOf(
@ -63,8 +71,8 @@ class LocationLiveMapViewModelTest {
showStopSharingButton = false showStopSharingButton = false
) )
) )
every { locationServiceConnection.bind(any()) } just runs locationServiceConnection.givenBind()
every { getListOfUserLiveLocationUseCase.execute(fakeRoomId) } returns flowOf(userLocations) every { getListOfUserLiveLocationUseCase.execute(A_ROOM_ID) } returns flowOf(userLocations)
val viewModel = createViewModel() val viewModel = createViewModel()
viewModel viewModel
@ -76,6 +84,6 @@ class LocationLiveMapViewModelTest {
) )
.finish() .finish()
verify { locationServiceConnection.bind(viewModel) } locationServiceConnection.verifyBind(viewModel)
} }
} }

View File

@ -18,9 +18,11 @@ package im.vector.app.test.fakes
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.mockk.coEvery
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import org.matrix.android.sdk.api.session.room.location.LocationSharingService import org.matrix.android.sdk.api.session.room.location.LocationSharingService
import org.matrix.android.sdk.api.session.room.location.UpdateLiveLocationShareResult
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
class FakeLocationSharingService : LocationSharingService by mockk() { class FakeLocationSharingService : LocationSharingService by mockk() {
@ -31,4 +33,8 @@ class FakeLocationSharingService : LocationSharingService by mockk() {
every { getRunningLiveLocationShareSummaries() } returns it every { getRunningLiveLocationShareSummaries() } returns it
} }
} }
fun givenStopLiveLocationShareReturns(result: UpdateLiveLocationShareResult) {
coEvery { stopLiveLocationShare() } returns result
}
} }

View File

@ -27,6 +27,14 @@ class FakeLocationSharingServiceConnection {
val instance = mockk<LocationSharingServiceConnection>() val instance = mockk<LocationSharingServiceConnection>()
fun givenBind() {
every { instance.bind(any()) } just runs
}
fun verifyBind(callback: LocationSharingServiceConnection.Callback) {
verify { instance.bind(callback) }
}
fun givenStopLiveLocationSharing() { fun givenStopLiveLocationSharing() {
every { instance.stopLiveLocationSharing(any()) } just runs every { instance.stopLiveLocationSharing(any()) } just runs
} }
@ -34,4 +42,8 @@ class FakeLocationSharingServiceConnection {
fun verifyStopLiveLocationSharing(roomId: String) { fun verifyStopLiveLocationSharing(roomId: String) {
verify { instance.stopLiveLocationSharing(roomId) } verify { instance.stopLiveLocationSharing(roomId) }
} }
fun verifyStopLiveLocationSharingNotCalled(roomId: String) {
verify(inverse = true) { instance.stopLiveLocationSharing(roomId) }
}
} }