Skip to content

Commit 8d35279

Browse files
committed
Refactor some test code that uses TestCompositionLocalProvider
1 parent 73b3d2e commit 8d35279

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

app/src/test/java/io/github/tobyhs/weatherweight/forecast/ForecastScreenTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
1414
import io.github.tobyhs.weatherweight.R
1515
import io.github.tobyhs.weatherweight.data.model.ForecastResultSet
1616
import io.github.tobyhs.weatherweight.test.ForecastResultSetFactory
17-
import io.github.tobyhs.weatherweight.test.TestCompositionLocalProvider
17+
import io.github.tobyhs.weatherweight.test.setContentWithTestProvider
1818
import io.github.tobyhs.weatherweight.ui.LoadState
1919

2020
import io.mockk.every
@@ -116,10 +116,8 @@ class ForecastScreenTest {
116116
}
117117

118118
private fun setContent() {
119-
composeRule.setContent {
120-
TestCompositionLocalProvider {
121-
ForecastScreen(viewModel)
122-
}
119+
setContentWithTestProvider(composeRule) {
120+
ForecastScreen(viewModel)
123121
}
124122
}
125123

app/src/test/java/io/github/tobyhs/weatherweight/forecast/HourlyForecastCardTest.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import androidx.compose.ui.test.onAllNodesWithTag
77
import androidx.test.ext.junit.runners.AndroidJUnit4
88

99
import io.github.tobyhs.weatherweight.test.ForecastResultSetFactory
10-
import io.github.tobyhs.weatherweight.test.TestCompositionLocalProvider
10+
import io.github.tobyhs.weatherweight.test.setContentWithTestProvider
1111

1212
import org.junit.Rule
1313
import org.junit.Test
@@ -19,10 +19,8 @@ class HourlyForecastCardTest {
1919

2020
@Test
2121
fun compose() {
22-
composeRule.setContent {
23-
TestCompositionLocalProvider {
24-
HourlyForecastCard(ForecastResultSetFactory.createHourlyForecasts()[0])
25-
}
22+
setContentWithTestProvider(composeRule) {
23+
HourlyForecastCard(ForecastResultSetFactory.createHourlyForecasts()[0])
2624
}
2725
checkContent(composeRule)
2826
}

app/src/test/java/io/github/tobyhs/weatherweight/test/TestCompositionLocalProvider.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.github.tobyhs.weatherweight.test
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.CompositionLocalProvider
5+
import androidx.compose.ui.test.junit4.ComposeContentTestRule
56

67
import io.github.tobyhs.weatherweight.ui.LocalClock
78

@@ -13,3 +14,7 @@ fun TestCompositionLocalProvider(content: @Composable () -> Unit) {
1314
val clock = Clock.system(ZoneId.of("America/Los_Angeles"))
1415
CompositionLocalProvider(LocalClock provides clock, content)
1516
}
17+
18+
fun setContentWithTestProvider(composeRule: ComposeContentTestRule, content: @Composable () -> Unit) {
19+
composeRule.setContent { TestCompositionLocalProvider(content) }
20+
}

0 commit comments

Comments
 (0)