Adding unit tests for GetSpacesUseCase

This commit is contained in:
Maxime NATUREL 2023-02-22 11:58:26 +01:00
parent c74cdb9540
commit a509da54e8
4 changed files with 143 additions and 1 deletions

View File

@ -24,7 +24,6 @@ import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams
import org.matrix.android.sdk.flow.flow
import javax.inject.Inject
// TODO add unit tests
class GetSpacesUseCase @Inject constructor(
private val activeSessionHolder: ActiveSessionHolder,
) {

View File

@ -0,0 +1,104 @@
/*
* Copyright (c) 2023 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.features.spaces
import im.vector.app.test.fakes.FakeActiveSessionHolder
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
import im.vector.app.test.fakes.givenAsFlow
import im.vector.app.test.test
import io.mockk.mockk
import io.mockk.unmockkAll
import io.mockk.verify
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.space.SpaceSummaryQueryParams
internal class GetSpacesUseCaseTest {
private val fakeActiveSessionHolder = FakeActiveSessionHolder()
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
private val getSpacesUseCase = GetSpacesUseCase(
activeSessionHolder = fakeActiveSessionHolder.instance,
)
@Before
fun setUp() {
fakeFlowLiveDataConversions.setup()
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given params when execute then the list of summaries is returned`() = runTest {
// Given
val queryParams = givenSpaceQueryParams()
val firstSummaries = listOf<RoomSummary>(mockk())
val nextSummaries = listOf<RoomSummary>(mockk())
fakeActiveSessionHolder.fakeSession
.fakeSpaceService
.givenGetSpaceSummariesReturns(firstSummaries)
fakeActiveSessionHolder.fakeSession
.fakeSpaceService
.givenGetSpaceSummariesLiveReturns(nextSummaries)
.givenAsFlow()
// When
val testObserver = getSpacesUseCase.execute(queryParams).test(this)
advanceUntilIdle()
// Then
testObserver
.assertValues(firstSummaries, nextSummaries)
.finish()
verify {
fakeActiveSessionHolder.fakeSession.fakeSpaceService.getSpaceSummaries(queryParams)
fakeActiveSessionHolder.fakeSession.fakeSpaceService.getSpaceSummariesLive(queryParams)
}
}
@Test
fun `given no active session when execute then empty flow is returned`() = runTest {
// Given
fakeActiveSessionHolder.givenGetSafeActiveSessionReturns(null)
val queryParams = givenSpaceQueryParams()
// When
val testObserver = getSpacesUseCase.execute(queryParams).test(this)
advanceUntilIdle()
// Then
testObserver
.assertNoValues()
.finish()
verify(inverse = true) {
fakeActiveSessionHolder.fakeSession.fakeSpaceService.getSpaceSummaries(queryParams)
fakeActiveSessionHolder.fakeSession.fakeSpaceService.getSpaceSummariesLive(queryParams)
}
}
private fun givenSpaceQueryParams(): SpaceSummaryQueryParams {
return mockk()
}
}

View File

@ -46,6 +46,7 @@ class FakeSession(
val fakeUserService: FakeUserService = FakeUserService(),
private val fakeEventService: FakeEventService = FakeEventService(),
val fakeSessionAccountDataService: FakeSessionAccountDataService = FakeSessionAccountDataService(),
val fakeSpaceService: FakeSpaceService = FakeSpaceService(),
) : Session by mockk(relaxed = true) {
init {
@ -66,6 +67,7 @@ class FakeSession(
override fun pushersService() = fakePushersService
override fun accountDataService() = fakeSessionAccountDataService
override fun userService() = fakeUserService
override fun spaceService() = fakeSpaceService
fun givenVectorStore(vectorSessionStore: VectorSessionStore) {
coEvery {

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 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.MutableLiveData
import io.mockk.every
import io.mockk.mockk
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.space.SpaceService
class FakeSpaceService : SpaceService by mockk() {
fun givenGetSpaceSummariesLiveReturns(roomSummaries: List<RoomSummary>): LiveData<List<RoomSummary>> {
return MutableLiveData(roomSummaries).also {
every { getSpaceSummariesLive(any()) } returns it
}
}
fun givenGetSpaceSummariesReturns(roomSummaries: List<RoomSummary>) {
every { getSpaceSummaries(any()) } returns roomSummaries
}
}