Skip to content

Instantly share code, notes, and snippets.

View mcatta's full-sized avatar
📚
Learning

Marco Cattaneo mcatta

📚
Learning
View GitHub Profile
spec {
// ContentState
inState<GithubState.ContentState> {
on { action: GithubAction.TypeOwner, state: State<GithubState.ContentState> ->
state.mutate { copy(owner = action.input) }
}
on { _: GithubAction.Confirm, state: State<GithubState.ContentState> ->
val owner = state.snapshot.owner
if (owner.isNotBlank()) {
@ViewModelScoped
class GithubStateMachine @Inject constructor(
private val githubRepository: GithubRepository
) : FlowReduxStateMachine<GithubState, GithubAction>(
initialState = GithubState.ContentState(owner = "", repositories = emptyList())
) {
init {
spec {
// spec
}
sealed interface GithubState {
data class Load(val owner: String) : GithubState
data class Error(val e: Throwable, val owner: String) : GithubState
data class ContentState(
val repositories: List<Repository> = emptyList(),
val owner: String = "",
) : GithubState
}
sealed interface GithubAction {
@mcatta
mcatta / Screen.kt
Last active October 26, 2022 13:11
Column {
Row {
TextField(
placeholder = { Text(text = "Owner") },
value = contentState.owner, onValueChange = { /* On type */ }
)
Button(
content = { Text(text = "OK") },
onClick = { /* Confirm */ }
)
interface GithubRepository {
suspend fun repositoryByOwner(owner: String): Either<AppError, List<Repository>>
}
@mcatta
mcatta / RepositoriesListingTest.kt
Created October 4, 2021 20:58
UI testing with robot pattern
@HiltAndroidTest
internal class RepositoriesListingTest: BaseAndroidComposeTest() {
override fun provideTestInstance() = this
@Test
fun testConfirmButtonBehaviour() {
welcomeRobotScreen {
isConfirmEnabled(false)
typeUsername("mcatta")
@mcatta
mcatta / WelcomeRobotScreen.kt
Created October 4, 2021 20:57
WelcomeRobotScreen sample
internal class WelcomeRobotScreen(
private val baseAndroidComposeTest: BaseAndroidComposeTest
) : BaseRobotScreen(
baseAndroidComposeTest.composeTestRule
) {
private val usernameField by lazy { composeTestRule.onNode(hasText(getString(R.string.welcome_username_hint))) }
private val confirmButton by lazy { composeTestRule.onNode(hasText(getString(R.string.welcome_confirm_button))) }
@mcatta
mcatta / onNodeSamples.kt
Created October 4, 2021 20:56
Search node by semantics
// Search by contentDescription
composeTestRule.
onNodeWithContentDescription(label = "My fancy button").assertExists()
// Search by a text (for example a button label)
composeTestRule.onNode(hasText("My text label")).assertExists()
// Search by testTag
composeTestRule.onNode(hasTestTag("test_tag")).assertExists()
@mcatta
mcatta / MyButton.kt
Created October 4, 2021 20:53
Sample about semantics Modifier
MyButton(
modifier = Modifier.semantics { contentDescription = "My fancy button" }
)
package com.marand.domain.user.interactor
import com.marand.domain.MainCoroutineRule
import com.marand.domain.UseCase
import com.marand.domain.factory.user.UsersFactory
import com.marand.domain.user.entity.UserEntity
import com.marand.domain.user.repository.UserRepository
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk