Skip to content

Commit

Permalink
add unit tests for navigate up and pin shortcut actions on ToolDetail…
Browse files Browse the repository at this point in the history
…sLayout
  • Loading branch information
frett committed Mar 5, 2024
1 parent d45be76 commit 7e9676d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ import org.cru.godtools.ui.tools.VariantToolCard

private val TOOL_DETAILS_HORIZONTAL_MARGIN = 32.dp

internal const val TEST_TAG_ACTION_NAVIGATE_UP = "action_up"
internal const val TEST_TAG_ACTION_OVERFLOW = "action_overflow"
internal const val TEST_TAG_ACTION_PIN_SHORTCUT = "action_pin_shortcut"
internal const val TEST_TAG_ACTION_TOOL_TRAINING = "action_tool_training"

@Composable
Expand All @@ -94,21 +97,28 @@ fun ToolDetailsLayout(state: State, modifier: Modifier = Modifier) = DrawerMenuL
TopAppBar(
title = {},
navigationIcon = {
IconButton(onClick = { eventSink(Event.NavigateUp) }) {
IconButton(
onClick = { eventSink(Event.NavigateUp) },
modifier = Modifier.testTag(TEST_TAG_ACTION_NAVIGATE_UP),
) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, null)
}
},
actions = {
if (hasShortcut) {
var showOverflow by remember { mutableStateOf(false) }

IconButton(onClick = { showOverflow = !showOverflow }) {
IconButton(
onClick = { showOverflow = !showOverflow },
modifier = Modifier.testTag(TEST_TAG_ACTION_OVERFLOW),
) {
Icon(Icons.Filled.MoreVert, null)
}
DropdownMenu(expanded = showOverflow, onDismissRequest = { showOverflow = false }) {
DropdownMenuItem(
text = { Text(stringResource(R.string.menu_add_to_home)) },
onClick = { eventSink(Event.PinShortcut) }
onClick = { eventSink(Event.PinShortcut) },
modifier = Modifier.testTag(TEST_TAG_ACTION_PIN_SHORTCUT),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.slack.circuit.test.TestEventSink
import io.mockk.every
import io.mockk.mockk
import kotlinx.collections.immutable.persistentListOf
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.Event
import org.cru.godtools.ui.tooldetails.ToolDetailsScreen.State
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -22,26 +26,61 @@ class ToolDetailsLayoutTest {
@get:Rule
val composeTestRule = createComposeRule()

private val events = TestEventSink<Event>()

// region Action - Navigate Up
fun `Action - Navigate Up`() {
val state = State(eventSink = events)
composeTestRule.setContent { ToolDetailsLayout(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_NAVIGATE_UP).assertExists().performClick()
events.assertEvent(Event.NavigateUp)
}
// endregion Action - Navigate Up

// region Action - Pin Shortcut
fun `Action - Pin Shortcut`() {
val state = State(hasShortcut = true, eventSink = events)
composeTestRule.setContent { ToolDetailsLayout(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_PIN_SHORTCUT).assertDoesNotExist()
composeTestRule.onNodeWithTag(TEST_TAG_ACTION_OVERFLOW).assertExists().performClick()
events.assertNoEvents()

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_PIN_SHORTCUT).assertExists().performClick()
events.assertEvent(Event.PinShortcut)
}

fun `Action - Pin Shortcut - hasShortcut=false`() {
val state = State(hasShortcut = false, eventSink = events)
composeTestRule.setContent { ToolDetailsLayout(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_PIN_SHORTCUT).assertDoesNotExist()
composeTestRule.onNodeWithTag(TEST_TAG_ACTION_OVERFLOW).assertDoesNotExist()
events.assertNoEvents()
}
// endregion Action - Pin Shortcut

// region ToolDetailsActions()
@Test
fun `ToolDetailsActions() - Tool Training Button - visible when manifest has tips`() {
val state = ToolDetailsScreen.State(manifest = mockk { every { hasTips } returns true })
val state = State(manifest = mockk { every { hasTips } returns true })
composeTestRule.setContent { ToolDetailsActions(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_TOOL_TRAINING).assertExists()
}

@Test
fun `ToolDetailsActions() - Tool Training Button - gone when manifest does not have tips`() {
val state = ToolDetailsScreen.State(manifest = mockk { every { hasTips } returns false })
val state = State(manifest = mockk { every { hasTips } returns false })
composeTestRule.setContent { ToolDetailsActions(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_TOOL_TRAINING).assertDoesNotExist()
}

@Test
fun `ToolDetailsActions() - Tool Training Button - gone when manifest does not exist`() {
val state = ToolDetailsScreen.State(manifest = null)
val state = State(manifest = null)
composeTestRule.setContent { ToolDetailsActions(state) }

composeTestRule.onNodeWithTag(TEST_TAG_ACTION_TOOL_TRAINING).assertDoesNotExist()
Expand All @@ -51,7 +90,7 @@ class ToolDetailsLayoutTest {
// region ToolDetailsLanguages()
@Test
fun `ToolDetailsLanguages() - No Languages`() {
val state = ToolDetailsScreen.State(availableLanguages = persistentListOf())
val state = State(availableLanguages = persistentListOf())
composeTestRule.setContent { ToolDetailsLanguages(state, true, {}) }

// The entire ToolDetailsLanguages() composable should be gone if there are no languages
Expand All @@ -60,7 +99,7 @@ class ToolDetailsLayoutTest {

@Test
fun `ToolDetailsLanguages() - Sorted Languages`() {
val state = ToolDetailsScreen.State(
val state = State(
availableLanguages = persistentListOf("Language 1", "Language 2")
)
composeTestRule.setContent { ToolDetailsLanguages(state, true, {}) }
Expand Down

0 comments on commit 7e9676d

Please sign in to comment.