From cac34090a011f332c8286361846bd5ac7ef2e988 Mon Sep 17 00:00:00 2001 From: John O'Reilly Date: Sun, 20 Oct 2024 11:24:45 +0100 Subject: [PATCH] Compose Multiplatform UI test updates --- .../screen/ClimateTraceTestsScreen.kt | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/composeApp/src/commonTest/kotlin/dev/johnoreilly/climatetrace/screen/ClimateTraceTestsScreen.kt b/composeApp/src/commonTest/kotlin/dev/johnoreilly/climatetrace/screen/ClimateTraceTestsScreen.kt index 38076e5..544a014 100644 --- a/composeApp/src/commonTest/kotlin/dev/johnoreilly/climatetrace/screen/ClimateTraceTestsScreen.kt +++ b/composeApp/src/commonTest/kotlin/dev/johnoreilly/climatetrace/screen/ClimateTraceTestsScreen.kt @@ -1,24 +1,52 @@ package dev.johnoreilly.climatetrace.screen -import androidx.compose.material3.Text import androidx.compose.ui.test.ExperimentalTestApi import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.runComposeUiTest import dev.johnoreilly.climatetrace.remote.Country +import dev.johnoreilly.climatetrace.remote.CountryEmissionsInfo +import dev.johnoreilly.climatetrace.remote.EmissionInfo +import dev.johnoreilly.climatetrace.ui.CountryInfoDetailedView import dev.johnoreilly.climatetrace.ui.CountryListView +import dev.johnoreilly.climatetrace.ui.toPercent +import dev.johnoreilly.climatetrace.viewmodel.CountryDetailsUIState import kotlin.test.Test @OptIn(ExperimentalTestApi::class) class ClimateTraceScreenTest { - private val countryList = listOf(Country("IRL", "IE", "Ireland", "Europe")) + private val country = Country("IRL", "IE", "Ireland", "Europe") + private val countryList = listOf(country) + private val countryEmissions = EmissionInfo(53_000_000.0f, 75_000_000.0f, 100_000_000.0f) + private val worldEmissions = EmissionInfo(53_000_000_000.0f, 75_000_000_000.0f, 100_000_000_000.0f) + private val countryEmissionsInfo = CountryEmissionsInfo(country = country.alpha3, + rank = 73, emissions = countryEmissions, worldEmissions = worldEmissions) + private val year = "2022" @Test - fun testCountryListScreen() = runComposeUiTest { + fun testCountryListView() = runComposeUiTest { setContent { CountryListView(countryList, null, {}) } - onNodeWithText("Ireland").assertExists() + onNodeWithText(country.name).assertExists() } + + + @Test + fun testCountryInfoDetailsView() = runComposeUiTest { + val state = CountryDetailsUIState.Success(country, + year, countryEmissionsInfo, emptyList() + ) + setContent { + CountryInfoDetailedView(state, {}) + } + + onNodeWithText(country.name).assertExists() + val millionTonnes = (countryEmissions.co2 / 1_000_000).toInt() + val percentage = (countryEmissions.co2 / worldEmissions.co2).toPercent(2) + onNodeWithText("co2 = $millionTonnes Million Tonnes ($year)").assertExists() + onNodeWithText("rank = ${countryEmissionsInfo.rank} ($percentage)").assertExists() + } + }