-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from joreilly/cmp_ui_tests
Compose Multiplatform UI test updates
- Loading branch information
Showing
1 changed file
with
32 additions
and
4 deletions.
There are no files selected for viewing
36 changes: 32 additions & 4 deletions
36
...eApp/src/commonTest/kotlin/dev/johnoreilly/climatetrace/screen/ClimateTraceTestsScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} | ||
|