Skip to content

Commit

Permalink
fix questionnaire composable integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
eldcn committed Nov 25, 2024
1 parent e06e3f9 commit 0253b5d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
package edu.stanford.bdh.engagehf.questionnaire

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import edu.stanford.bdh.engagehf.questionnaire.composables.QuestionnaireTestComposable
import edu.stanford.bdh.engagehf.questionnaire.simulators.QuestionnaireTestSimulator
import org.junit.Before
import edu.stanford.spezi.core.design.component.ComposeContentActivity
import org.junit.Rule
import org.junit.Test

@HiltAndroidTest
class QuestionnaireTest {

@get: Rule
private val composeRule = createComposeRule()
@get:Rule
val hiltRule = HiltAndroidRule(this)

@Before
fun init() {
try {
composeRule.setContent {
QuestionnaireTestComposable()
}
} catch (error: Throwable) {
// TODO: I currently do not know how to fix this
// and how to set the view up to work in a test environment
// - help much appreciated!
println("Error occurred: $error")
}
}
@get:Rule
val composeTestRule = createAndroidComposeRule<ComposeContentActivity>()

@Test
fun testQuestionnaireComposable() {
// TODO: This should actually contain logic, but is skipped for now
fun testQuestionnaireComposableDisplay() {
composeTestRule.activity.setScreen { QuestionnaireTestComposable() }
questionnaireComposable {
println("Empty test")
assertIsDisplayed()
}
}

private fun questionnaireComposable(block: QuestionnaireTestSimulator.() -> Unit) {
QuestionnaireTestSimulator(composeRule).apply { block() }
QuestionnaireTestSimulator(composeTestRule).apply { block() }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package edu.stanford.bdh.engagehf.questionnaire.simulators

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.ComposeTestRule
import edu.stanford.bdh.engagehf.questionnaire.spezi.QuestionnaireComposableTestIdentifiers
import edu.stanford.spezi.core.testing.onNodeWithIdentifier

data class QuestionnaireTestSimulator(
val composeTestRule: ComposeTestRule,
)
class QuestionnaireTestSimulator(
composeTestRule: ComposeTestRule,
) {
private val root =
composeTestRule.onNodeWithIdentifier(QuestionnaireComposableTestIdentifiers.ROOT)

fun assertIsDisplayed() {
root.assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import androidx.fragment.compose.rememberFragmentState
import ca.uhn.fhir.context.FhirContext
import com.google.android.fhir.datacapture.QuestionnaireFragment
import edu.stanford.spezi.core.design.component.StringResource
import edu.stanford.spezi.core.design.component.StringResource.Companion.invoke
import edu.stanford.spezi.core.design.theme.SpeziTheme
import edu.stanford.spezi.core.design.theme.ThemePreviews
import edu.stanford.spezi.core.utils.extensions.testIdentifier
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.Questionnaire

Expand Down Expand Up @@ -84,7 +82,9 @@ fun QuestionnaireComposable(
val fragmentState = rememberFragmentState()
AndroidFragment<QuestionnaireFragment>(

Check warning on line 83 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L82-L83

Added lines #L82 - L83 were not covered by tests
fragmentState = fragmentState,
modifier = modifier.fillMaxSize(),
modifier = modifier
.fillMaxSize()
.testIdentifier(QuestionnaireComposableTestIdentifiers.ROOT),

Check warning on line 87 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L85-L87

Added lines #L85 - L87 were not covered by tests
arguments = arguments,
) { fragment ->
fragment.setFragmentResultListener(
Expand Down Expand Up @@ -115,6 +115,10 @@ fun QuestionnaireComposable(
}
}

Check warning on line 116 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L113-L116

Added lines #L113 - L116 were not covered by tests

enum class QuestionnaireComposableTestIdentifiers {
ROOT,
}

Check warning on line 120 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L119-L120

Added lines #L119 - L120 were not covered by tests

@Composable
private fun QuestionnaireCancelAlert(
modifier: Modifier = Modifier,

Check warning on line 124 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L124

Added line #L124 was not covered by tests
Expand All @@ -136,17 +140,3 @@ private fun QuestionnaireCancelAlert(
}

Check warning on line 140 in app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/kotlin/edu/stanford/bdh/engagehf/questionnaire/spezi/QuestionnaireComposable.kt#L139-L140

Added lines #L139 - L140 were not covered by tests
)
}

@ThemePreviews
@Composable
private fun QuestionnaireComposablePreview() {
SpeziTheme(isPreview = true) {
// TODO: Preview is unfortunately not working due to missing "FragmentActivity"
// - is there a fix for that or simply no way to preview this?
QuestionnaireComposable(
remember { Questionnaire() }
) {
println("Received result: $it")
}
}
}

0 comments on commit 0253b5d

Please sign in to comment.