Skip to content

Commit 0150852

Browse files
committed
Fix unit tests, change order of base renderForTest method and convienience overloads
1 parent d46ca91 commit 0150852

File tree

2 files changed

+74
-61
lines changed

2 files changed

+74
-61
lines changed

workflow-testing/src/main/java/com/squareup/workflow1/testing/WorkflowTurbine.kt

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -49,58 +49,6 @@ import kotlin.time.Duration.Companion.milliseconds
4949
* The default [RuntimeConfig] will be the one specified via [JvmTestRuntimeConfigTools].
5050
*/
5151
@OptIn(ExperimentalCoroutinesApi::class)
52-
public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.renderForTest(
53-
props: StateFlow<PropsT>,
54-
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
55-
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
56-
interceptors: List<WorkflowInterceptor> = emptyList(),
57-
onOutput: suspend (OutputT) -> Unit = {},
58-
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
59-
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
60-
) = asStatefulWorkflow().renderForTest(
61-
props,
62-
testParams,
63-
coroutineContext,
64-
onOutput,
65-
testTimeout,
66-
testCase
67-
)
68-
69-
/**
70-
* Version of [renderForTest] that does not require props. For Workflows that have [Unit]
71-
* props type.
72-
*/
73-
@OptIn(ExperimentalCoroutinesApi::class)
74-
public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.renderForTest(
75-
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
76-
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
77-
interceptors: List<WorkflowInterceptor> = emptyList(),
78-
onOutput: suspend (OutputT) -> Unit = {},
79-
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
80-
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
81-
): Unit = renderForTest(
82-
props = MutableStateFlow(Unit).asStateFlow(),
83-
testParams = testParams,
84-
coroutineContext = coroutineContext,
85-
interceptors = interceptors,
86-
onOutput = onOutput,
87-
testTimeout = testTimeout,
88-
testCase = testCase
89-
)
90-
91-
/**
92-
* Version of [renderForTest] for a [StatefulWorkflow]
93-
* that accepts [WorkflowTestParams] for configuring the test,
94-
* including starting from a specific state or snapshot.
95-
*
96-
* @param props StateFlow of props to send to the workflow.
97-
* @param testParams Test configuration parameters. See [WorkflowTestParams] for details.
98-
* @param coroutineContext Optional [CoroutineContext] to use for the test.
99-
* @param onOutput Callback for workflow outputs.
100-
* @param testTimeout Maximum time to wait for workflow operations in milliseconds.
101-
* @param testCase The test code to run with access to the [WorkflowTurbine].
102-
*/
103-
@OptIn(ExperimentalCoroutinesApi::class)
10452
public fun <PropsT, StateT, OutputT, RenderingT>
10553
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.renderForTest(
10654
props: StateFlow<PropsT>,
@@ -189,9 +137,8 @@ public fun <PropsT, StateT, OutputT, RenderingT>
189137
}
190138

191139
/**
192-
* Version of [renderForTest] for a [StatefulWorkflow]
193-
* that accepts [WorkflowTestParams] and doesn't require props.
194-
* For Workflows that have [Unit] props type.
140+
* Version of [renderForTest] that does not require props. For [StatefulWorkflow]s that have [Unit]
141+
* props type.
195142
*/
196143
@OptIn(ExperimentalCoroutinesApi::class)
197144
public fun <StateT, OutputT, RenderingT>
@@ -210,6 +157,59 @@ public fun <StateT, OutputT, RenderingT>
210157
testCase = testCase
211158
)
212159

