Skip to content

Commit

Permalink
Compose Multiplatform UI test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
joreilly committed Oct 20, 2024
1 parent 6a2f966 commit cac3409
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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>(Country("IRL", "IE", "Ireland", "Europe"))
private val country = Country("IRL", "IE", "Ireland", "Europe")
private val countryList = listOf<Country>(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()
}

}

0 comments on commit cac3409

Please sign in to comment.