Skip to content

Commit c6a5c4a

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

File tree

2 files changed

+85
-71
lines changed

2 files changed

+85
-71
lines changed

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

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -49,67 +49,15 @@ 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(
52+
public fun <PropsT, StateT, OutputT, RenderingT>
53+
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.renderForTest(
5354
props: StateFlow<PropsT>,
54-
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
55+
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
5556
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
56-
interceptors: List<WorkflowInterceptor> = emptyList(),
5757
onOutput: suspend (OutputT) -> Unit = {},
5858
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
5959
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)
104-
public fun <PropsT, StateT, OutputT, RenderingT>
105-
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.renderForTest(
106-
props: StateFlow<PropsT>,
107-
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
108-
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
109-
onOutput: suspend (OutputT) -> Unit = {},
110-
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
111-
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
112-
) {
60+
) {
11361
val workflow: Workflow<PropsT, OutputT, RenderingT> = this
11462

11563
// Determine the initial snapshot based on startFrom mode
@@ -189,22 +137,75 @@ 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>
198145
StatefulWorkflow<Unit, StateT, OutputT, RenderingT>.renderForTest(
199-
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
200-
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
201-
onOutput: suspend (OutputT) -> Unit = {},
202-
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
203-
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
204-
): Unit = renderForTest(
146+
testParams: WorkflowTestParams<StateT> = WorkflowTestParams(),
147+
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
148+
onOutput: suspend (OutputT) -> Unit = {},
149+
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
150+
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
151+
): Unit = renderForTest(
152+
props = MutableStateFlow(Unit).asStateFlow(),
153+
testParams = testParams,
154+
coroutineContext = coroutineContext,
155+
onOutput = onOutput,
156+
testTimeout = testTimeout,
157+
testCase = testCase
158+
)
159+
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+
/**
192+
* Version of [renderForTest] for any [Workflow]
193+
* that accepts [WorkflowTestParams] and doesn't require props.
194+
* For Workflows that have [Unit] props type.
195+
*/
196+
@OptIn(ExperimentalCoroutinesApi::class)
197+
public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.renderForTestForStartWith(
198+
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
199+
testParams: WorkflowTestParams<Nothing> = WorkflowTestParams(),
200+
interceptors: List<WorkflowInterceptor> = emptyList(),
201+
onOutput: suspend (OutputT) -> Unit = {},
202+
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
203+
testCase: suspend WorkflowTurbine<RenderingT, OutputT>.() -> Unit
204+
): Unit = renderForTestForStartWith(
205205
props = MutableStateFlow(Unit).asStateFlow(),
206206
testParams = testParams,
207207
coroutineContext = coroutineContext,
208+
interceptors = interceptors,
208209
onOutput = onOutput,
209210
testTimeout = testTimeout,
210211
testCase = testCase

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)