create tests for profile use case

This commit is contained in:
Adam Brown 2022-11-07 21:13:52 +00:00
parent 84bb15e5db
commit aa19346aed
3 changed files with 58 additions and 7 deletions

View File

@ -17,4 +17,5 @@ class FakeChatEngine : ChatEngine by mockk() {
fun givenNotificationsInvites() = every { notificationsInvites() }.delegateEmit()
fun givenNotificationsMessages() = every { notificationsMessages() }.delegateEmit()
fun givenInvites() = every { invites() }.delegateEmit()
fun givenMe(forceRefresh: Boolean) = coEvery { me(forceRefresh) }.delegateReturn()
}

View File

@ -3,6 +3,7 @@ package app.dapk.st.profile.state
import app.dapk.st.core.Lce
import app.dapk.st.core.extensions.ErrorTracker
import app.dapk.st.engine.ChatEngine
import app.dapk.st.engine.Me
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
@ -13,18 +14,22 @@ class ProfileUseCase(
private val errorTracker: ErrorTracker,
) {
private var meCache: Me? = null
fun content(): Flow<Lce<Page.Profile.Content>> {
val flow = flow {
val result = runCatching { chatEngine.me(forceRefresh = true) }
.onFailure { errorTracker.track(it, "Loading profile") }
emit(result)
}
val combine = combine(flow, chatEngine.invites(), transform = { me, invites -> me to invites })
return combine.map { (me, invites) ->
return combine(fetchMe(), chatEngine.invites(), transform = { me, invites -> me to invites }).map { (me, invites) ->
when (me.isSuccess) {
true -> Lce.Content(Page.Profile.Content(me.getOrThrow(), invites.size))
false -> Lce.Error(me.exceptionOrNull()!!)
}
}
}
private fun fetchMe() = flow {
meCache?.let { emit(Result.success(it)) }
val result = runCatching { chatEngine.me(forceRefresh = true) }
.onFailure { errorTracker.track(it, "Loading profile") }
.onSuccess { meCache = it }
emit(result)
}
}

View File

@ -0,0 +1,45 @@
package app.dapk.st.profile.state
import app.dapk.st.core.Lce
import app.dapk.st.engine.Me
import app.dapk.st.matrix.common.HomeServerUrl
import fake.FakeChatEngine
import fake.FakeErrorTracker
import fixture.aRoomInvite
import fixture.aUserId
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
private val A_ME = Me(aUserId(), null, null, HomeServerUrl("ignored"))
private val AN_INVITES_LIST = listOf(aRoomInvite(), aRoomInvite(), aRoomInvite(), aRoomInvite())
private val AN_ERROR = RuntimeException()
class ProfileUseCaseTest {
private val fakeChatEngine = FakeChatEngine()
private val fakeErrorTracker = FakeErrorTracker()
private val useCase = ProfileUseCase(fakeChatEngine, fakeErrorTracker)
@Test
fun `given me and invites, when fetching content, then emits content`() = runTest {
fakeChatEngine.givenMe(forceRefresh = true).returns(A_ME)
fakeChatEngine.givenInvites().emits(AN_INVITES_LIST)
val result = useCase.content().first()
result shouldBeEqualTo Lce.Content(Page.Profile.Content(A_ME, invitationsCount = AN_INVITES_LIST.size))
}
@Test
fun `given me fails, when fetching content, then emits error`() = runTest {
fakeChatEngine.givenMe(forceRefresh = true).throws(AN_ERROR)
fakeChatEngine.givenInvites().emits(emptyList())
val result = useCase.content().first()
result shouldBeEqualTo Lce.Error(AN_ERROR)
}
}