Skip to content

Commit

Permalink
pop back stack on back pressed
Browse files Browse the repository at this point in the history
  • Loading branch information
toluo-stripe committed Sep 27, 2024
1 parent 46c2d58 commit 7f65fd9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ internal class LinkActivityViewModel : ViewModel() {
}

private fun handleBackPressed() {
dismissWithResult?.invoke(LinkActivityResult.Canceled())
navController?.let { navController ->
if (!navController.popBackStack()) {
dismissWithResult?.invoke(LinkActivityResult.Canceled())
}
}
}

fun unregisterActivity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
Expand All @@ -35,12 +37,25 @@ internal class LinkActivityViewModelTest {
}

@Test
fun `test that cancel result is called on back pressed`() = runTest(dispatcher) {
fun `test that cancel result is called on back pressed with empty stack`() = runTest(dispatcher) {
whenever(navController.popBackStack()).thenReturn(false)

vm.handleViewAction(LinkAction.BackPressed)

verify(navController).popBackStack()
verify(dismissWithResult).invoke(LinkActivityResult.Canceled())
}

@Test
fun `test that cancel result is called on back pressed with non-empty stack`() = runTest(dispatcher) {
whenever(navController.popBackStack()).thenReturn(true)

vm.handleViewAction(LinkAction.BackPressed)

verify(navController).popBackStack()
verify(dismissWithResult, times(0)).invoke(LinkActivityResult.Canceled())
}

@Test
fun `test that activity unregister removes dismissWithResult and nav controller`() = runTest(dispatcher) {
vm.unregisterActivity()
Expand Down

0 comments on commit 7f65fd9

Please sign in to comment.