-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
97 additions
and
91 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
library/src/test/java/com/telefonica/mistica/compose/button/ButtonBehaviourTest.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,64 @@ | ||
package com.telefonica.mistica.compose.button | ||
|
||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.test.junit4.createComposeRule | ||
import androidx.compose.ui.test.onNodeWithText | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.unit.dp | ||
import com.telefonica.mistica.compose.theme.MisticaTheme | ||
import com.telefonica.mistica.compose.theme.brand.MovistarBrand | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
internal class ButtonBehaviourTest { | ||
@get:Rule | ||
val composeTestRule = createComposeRule() | ||
|
||
@Test | ||
fun `check the button is clicked`() = test { | ||
`given Button`() | ||
|
||
`when the button is clicked`() | ||
|
||
`then the onClickListener has been invoked`() | ||
} | ||
|
||
private fun TestScope.`given Button`() { | ||
`when Button`() | ||
} | ||
|
||
private fun TestScope.`when Button`() { | ||
composeTestRule.setContent { | ||
MisticaTheme(brand = MovistarBrand) { | ||
Button( | ||
text = textValue, | ||
onClickListener = onClickListener, | ||
modifier = Modifier.padding(16.dp) | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun TestScope.`when the button is clicked`() { | ||
composeTestRule.onNodeWithText(textValue).performClick() | ||
} | ||
|
||
private fun TestScope.`then the onClickListener has been invoked`() { | ||
assertTrue(clicked) | ||
} | ||
|
||
private fun test(block: TestScope.() -> Unit) { | ||
TestScope().block() | ||
} | ||
|
||
private class TestScope { | ||
val textValue = "textValue" | ||
var clicked = false | ||
val onClickListener: () -> Unit = { clicked = true } | ||
} | ||
} |
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
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