Skip to content

Commit

Permalink
ANDROID-15075 add few tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeprubio committed Aug 28, 2024
1 parent cd7d0d4 commit 104b32f
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.telefonica.mistica.compose.list

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn
import androidx.compose.ui.test.isToggleable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performClick
import com.telefonica.mistica.compose.tag.Tag
import com.telefonica.mistica.compose.theme.MisticaTheme
import com.telefonica.mistica.compose.theme.brand.MovistarBrand
import com.telefonica.mistica.testutils.ScreenshotsTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class ListRowItemWithCheckBoxTest : ScreenshotsTest() {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check ListRowItemWithCheckBox Off`() {
`when ListRowItemWithCheckBox`(checked = false)

`then is NOT checked`()
`then screenshot is OK`()
}

@Test
fun `check ListRowItemWithCheckBox On`() {
`when ListRowItemWithCheckBox`(checked = true)

`then is checked`()
`then screenshot is OK`()
}

@Test
fun `check ListRowItemWithCheckBox state change`() {
`given ListRowItemWithCheckBox`(checked = false)
`given is NOT checked`()

`when toggling the state`()

`then is checked`()
}

private fun `given ListRowItemWithCheckBox`(checked: Boolean = false) =
`when ListRowItemWithCheckBox`(checked)

private fun `given is NOT checked`() =
`then is NOT checked`()

private fun `when ListRowItemWithCheckBox`(checked: Boolean = false) {
composeTestRule.setContent {
var isChecked by remember { mutableStateOf(checked) }
MisticaTheme(brand = MovistarBrand) {
ListRowItemWithCheckBox(
listRowIcon = null,
headline = Tag("Promo"),
title = "title",
subtitle = "Subtitle",
description = "Description",
checked = isChecked,
onCheckedChange = { isChecked = it},
)
}
}
}

private fun `when toggling the state`() {
composeTestRule.onNode(isToggleable()).performClick()
}

private fun `then screenshot is OK`() {
compareScreenshot(composeTestRule.onRoot())
}

private fun `then is NOT checked`() {
composeTestRule.onNode(isToggleable()).assertIsOff()
}

private fun `then is checked`() {
composeTestRule.onNode(isToggleable()).assertIsOn()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.telefonica.mistica.compose.list

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assertIsOff
import androidx.compose.ui.test.assertIsOn
import androidx.compose.ui.test.isToggleable
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performClick
import com.telefonica.mistica.compose.tag.Tag
import com.telefonica.mistica.compose.theme.MisticaTheme
import com.telefonica.mistica.compose.theme.brand.MovistarBrand
import com.telefonica.mistica.testutils.ScreenshotsTest
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
internal class ListRowItemWithSwitchTest : ScreenshotsTest() {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun `check ListRowItemWithSwitch Off`() {
`when ListRowItemWithSwitch`(checked = false)

`then is NOT checked`()
`then screenshot is OK`()
}

@Test
fun `check ListRowItemWithSwitch On`() {
`when ListRowItemWithSwitch`(checked = true)

`then is checked`()
`then screenshot is OK`()
}

@Test
fun `check ListRowItemWithSwitch state change`() {
`given ListRowItemWithSwitch`(checked = false)
`given is NOT checked`()

`when toggling the state`()

`then is checked`()
}

private fun `given ListRowItemWithSwitch`(checked: Boolean = false) =
`when ListRowItemWithSwitch`(checked)

private fun `given is NOT checked`() =
`then is NOT checked`()

private fun `when ListRowItemWithSwitch`(checked: Boolean = false) {
composeTestRule.setContent {
var isChecked by remember { mutableStateOf(checked) }
MisticaTheme(brand = MovistarBrand) {
ListRowItemWithSwitch(
listRowIcon = null,
headline = Tag("Promo"),
title = "title",
subtitle = "Subtitle",
description = "Description",
checked = isChecked,
onCheckedChange = { isChecked = it},
)
}
}
}

private fun `when toggling the state`() {
composeTestRule.onNode(isToggleable()).performClick()
}

private fun `then screenshot is OK`() {
compareScreenshot(composeTestRule.onRoot())
}

private fun `then is NOT checked`() {
composeTestRule.onNode(isToggleable()).assertIsOff()
}

private fun `then is checked`() {
composeTestRule.onNode(isToggleable()).assertIsOn()
}
}

0 comments on commit 104b32f

Please sign in to comment.