Adding unit tests on CanTogglePushNotificationsViaPusherUseCase

This commit is contained in:
Maxime NATUREL 2022-11-04 11:47:23 +01:00
parent 18929324fe
commit e5e971683b
3 changed files with 75 additions and 1 deletions

View File

@ -25,7 +25,6 @@ import javax.inject.Inject
class CanTogglePushNotificationsViaPusherUseCase @Inject constructor() {
// TODO add unit tests
fun execute(session: Session): Flow<Boolean> {
return session
.homeServerCapabilitiesService()

View File

@ -0,0 +1,65 @@
/*
* 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.features.settings.devices.v2.notification
import im.vector.app.test.fakes.FakeFlowLiveDataConversions
import im.vector.app.test.fakes.FakeSession
import im.vector.app.test.fakes.givenAsFlow
import im.vector.app.test.fixtures.aHomeServerCapabilities
import io.mockk.unmockkAll
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.After
import org.junit.Before
import org.junit.Test
private val A_HOMESERVER_CAPABILITIES = aHomeServerCapabilities(canRemotelyTogglePushNotificationsOfDevices = true)
class CanTogglePushNotificationsViaPusherUseCaseTest {
private val fakeSession = FakeSession()
private val fakeFlowLiveDataConversions = FakeFlowLiveDataConversions()
private val canTogglePushNotificationsViaPusherUseCase =
CanTogglePushNotificationsViaPusherUseCase()
@Before
fun setUp() {
fakeFlowLiveDataConversions.setup()
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given current session when execute then flow of the toggle capability is returned`() = runTest {
// Given
fakeSession
.fakeHomeServerCapabilitiesService
.givenCapabilitiesLiveReturns(A_HOMESERVER_CAPABILITIES)
.givenAsFlow()
// When
val result = canTogglePushNotificationsViaPusherUseCase.execute(fakeSession).firstOrNull()
// Then
result shouldBeEqualTo A_HOMESERVER_CAPABILITIES.canRemotelyTogglePushNotificationsOfDevices
}
}

View File

@ -16,14 +16,24 @@
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.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.api.util.Optional
import org.matrix.android.sdk.api.util.toOptional
class FakeHomeServerCapabilitiesService : HomeServerCapabilitiesService by mockk() {
fun givenCapabilities(homeServerCapabilities: HomeServerCapabilities) {
every { getHomeServerCapabilities() } returns homeServerCapabilities
}
fun givenCapabilitiesLiveReturns(homeServerCapabilities: HomeServerCapabilities): LiveData<Optional<HomeServerCapabilities>> {
return MutableLiveData(homeServerCapabilities.toOptional()).also {
every { getHomeServerCapabilitiesLive() } returns it
}
}
}