diff --git a/app/src/androidTest/java/dev/msfjarvis/todo/MainActivityTest.kt b/app/src/androidTest/java/dev/msfjarvis/todo/MainActivityTest.kt index 2dc977b5..1439d455 100644 --- a/app/src/androidTest/java/dev/msfjarvis/todo/MainActivityTest.kt +++ b/app/src/androidTest/java/dev/msfjarvis/todo/MainActivityTest.kt @@ -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() 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()) + 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()) + TodoTheme { + TodoApp( + items, + items::add, + items::remove, + ) + } + } + onNodeWithText("Item 1").assertDoesNotExist() + onNodeWithTag("fab").performClick() + onNodeWithTag("add_button").performClick() + onNodeWithText("Item 1").assertDoesNotExist() + } } diff --git a/app/src/main/java/dev/msfjarvis/todo/MainActivity.kt b/app/src/main/java/dev/msfjarvis/todo/MainActivity.kt index 56e1a3c5..c5d0f85e 100644 --- a/app/src/main/java/dev/msfjarvis/todo/MainActivity.kt +++ b/app/src/main/java/dev/msfjarvis/todo/MainActivity.kt @@ -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, 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, ) }