Adding unit tests for ViewNavigators

This commit is contained in:
Maxime NATUREL 2023-02-03 11:39:01 +01:00
parent b75787a4f4
commit beecd8366b
5 changed files with 177 additions and 2 deletions

View file

@ -20,7 +20,6 @@ import android.content.Context
import im.vector.app.features.navigation.Navigator
import javax.inject.Inject
// TODO add unit tests
class RoomPollDetailNavigator @Inject constructor(
private val navigator: Navigator,
) {

View file

@ -20,7 +20,6 @@ import android.content.Context
import im.vector.app.features.roomprofile.polls.detail.ui.RoomPollDetailActivity
import javax.inject.Inject
// TODO add unit tests
class RoomPollsListNavigator @Inject constructor() {
fun goToPollDetails(context: Context, pollId: String, roomId: String, isEnded: Boolean) {

View file

@ -0,0 +1,60 @@
/*
* 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.roomprofile.polls.detail.ui
import android.content.Context
import im.vector.app.test.fakes.FakeNavigator
import io.mockk.mockk
import org.junit.Test
internal class RoomPollDetailNavigatorTest {
private val fakeNavigator = FakeNavigator()
private val roomPollDetailNavigator = RoomPollDetailNavigator(
navigator = fakeNavigator.instance,
)
@Test
fun `given main navigator when goToTimelineEvent then correct method main navigator is called`() {
// Given
val aContext = mockk<Context>()
val aRoomId = "roomId"
val anEventId = "eventId"
fakeNavigator.givenOpenRoomSuccess(
context = aContext,
roomId = aRoomId,
eventId = anEventId,
buildTask = true,
isInviteAlreadyAccepted = false,
trigger = null,
)
// When
roomPollDetailNavigator.goToTimelineEvent(aContext, aRoomId, anEventId)
// Then
fakeNavigator.verifyOpenRoom(
context = aContext,
roomId = aRoomId,
eventId = anEventId,
buildTask = true,
isInviteAlreadyAccepted = false,
trigger = null,
)
}
}

View file

@ -0,0 +1,66 @@
/*
* 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.roomprofile.polls.list.ui
import android.content.Intent
import im.vector.app.features.roomprofile.polls.detail.ui.RoomPollDetailActivity
import im.vector.app.test.fakes.FakeContext
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Before
import org.junit.Test
internal class RoomPollsListNavigatorTest {
private val fakeContext = FakeContext()
private val roomPollsListNavigator = RoomPollsListNavigator()
@Before
fun setUp() {
mockkObject(RoomPollDetailActivity.Companion)
}
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given info about poll when goToPollDetails then it starts the correct activity`() {
// Given
val aPollId = "pollId"
val aRoomId = "roomId"
val isEnded = true
val intent = givenIntentForPollDetails(aPollId, aRoomId, isEnded)
fakeContext.givenStartActivity(intent)
// When
roomPollsListNavigator.goToPollDetails(fakeContext.instance, aPollId, aRoomId, isEnded)
// Then
fakeContext.verifyStartActivity(intent)
}
private fun givenIntentForPollDetails(pollId: String, roomId: String, isEnded: Boolean): Intent {
val intent = mockk<Intent>()
every { RoomPollDetailActivity.newIntent(fakeContext.instance, pollId, roomId, isEnded) } returns intent
return intent
}
}

View file

@ -0,0 +1,51 @@
/*
* 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 android.content.Context
import im.vector.app.features.analytics.plan.ViewRoom
import im.vector.app.features.navigation.Navigator
import io.mockk.justRun
import io.mockk.mockk
import io.mockk.verify
class FakeNavigator {
val instance: Navigator = mockk()
fun givenOpenRoomSuccess(
context: Context,
roomId: String,
eventId: String?,
buildTask: Boolean,
isInviteAlreadyAccepted: Boolean,
trigger: ViewRoom.Trigger?,
) {
justRun { instance.openRoom(context, roomId, eventId, buildTask, isInviteAlreadyAccepted, trigger) }
}
fun verifyOpenRoom(
context: Context,
roomId: String,
eventId: String?,
buildTask: Boolean,
isInviteAlreadyAccepted: Boolean,
trigger: ViewRoom.Trigger?,
) {
verify { instance.openRoom(context, roomId, eventId, buildTask, isInviteAlreadyAccepted, trigger) }
}
}