Skip to content

Commit

Permalink
refactor navigation composable & follow naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
yoan authored and violoncelloCH committed Apr 6, 2024
1 parent e30d362 commit e8cd1d3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.swent.echo.compose.navigation.AppNavigationHost
import com.github.swent.echo.ui.theme.EchoTheme
import io.mockk.every
import io.mockk.mockk
Expand All @@ -24,8 +25,8 @@ class NavigationTest {
fun testNavigationActionsNavigateTo() {
composeTestRule.setContent {
val navController = rememberNavController()
appNavigationHost(navController)
val navigationActions = navigationActions(navController)
AppNavigationHost(navController)
val navigationActions = NavigationActions(navController)
for (route in Routes.entries) {
navigationActions.navigateTo(route)
assert(navController.currentDestination?.route == route.name)
Expand All @@ -43,15 +44,15 @@ class NavigationTest {
fun testNavigationActionsGoBack() {
val mockedNavController = mockk<NavHostController>(relaxed = true)
every { mockedNavController.navigateUp() } returns true
val navigationActions = navigationActions(mockedNavController)
val navigationActions = NavigationActions(mockedNavController)
navigationActions.goBack()
verify { mockedNavController.navigateUp() }
}

// test the start route is displayed
@Test
fun testAppNavHostComposableStartRoute() {
composeTestRule.setContent { EchoTheme { appNavigationHost() } }
composeTestRule.setContent { EchoTheme { AppNavigationHost() } }
composeTestRule.onNodeWithTag("signInScreen").assertIsDisplayed()
}

Expand All @@ -60,7 +61,7 @@ class NavigationTest {
fun testAppNavHostComposableMapRoute() {
composeTestRule.setContent {
val navController = rememberNavController()
appNavigationHost(navController)
AppNavigationHost(navController)
navController.navigate(Routes.MAP.name)
}
composeTestRule.onNodeWithTag("mapScreen").assertIsDisplayed()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.swent.echo.compose.navigation

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.github.swent.echo.ui.navigation.Routes

/**
* Navigation composable, it display the relevant screen based on the route and pass an instance of
* navigationActions to the screens
*
* @param navController the navigation controller, created if none is given
*/
@Composable
fun AppNavigationHost(
navController: NavHostController = rememberNavController(),
) {
NavHost(
navController = navController,
startDestination = Routes.SIGN_IN.name,
) {
composable(Routes.SIGN_IN.name) {
// placeholder for the sign in composable to test it's displayed
Text("sign in screen", modifier = Modifier.testTag("signInScreen"))
}

composable(Routes.MAP.name) {
// placeholder for the map composable
Text("map screen", modifier = Modifier.testTag("mapScreen"))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package com.github.swent.echo.ui.navigation

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController

/** The routes to the screens of the app */
enum class Routes {
Expand All @@ -20,7 +13,7 @@ enum class Routes {
*
* @param navController the navigation controller
*/
class navigationActions(val navController: NavHostController) {
class NavigationActions(val navController: NavHostController) {

// navigate to one of the routes available in Routes
fun navigateTo(route: Routes) {
Expand All @@ -32,29 +25,3 @@ class navigationActions(val navController: NavHostController) {
navController.navigateUp()
}
}

/**
* Navigation composable, it display the relevant screen based on the route and pass an instance of
* navigationActions to the screens
*
* @param navController the navigation controller, created if none is given
*/
@Composable
fun appNavigationHost(
navController: NavHostController = rememberNavController(),
) {
NavHost(
navController = navController,
startDestination = Routes.SIGN_IN.name,
) {
composable(Routes.SIGN_IN.name) {
// placeholder for the sign in composable to test it's displayed
Text("sign in screen", modifier = Modifier.testTag("signInScreen"))
}

composable(Routes.MAP.name) {
// placeholder for the map composable
Text("map screen", modifier = Modifier.testTag("mapScreen"))
}
}
}

0 comments on commit e8cd1d3

Please sign in to comment.