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
fun `given a room id when calling use case then the current live is stopped with success`() = runTest {
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)
result shouldBeEqualTo UpdateLiveLocationShareResult.Success(AN_EVENT_ID)
result shouldBeEqualTo updateLiveResult
fakeLocationSharingServiceConnection.verifyStopLiveLocationSharing(A_ROOM_ID)
}
@Test
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 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 io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import io.mockk.unmockkAll
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.matrix.android.sdk.api.util.MatrixItem
private const val A_ROOM_ID = "room_id"
class LocationLiveMapViewModelTest {
@get:Rule
val mvrxTestRule = MvRxTestRule()
val mvRxTestRule = MvRxTestRule(testDispatcher = UnconfinedTestDispatcher())
private val fakeRoomId = ""
private val args = LocationLiveMapViewArgs(roomId = fakeRoomId)
private val args = LocationLiveMapViewArgs(roomId = A_ROOM_ID)
private val getListOfUserLiveLocationUseCase = mockk<GetListOfUserLiveLocationUseCase>()
private val locationServiceConnection = mockk<LocationSharingServiceConnection>()
private val locationServiceConnection = FakeLocationSharingServiceConnection()
private val stopLiveLocationShareUseCase = mockk<StopLiveLocationShareUseCase>()
private fun createViewModel(): LocationLiveMapViewModel {
return LocationLiveMapViewModel(
LocationLiveMapViewState(args),
getListOfUserLiveLocationUseCase,
locationServiceConnection
locationServiceConnection.instance,
stopLiveLocationShareUseCase
)
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given the viewModel has been initialized then viewState contains user locations list`() = runTest {
val userLocations = listOf(
@ -63,8 +71,8 @@ class LocationLiveMapViewModelTest {
showStopSharingButton = false
)
)
every { locationServiceConnection.bind(any()) } just runs
every { getListOfUserLiveLocationUseCase.execute(fakeRoomId) } returns flowOf(userLocations)
locationServiceConnection.givenBind()
every { getListOfUserLiveLocationUseCase.execute(A_ROOM_ID) } returns flowOf(userLocations)
val viewModel = createViewModel()
viewModel
@ -76,6 +84,6 @@ class LocationLiveMapViewModelTest {
)
.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.MutableLiveData
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
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
class FakeLocationSharingService : LocationSharingService by mockk() {
@ -31,4 +33,8 @@ class FakeLocationSharingService : LocationSharingService by mockk() {
every { getRunningLiveLocationShareSummaries() } returns it
}
}
fun givenStopLiveLocationShareReturns(result: UpdateLiveLocationShareResult) {
coEvery { stopLiveLocationShare() } returns result
}
}

View File

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