mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 23:47:02 +05:30
Merge pull request #12 from msfjarvis/testing
Increase test surface to cover more cases
This commit is contained in:
commit
b5a3d6489b
2 changed files with 54 additions and 8 deletions
|
@ -1,8 +1,13 @@
|
||||||
package dev.msfjarvis.todo
|
package dev.msfjarvis.todo
|
||||||
|
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.ui.test.assertIsDisplayed
|
import androidx.ui.test.assertIsDisplayed
|
||||||
import androidx.ui.test.createComposeRule
|
import androidx.ui.test.createComposeRule
|
||||||
|
import androidx.ui.test.onNodeWithTag
|
||||||
import androidx.ui.test.onNodeWithText
|
import androidx.ui.test.onNodeWithText
|
||||||
|
import androidx.ui.test.performClick
|
||||||
|
import androidx.ui.test.performTextInput
|
||||||
import dev.msfjarvis.todo.data.model.TodoItem
|
import dev.msfjarvis.todo.data.model.TodoItem
|
||||||
import dev.msfjarvis.todo.ui.TodoTheme
|
import dev.msfjarvis.todo.ui.TodoTheme
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
|
@ -14,10 +19,10 @@ class MainActivityTest {
|
||||||
val composeTestRule = createComposeRule()
|
val composeTestRule = createComposeRule()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun test_item_addition() {
|
fun item_add_dialog_shows_on_fab_click() {
|
||||||
composeTestRule.setContent {
|
composeTestRule.setContent {
|
||||||
TodoTheme {
|
TodoTheme {
|
||||||
val items = arrayListOf(TodoItem("Item 1"))
|
val items = arrayListOf<TodoItem>()
|
||||||
TodoApp(
|
TodoApp(
|
||||||
items,
|
items,
|
||||||
items::add,
|
items::add,
|
||||||
|
@ -25,7 +30,44 @@ class MainActivityTest {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//onNode(hasTestTag("fab")).performClick()
|
onNodeWithTag("fab").performClick()
|
||||||
|
onNodeWithTag("item_dialog").assertIsDisplayed()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun item_addition_adds_new_entry() {
|
||||||
|
composeTestRule.setContent {
|
||||||
|
val items by mutableStateOf(arrayListOf<TodoItem>())
|
||||||
|
TodoTheme {
|
||||||
|
TodoApp(
|
||||||
|
items,
|
||||||
|
items::add,
|
||||||
|
items::remove,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onNodeWithText("Item 1").assertDoesNotExist()
|
||||||
|
onNodeWithTag("fab").performClick()
|
||||||
|
onNodeWithTag("item_name").performTextInput("Item 1")
|
||||||
|
onNodeWithTag("add_button").performClick()
|
||||||
onNodeWithText("Item 1").assertIsDisplayed()
|
onNodeWithText("Item 1").assertIsDisplayed()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun item_addition_with_empty_name_does_not_add_new_entry() {
|
||||||
|
composeTestRule.setContent {
|
||||||
|
val items by mutableStateOf(arrayListOf<TodoItem>())
|
||||||
|
TodoTheme {
|
||||||
|
TodoApp(
|
||||||
|
items,
|
||||||
|
items::add,
|
||||||
|
items::remove,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onNodeWithText("Item 1").assertDoesNotExist()
|
||||||
|
onNodeWithTag("fab").performClick()
|
||||||
|
onNodeWithTag("add_button").performClick()
|
||||||
|
onNodeWithText("Item 1").assertDoesNotExist()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,7 @@ import androidx.compose.runtime.rememberCoroutineScope
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.setContent
|
import androidx.compose.ui.platform.setContent
|
||||||
import androidx.compose.ui.semantics.semantics
|
import androidx.compose.ui.platform.testTag
|
||||||
import androidx.compose.ui.semantics.testTag
|
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.ui.tooling.preview.Preview
|
import androidx.ui.tooling.preview.Preview
|
||||||
|
@ -64,6 +63,7 @@ fun TodoApp(
|
||||||
ItemAddDialog(
|
ItemAddDialog(
|
||||||
showingDialog = showingDialog,
|
showingDialog = showingDialog,
|
||||||
onAdd = onAdd,
|
onAdd = onAdd,
|
||||||
|
modifier = Modifier.testTag("item_dialog")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ fun TodoApp(
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onClick = { showingDialog.value = true },
|
onClick = { showingDialog.value = true },
|
||||||
elevation = 8.dp,
|
elevation = 8.dp,
|
||||||
modifier = Modifier.semantics { testTag = "fab" }
|
modifier = Modifier.testTag("fab")
|
||||||
) {
|
) {
|
||||||
IconResource(
|
IconResource(
|
||||||
resourceId = R.drawable.ic_exposure_plus_1_24dp,
|
resourceId = R.drawable.ic_exposure_plus_1_24dp,
|
||||||
|
@ -96,6 +96,7 @@ fun TodoApp(
|
||||||
fun ItemAddDialog(
|
fun ItemAddDialog(
|
||||||
showingDialog: MutableState<Boolean>,
|
showingDialog: MutableState<Boolean>,
|
||||||
onAdd: (item: TodoItem) -> Unit,
|
onAdd: (item: TodoItem) -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
var newItemName by mutableStateOf(TextFieldValue(""))
|
var newItemName by mutableStateOf(TextFieldValue(""))
|
||||||
val hideDialog = { showingDialog.value = false }
|
val hideDialog = { showingDialog.value = false }
|
||||||
|
@ -107,6 +108,7 @@ fun ItemAddDialog(
|
||||||
value = newItemName,
|
value = newItemName,
|
||||||
onValueChange = { newItemName = it },
|
onValueChange = { newItemName = it },
|
||||||
label = { Text(text = "Name") },
|
label = { Text(text = "Name") },
|
||||||
|
modifier = Modifier.testTag("item_name")
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
confirmButton = {
|
confirmButton = {
|
||||||
|
@ -117,11 +119,13 @@ fun ItemAddDialog(
|
||||||
newItemName = TextFieldValue("")
|
newItemName = TextFieldValue("")
|
||||||
hideDialog.invoke()
|
hideDialog.invoke()
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
modifier = Modifier.testTag("add_button")
|
||||||
) {
|
) {
|
||||||
Text(text = "Add")
|
Text(text = "Add")
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
modifier = Modifier then modifier,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue