This file contains hidden or 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
| internal class MyComposableTest { | |
| @get:Rule | |
| val composeTestRule = createComposeRule() | |
| @Before | |
| fun setup() { | |
| composeTestRule.setContent { | |
| MyTheme { | |
| MyComposable() |
This file contains hidden or 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
| @Test | |
| @AllowFlaky(attempts = 5) | |
| fun test_TitleIsShownWhenLoaded() { | |
| initComposable(state = MyState.Loaded) | |
| composeTestRule.onNodeWithText("Title").assertIsDisplayed() | |
| } |
This file contains hidden or 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
| internal class TaskListViewModel : ViewModel() { | |
| private val _state = MutableStateFlow<TaskListViewState>(TaskListViewState.Empty) | |
| val state: StateFlow<TaskListViewState> | |
| get() = _state | |
| fun loadTasks() = viewModelScope.launch { | |
| loadAllTasksUseCase().collect { tasks -> | |
| _state.value = if (tasks.isNotEmpty()) { |
This file contains hidden or 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
| @Composable | |
| private fun TaskListLoader(viewModel: TaskListViewModel = getViewModel()) { | |
| val viewState by viewModel.state.collectAsState() | |
| } |
This file contains hidden or 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
| val state: StateFlow<TaskListViewState> = flow { | |
| loadAllTasksUseCase().collect { tasks -> | |
| val state = if (tasks.isNotEmpty()) { | |
| TaskListViewState.Loaded(tasks) | |
| } else { | |
| TaskListViewState.Empty | |
| } | |
| emit(state) | |
| } | |
| }.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), TaskListViewState.Empty) |
This file contains hidden or 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
| fun loadTaskList(): Flow<TaskListViewState> = flow { | |
| loadAllTasksUseCase().collect { tasks -> | |
| val state = if (tasks.isNotEmpty()) { | |
| TaskListViewState.Loaded(tasks) | |
| } else { | |
| TaskListViewState.Empty | |
| } | |
| emit(state) | |
| } | |
| } |
This file contains hidden or 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
| val viewState by viewModel.loadTaskList().collectAsState() |
This file contains hidden or 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
| fun loadTaskInfo(taskId: TaskId): Flow<TaskDetailState> = flow { | |
| val task = loadTaskUseCase(taskId = taskId.value) | |
| if (task != null) { | |
| emit(TaskDetailState.Loaded(task)) | |
| } else { | |
| emit(TaskDetailState.Error) | |
| } | |
| } |
This file contains hidden or 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
| internal class MyComposableTest { | |
| @get:Rule | |
| val composeTestRule = createComposeRule() | |
| @Test | |
| fun test_TitleIsShownWhenLoaded() { | |
| initComposable(state = MyState.Loaded) | |
| composeTestRule.onNodeWithText("Title").assertIsDisplayed() | |
| } |
This file contains hidden or 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
| internal class MyComposableTest { | |
| @get:Rule | |
| val flakyRule = FlakyTestRule() | |
| @get:Rule | |
| val composeTestRule = createEmptyComposeRule() | |
| private lateinit var scenario: ActivityScenario<ComponentActivity> |