Skip to content

Commit

Permalink
create tests for profile use case
Browse files Browse the repository at this point in the history
  • Loading branch information
ouchadam committed Nov 7, 2022
1 parent 84bb15e commit aa19346
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions chat-engine/src/testFixtures/kotlin/fake/FakeChatEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit aa19346

Please sign in to comment.