Skip to content

Commit

Permalink
✅ Add tests to JopiterTopBar
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColman committed Jan 7, 2024
1 parent 5e5325e commit 9164bfe
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ android {
dependencies {
// Compose
implementation(libs.bundles.compose)
compileOnly(libs.compose.ui.tooling)
debugImplementation(libs.compose.ui.tooling)
androidTestImplementation(libs.compose.ui.test.junit4)
androidTestImplementation(libs.compose.ui.test.manifest)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package app.jopiter.navigation

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class JopiterTopBarTest {

@get:Rule
val composeRule = createComposeRule()

private var clickCount = 0

@Before
fun setContent() {
composeRule.setContent {
JopiterTopBar { clickCount++ }
}
}


@Test
fun should_include_app_name_in_top_bar() {
composeRule.barTitle().assertIsDisplayed().assertTextEquals("Jopiter")
}

@Test
fun should_run_on_click_when_clicking_navigation_button() {
composeRule.barIcon().performClick()
assertEquals(1, clickCount)
composeRule.barIcon().performClick()
assertEquals(2, clickCount)
}

private fun ComposeTestRule.barTitle() = onNodeWithTag("top_app_bar_title")
private fun ComposeTestRule.barIcon() = onNodeWithTag("top_app_bar_icon")
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="androidx.activity.ComponentActivity" />
</application>

</manifest>
2 changes: 1 addition & 1 deletion app/src/main/java/app/jopiter/navigation/JopiterTopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun JopiterTopBar(onNavigationClick: () -> Unit) {

TopAppBar(
title = { Text(title, Modifier.testTag("top_app_bar_title")) },
navigationIcon = { Icon(Icons.Default.Menu, iconDescription, Modifier.clickable { onNavigationClick() }) },
navigationIcon = { Icon(Icons.Default.Menu, iconDescription, Modifier.clickable { onNavigationClick() }.testTag("top_app_bar_icon")) },
modifier = Modifier.testTag("top_app_bar")
)
}

0 comments on commit 9164bfe

Please sign in to comment.