Merge pull request #12 from msfjarvis/testing

Increase test surface to cover more cases
This commit is contained in:
probot-auto-merge[bot] 2020-09-08 00:40:10 +00:00 committed by GitHub
commit b5a3d6489b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 8 deletions

View file

@ -1,8 +1,13 @@
package dev.msfjarvis.todo
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.ui.test.assertIsDisplayed
import androidx.ui.test.createComposeRule
import androidx.ui.test.onNodeWithTag
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.ui.TodoTheme
import org.junit.Rule
@ -14,10 +19,10 @@ class MainActivityTest {
val composeTestRule = createComposeRule()
@Test
fun test_item_addition() {
fun item_add_dialog_shows_on_fab_click() {
composeTestRule.setContent {
TodoTheme {
val items = arrayListOf(TodoItem("Item 1"))
val items = arrayListOf<TodoItem>()
TodoApp(
items,
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()
}
@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()
}
}

View file

@ -22,8 +22,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
@ -64,6 +63,7 @@ fun TodoApp(
ItemAddDialog(
showingDialog = showingDialog,
onAdd = onAdd,
modifier = Modifier.testTag("item_dialog")
)
}
@ -73,7 +73,7 @@ fun TodoApp(
FloatingActionButton(
onClick = { showingDialog.value = true },
elevation = 8.dp,
modifier = Modifier.semantics { testTag = "fab" }
modifier = Modifier.testTag("fab")
) {
IconResource(
resourceId = R.drawable.ic_exposure_plus_1_24dp,
@ -96,6 +96,7 @@ fun TodoApp(
fun ItemAddDialog(
showingDialog: MutableState<Boolean>,
onAdd: (item: TodoItem) -> Unit,
modifier: Modifier = Modifier,
) {
var newItemName by mutableStateOf(TextFieldValue(""))
val hideDialog = { showingDialog.value = false }
@ -107,6 +108,7 @@ fun ItemAddDialog(
value = newItemName,
onValueChange = { newItemName = it },
label = { Text(text = "Name") },
modifier = Modifier.testTag("item_name")
)
},
confirmButton = {
@ -117,11 +119,13 @@ fun ItemAddDialog(
newItemName = TextFieldValue("")
hideDialog.invoke()
}
}
},
modifier = Modifier.testTag("add_button")
) {
Text(text = "Add")
}
}
},
modifier = Modifier then modifier,
)
}