160+
/**
161+
* Version of [renderForTest] for any [Workflow]
162+
* that accepts [WorkflowTestParams] for configuring the test,
163+
* including starting from a specific state or snapshot.
164+
*
165+
* @param props StateFlow of props to send to the workflow.
166+
* @param testParams Test configuration parameters. See [WorkflowTestParams] for details.
167+
* @param coroutineContext Optional [CoroutineContext] to use for the test.
168+
* @param onOutput Callback for workflow outputs.
169+
* @param testTimeout Maximum time to wait for workflow operations in milliseconds.
170+
* @param testCase The test code to run with access to the [WorkflowTurbine].
171+
*/
172+
@OptIn(ExperimentalCoroutinesApi::class)
173+
public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.renderForTestForStartWith(
174+
props: StateFlow<PropsT>,
175+
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
176+
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
177+
interceptors: List<WorkflowInterceptor> = emptyList(),
178+
onOutput: suspend (OutputT) -> Unit = {},
179+
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
180+
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
181+
) = asStatefulWorkflow().renderForTest(
182+
props,
183+
testParams,
184+
coroutineContext,
185+
onOutput,
186+
testTimeout,
187+
testCase
188+
)
189+
190+
/**
191+
* Version of [renderForTest] for any [Workflow]
192+
* that accepts [WorkflowTestParams] and doesn't require props.
193+
* For Workflows that have [Unit] props type.
194+
*/
195+
@OptIn(ExperimentalCoroutinesApi::class)
196+
public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.renderForTestForStartWith(
197+
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
198+
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
199+
interceptors: List<WorkflowInterceptor> = emptyList(),
200+
onOutput: suspend (OutputT) -> Unit = {},
201+
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
202+
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
203+
): Unit = renderForTestForStartWith(
204+
props = MutableStateFlow(Unit).asStateFlow(),
205+
testParams = testParams,
206+
coroutineContext = coroutineContext,
207+
interceptors = interceptors,
208+
onOutput = onOutput,
209+
testTimeout = testTimeout,
210+
testCase = testCase
211+
)
212+
213213
/**
214214
* Convenience function to test a workflow starting from a specific state.
215215
*

workflow-testing/src/test/java/com/squareup/workflow1/WorkflowsLifecycleTests.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.squareup.workflow1.RuntimeConfigOptions.CONFLATE_STALE_RENDERINGS
77
import com.squareup.workflow1.RuntimeConfigOptions.Companion.RuntimeOptions
88
import com.squareup.workflow1.RuntimeConfigOptions.Companion.RuntimeOptions.NONE
99
import com.squareup.workflow1.RuntimeConfigOptions.DRAIN_EXCLUSIVE_ACTIONS
10+
import com.squareup.workflow1.testing.WorkflowTestParams
1011
import com.squareup.workflow1.testing.renderForTest
1112
import kotlinx.coroutines.ExperimentalCoroutinesApi
1213
import kotlinx.coroutines.Job
@@ -85,7 +86,9 @@ class WorkflowsLifecycleTests(
8586

8687
@Test fun sideEffectsStartedWhenExpected() {
8788
workflowWithSideEffects.renderForTest(
88-
runtimeConfig = runtimeConfig
89+
testParams = WorkflowTestParams(
90+
runtimeConfig = runtimeConfig
91+
)
8992
) {
9093
// One time starts but does not stop the side effect.
9194
repeat(1) {
@@ -99,7 +102,9 @@ class WorkflowsLifecycleTests(
99102

100103
@Test fun sideEffectsStoppedWhenExpected() {
101104
workflowWithSideEffects.renderForTest(
102-
runtimeConfig = runtimeConfig
105+
testParams = WorkflowTestParams(
106+
runtimeConfig = runtimeConfig
107+
)
103108
) {
104109
// Twice will start and stop the side effect.
105110
repeat(2) {
@@ -113,7 +118,9 @@ class WorkflowsLifecycleTests(
113118

114119
@Test fun childSessionWorkflowStartedWhenExpected() {
115120
workflowWithChildSession.renderForTest(
116-
runtimeConfig = runtimeConfig
121+
testParams = WorkflowTestParams(
122+
runtimeConfig = runtimeConfig
123+
)
117124
) {
118125
// One time starts but does not stop the child session workflow.
119126
repeat(1) {
@@ -141,7 +148,9 @@ class WorkflowsLifecycleTests(
141148
val dispatcher = UnconfinedTestDispatcher()
142149
workflowWithSideEffects.renderForTest(
143150
coroutineContext = dispatcher,
144-
runtimeConfig = runtimeConfig
151+
testParams = WorkflowTestParams(
152+
runtimeConfig = runtimeConfig
153+
)
145154
) {
146155

147156
val (_, setState) = awaitNextRendering()
@@ -164,7 +173,9 @@ class WorkflowsLifecycleTests(
164173

165174
@Test fun childSessionWorkflowStoppedWhenExpected() {
166175
workflowWithChildSession.renderForTest(
167-
runtimeConfig = runtimeConfig
176+
testParams = WorkflowTestParams(
177+
runtimeConfig = runtimeConfig
178+
)
168179
) {
169180
// Twice will start and stop the child session workflow.
170181
repeat(2) {
@@ -187,7 +198,9 @@ class WorkflowsLifecycleTests(
187198
val dispatcher = UnconfinedTestDispatcher()
188199
workflowWithChildSession.renderForTest(
189200
coroutineContext = dispatcher,
190-
runtimeConfig = runtimeConfig
201+
testParams = WorkflowTestParams(
202+
runtimeConfig = runtimeConfig
203+
)
191204
) {
192205

193206
val (_, setState) = awaitNextRendering()

0 commit comments

Comments
 (0)