-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SavedState UI Test for Common Target
- Loading branch information
1 parent
6175c09
commit b674d3f
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
eungabi/src/desktopTest/kotlin/com/easternkite/eungabi/navigation/EunGabiSavedStateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.easternkite.eungabi.navigation | ||
|
||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import androidx.compose.ui.test.assertTextEquals | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.runComposeUiTest | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import kotlin.test.Test | ||
|
||
class EunGabiSavedStateTest { | ||
@OptIn(ExperimentalTestApi::class) | ||
@Test | ||
fun savedStateTest() = | ||
runComposeUiTest { | ||
setContent { | ||
HiltNavHost() | ||
} | ||
waitForIdle() | ||
onNodeWithTag("MainButton").performClick() | ||
waitForIdle() | ||
onNodeWithTag("state").assertTextEquals("SecondState") | ||
} | ||
} | ||
|
||
@Composable | ||
fun HiltNavHost(modifier: Modifier = Modifier) { | ||
val controller = rememberEunGabiController() | ||
EunGabiNavHost( | ||
modifier = modifier, | ||
controller = controller, | ||
startDestination = "Main" | ||
) { | ||
composable("Main") { | ||
Button( | ||
onClick = { controller.navigate("Second?state=SecondState") }, | ||
modifier = Modifier.testTag("MainButton") | ||
) { | ||
Text("Main") | ||
} | ||
} | ||
composable("Second") { | ||
HiltView() | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
fun HiltView(viewModel: TestSavedStateViewModel = eunGabiViewModel { TestSavedStateViewModel(it) }) { | ||
val state by viewModel.state.collectAsState() | ||
Text(text = state, modifier = Modifier.testTag("state")) | ||
} | ||
|
||
class TestSavedStateViewModel( | ||
savedStateHandle: SavedStateHandle | ||
) : ViewModel() { | ||
val state = savedStateHandle.getStateFlow("state", "state") | ||
} |