Mutualizing mock of asFlow extension

This commit is contained in:
Maxime NATUREL 2022-06-28 14:04:50 +02:00
parent 4c3f6db55c
commit 5b3711b634
3 changed files with 45 additions and 15 deletions

View File

@ -16,13 +16,11 @@
package im.vector.app.features.location.live package im.vector.app.features.location.live
import androidx.lifecycle.asFlow import im.vector.app.test.fakes.FakeFlowLiveDataConversions
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import io.mockk.every import im.vector.app.test.fakes.givenAsFlowReturns
import io.mockk.mockkStatic
import io.mockk.unmockkAll import io.mockk.unmockkAll
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.After import org.junit.After
@ -38,6 +36,7 @@ private const val AN_EVENT_ID = "event_id"
class GetLiveLocationShareSummaryUseCaseTest { class GetLiveLocationShareSummaryUseCaseTest {
private val fakeSession = FakeSession() private val fakeSession = FakeSession()
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
private val getLiveLocationShareSummaryUseCase = GetLiveLocationShareSummaryUseCase( private val getLiveLocationShareSummaryUseCase = GetLiveLocationShareSummaryUseCase(
session = fakeSession session = fakeSession
@ -45,7 +44,7 @@ class GetLiveLocationShareSummaryUseCaseTest {
@Before @Before
fun setUp() { fun setUp() {
mockkStatic("androidx.lifecycle.FlowLiveDataConversions") fakeFlowLiveDataConversions.setup()
} }
@After @After
@ -61,11 +60,11 @@ class GetLiveLocationShareSummaryUseCaseTest {
endOfLiveTimestampMillis = 123, endOfLiveTimestampMillis = 123,
lastLocationDataContent = MessageBeaconLocationDataContent() lastLocationDataContent = MessageBeaconLocationDataContent()
) )
val liveData = fakeSession.roomService() fakeSession.roomService()
.getRoom(A_ROOM_ID) .getRoom(A_ROOM_ID)
.locationSharingService() .locationSharingService()
.givenLiveLocationShareSummaryReturns(AN_EVENT_ID, summary) .givenLiveLocationShareSummaryReturns(AN_EVENT_ID, summary)
every { liveData.asFlow() } returns flowOf(Optional(summary)) .givenAsFlowReturns(Optional(summary))
val result = getLiveLocationShareSummaryUseCase.execute(A_ROOM_ID, AN_EVENT_ID).first() val result = getLiveLocationShareSummaryUseCase.execute(A_ROOM_ID, AN_EVENT_ID).first()

View File

@ -16,16 +16,14 @@
package im.vector.app.features.location.live.map package im.vector.app.features.location.live.map
import androidx.lifecycle.asFlow
import im.vector.app.features.location.LocationData import im.vector.app.features.location.LocationData
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
import im.vector.app.test.fakes.FakeSession import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.givenAsFlowReturns
import io.mockk.coEvery import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkAll import io.mockk.unmockkAll
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo import org.amshove.kluent.shouldBeEqualTo
import org.junit.After import org.junit.After
@ -41,6 +39,7 @@ class GetListOfUserLiveLocationUseCaseTest {
private val fakeSession = FakeSession() private val fakeSession = FakeSession()
private val viewStateMapper = mockk<UserLiveLocationViewStateMapper>() private val viewStateMapper = mockk<UserLiveLocationViewStateMapper>()
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
private val getListOfUserLiveLocationUseCase = GetListOfUserLiveLocationUseCase( private val getListOfUserLiveLocationUseCase = GetListOfUserLiveLocationUseCase(
session = fakeSession, session = fakeSession,
@ -49,7 +48,7 @@ class GetListOfUserLiveLocationUseCaseTest {
@Before @Before
fun setUp() { fun setUp() {
mockkStatic("androidx.lifecycle.FlowLiveDataConversions") fakeFlowLiveDataConversions.setup()
} }
@After @After
@ -78,12 +77,11 @@ class GetListOfUserLiveLocationUseCaseTest {
lastLocationDataContent = MessageBeaconLocationDataContent() lastLocationDataContent = MessageBeaconLocationDataContent()
) )
val summaries = listOf(summary1, summary2, summary3) val summaries = listOf(summary1, summary2, summary3)
val liveData = fakeSession.roomService() fakeSession.roomService()
.getRoom(A_ROOM_ID) .getRoom(A_ROOM_ID)
.locationSharingService() .locationSharingService()
.givenRunningLiveLocationShareSummariesReturns(summaries) .givenRunningLiveLocationShareSummariesReturns(summaries)
.givenAsFlowReturns(summaries)
every { liveData.asFlow() } returns flowOf(summaries)
val viewState1 = UserLiveLocationViewState( val viewState1 = UserLiveLocationViewState(
matrixItem = MatrixItem.UserItem(id = "@userId1:matrix.org", displayName = "User 1", avatarUrl = ""), matrixItem = MatrixItem.UserItem(id = "@userId1:matrix.org", displayName = "User 1", avatarUrl = ""),

View File

@ -0,0 +1,33 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.test.fakes
import androidx.lifecycle.LiveData
import androidx.lifecycle.asFlow
import io.mockk.every
import io.mockk.mockkStatic
import kotlinx.coroutines.flow.flowOf
class FakeFlowLiveDataConversions {
fun setup() {
mockkStatic("androidx.lifecycle.FlowLiveDataConversions")
}
}
fun <T> LiveData<T>.givenAsFlowReturns(value: T) {
every { asFlow() } returns flowOf(value)
}