Skip to content

Commit

Permalink
🧪: add an unit test for useState
Browse files Browse the repository at this point in the history
  • Loading branch information
junerver committed Mar 18, 2024
1 parent 7f0df60 commit 01f6813
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-ui-test = { group = "androidx.compose.ui", name = "ui-test" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
Expand Down
17 changes: 15 additions & 2 deletions hooks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ android {

defaultConfig {
minSdk = 24
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -35,17 +36,29 @@ tasks.dokkaHtml {


dependencies {
api(platform(libs.androidx.compose.bom))
androidTestImplementation(platform(libs.androidx.compose.bom))

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.lifecycle.runtime.compose)

// Testing dependencies
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

api(platform(libs.androidx.compose.bom))
// Compose testing dependencies
androidTestImplementation(libs.androidx.ui.test)
androidTestImplementation(libs.androidx.ui.test.junit4)
androidTestImplementation(libs.androidx.material3)
debugImplementation(libs.androidx.ui.test.manifest)

// Compose
api(libs.androidx.ui)
implementation(libs.androidx.lifecycle.runtime.compose)

// Kotlin and extension
implementation(platform(libs.kotlin.bom))
implementation(libs.kotlin.reflect)
api(libs.ktx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package xyz.junerver.compose.hooks.useState

import android.os.Build
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.MutableDoubleState
import androidx.compose.runtime.MutableFloatState
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableLongState
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.printToLog
import androidx.test.filters.SdkSuppress
import kotlin.reflect.full.isSubclassOf
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import xyz.junerver.compose.hooks.useState

/**
* Description:
*
* @author Junerver date: 2024/3/15-15:15 Email: [email protected]
* Version: v1.0
*/
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.O)
class UseStateTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun testStateIsNumber() {
composeTestRule.setContent {
val intState = useState(default = 1)
val longState = useState(default = 1L)
val floatState = useState(default = 1F)
val doubleState = useState(default = 1.0)
// by use `useState` create the corresponding number type state
assertEquals(true, intState::class.isSubclassOf(MutableIntState::class))
assertEquals(true, longState::class.isSubclassOf(MutableLongState::class))
assertEquals(true, floatState::class.isSubclassOf(MutableFloatState::class))
assertEquals(true, doubleState::class.isSubclassOf(MutableDoubleState::class))
}

composeTestRule.onRoot().printToLog("currentLabelExists")
}

@Test
fun testCreateControlledComponents() {
composeTestRule.setContent {
val (state, setState) = useState("")
OutlinedTextField(value = state, onValueChange = setState, label = { Text("Label") })
}
val textField = composeTestRule.onNode(hasText("Label"))
textField.performTextInput("Hello, Compose!")
composeTestRule.onNode(hasText("Hello, Compose!")).assertExists()
textField.performTextClearance()
composeTestRule.onNode(hasText("")).assertExists()
}
}

0 comments on commit 01f6813

Please sign in to comment.