Skip to content

Commit f3c86d7

Browse files
tomaszrybakiewiczabhishek1508
authored andcommitted
Unit-tests for RecenterButtonComponentContractImpl
1 parent baf7b09 commit f3c86d7

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

libnavui-dropin/src/main/java/com/mapbox/navigation/dropin/camera/RecenterButtonComponentContractImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class RecenterButtonComponentContractImpl(
2525
transform = ::isVisible
2626
).stateIn(
2727
scope,
28-
SharingStarted.WhileSubscribed(),
28+
SharingStarted.Eagerly,
2929
isVisible(store.state.value.camera.cameraMode, store.state.value.navigation)
3030
)
3131

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.mapbox.navigation.dropin.component.recenter
2+
3+
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
4+
import com.mapbox.navigation.dropin.util.TestStore
5+
import com.mapbox.navigation.testing.MainCoroutineRule
6+
import com.mapbox.navigation.ui.app.internal.camera.CameraAction
7+
import com.mapbox.navigation.ui.app.internal.camera.TargetCameraMode
8+
import com.mapbox.navigation.ui.app.internal.navigation.NavigationState
9+
import io.mockk.mockk
10+
import io.mockk.spyk
11+
import io.mockk.verify
12+
import kotlinx.coroutines.ExperimentalCoroutinesApi
13+
import kotlinx.coroutines.yield
14+
import org.junit.Assert.assertFalse
15+
import org.junit.Assert.assertTrue
16+
import org.junit.Before
17+
import org.junit.Rule
18+
import org.junit.Test
19+
20+
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class, ExperimentalCoroutinesApi::class)
21+
class RecenterButtonComponentContractImplTest {
22+
23+
@get:Rule
24+
var coroutineRule = MainCoroutineRule()
25+
26+
private lateinit var store: TestStore
27+
private lateinit var sut: RecenterButtonComponentContractImpl
28+
29+
@Before
30+
fun setUp() {
31+
store = spyk(TestStore())
32+
sut = RecenterButtonComponentContractImpl(coroutineRule.coroutineScope, store)
33+
}
34+
35+
@Test
36+
fun `isVisible - should return TRUE when camera is Idle and not in RoutePreview`() =
37+
coroutineRule.runBlockingTest {
38+
store.updateState {
39+
it.copy(
40+
camera = it.camera.copy(cameraMode = TargetCameraMode.Idle),
41+
navigation = NavigationState.FreeDrive
42+
)
43+
}
44+
yield() // yielding to allow isVisible StateFlow run its logic
45+
assertTrue(
46+
"expected TRUE when camera is Idle and not in RoutePreview",
47+
sut.isVisible.value
48+
)
49+
}
50+
51+
@Test
52+
fun `isVisible - should return FALSE when camera not Idle or in RoutePreview`() =
53+
coroutineRule.runBlockingTest {
54+
store.updateState {
55+
it.copy(
56+
camera = it.camera.copy(cameraMode = TargetCameraMode.Following),
57+
navigation = NavigationState.FreeDrive
58+
)
59+
}
60+
yield() // yielding to allow isVisible StateFlow run its logic
61+
assertFalse("expected FALSE when camera not Idle", sut.isVisible.value)
62+
63+
store.updateState {
64+
it.copy(
65+
camera = it.camera.copy(cameraMode = TargetCameraMode.Idle),
66+
navigation = NavigationState.RoutePreview
67+
)
68+
}
69+
yield() // yielding to allow isVisible StateFlow run its logic
70+
assertFalse("expected FALSE when NavigationState not RoutePreview", sut.isVisible.value)
71+
}
72+
73+
@Test
74+
fun `onClick should dispatch SetCameraMode action`() {
75+
sut.onClick(mockk())
76+
77+
verify { store.dispatch(CameraAction.SetCameraMode(TargetCameraMode.Following)) }
78+
}
79+
}

0 commit comments

Comments
 (0)