Skip to content

Commit cb3b818

Browse files
Rename headlessIntegrationTest -> renderForTest and deprecate launchForTestingFrom*
1 parent 742ecf5 commit cb3b818

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ public fun <PropsT, StateT, OutputT, RenderingT>
112112
* - It is a test failure if no workflow or worker emitted an output, no rendering event was
113113
* invoked, and any of the action verification methods on [RenderTestResult] is called.
114114
*
115+
* ## Testing Application
116+
*
117+
* Note that by providing the values for all child workflows, workers, and side effects, the test
118+
* is extremely focused on the logic of the `render()` method. Everything else is essentially
119+
* 'mocked'. If you do not have complex logic within `render()` that needs to be directly unit
120+
* tested, then you may be better served by using a [WorkflowTurbine] and [renderForResult] as this
121+
* will actually run your workflow (and any children, fake or not) in the runtime.
122+
*
115123
* ## Examples
116124
*
117125
* ### Worker output

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import kotlin.coroutines.EmptyCoroutineContext
5353
* - [sendProps]
5454
* - Send a new [PropsT] to the root workflow.
5555
*/
56+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
5657
public class WorkflowTestRuntime<PropsT, OutputT, RenderingT> @TestOnly internal constructor(
5758
private val props: MutableStateFlow<PropsT>,
5859
private val renderingsAndSnapshotsFlow: Flow<RenderingAndSnapshot<RenderingT>>,
@@ -173,6 +174,7 @@ public class WorkflowTestRuntime<PropsT, OutputT, RenderingT> @TestOnly internal
173174
*
174175
* All workflow-related coroutines are cancelled when the block exits.
175176
*/
177+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
176178
@TestOnly
177179
public fun <T, PropsT, OutputT, RenderingT>
178180
Workflow<PropsT, OutputT, RenderingT>.launchForTestingFromStartWith(
@@ -187,6 +189,7 @@ public fun <T, PropsT, OutputT, RenderingT>
187189
*
188190
* All workflow-related coroutines are cancelled when the block exits.
189191
*/
192+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
190193
@TestOnly
191194
public fun <T, OutputT, RenderingT>
192195
Workflow<Unit, OutputT, RenderingT>.launchForTestingFromStartWith(
@@ -202,6 +205,7 @@ public fun <T, OutputT, RenderingT>
202205
*
203206
* All workflow-related coroutines are cancelled when the block exits.
204207
*/
208+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
205209
@TestOnly
206210
public fun <T, PropsT, StateT, OutputT, RenderingT>
207211
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.launchForTestingFromStateWith(
@@ -223,6 +227,7 @@ public fun <T, PropsT, StateT, OutputT, RenderingT>
223227
*
224228
* All workflow-related coroutines are cancelled when the block exits.
225229
*/
230+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
226231
@TestOnly
227232
public fun <StateT, OutputT, RenderingT>
228233
StatefulWorkflow<Unit, StateT, OutputT, RenderingT>.launchForTestingFromStateWith(
@@ -236,6 +241,7 @@ public fun <StateT, OutputT, RenderingT>
236241
*
237242
* All workflow-related coroutines are cancelled when the block exits.
238243
*/
244+
@Deprecated("Use renderForTest and WorkflowTurbine instead.")
239245
@TestOnly
240246
public fun <T, PropsT, StateT, OutputT, RenderingT>
241247
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.launchForTestingWith(

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package com.squareup.workflow1.testing
33
import app.cash.turbine.ReceiveTurbine
44
import app.cash.turbine.test
55
import com.squareup.workflow1.RuntimeConfig
6-
import com.squareup.workflow1.RuntimeConfigOptions
76
import com.squareup.workflow1.Workflow
87
import com.squareup.workflow1.WorkflowInterceptor
8+
import com.squareup.workflow1.config.JvmTestRuntimeConfigTools
99
import com.squareup.workflow1.renderWorkflowIn
1010
import com.squareup.workflow1.testing.WorkflowTurbine.Companion.WORKFLOW_TEST_DEFAULT_TIMEOUT_MS
1111
import kotlinx.coroutines.CoroutineScope
@@ -22,8 +22,8 @@ import kotlin.coroutines.CoroutineContext
2222
import kotlin.time.Duration.Companion.milliseconds
2323

2424
/**
25-
* This is a test harness to run integration tests for a Workflow tree. The parameters passed here are
26-
* the same as those to start a Workflow runtime with [renderWorkflowIn] except for ignoring
25+
* This is a test harness to run turbine like tests for a Workflow tree. The parameters passed here
26+
* are the same as those to start a Workflow runtime with [renderWorkflowIn] except for ignoring
2727
* state persistence as that is not needed for this style of test.
2828
*
2929
* The [coroutineContext] rather than a [CoroutineScope] is passed so that this harness handles the
@@ -35,13 +35,15 @@ import kotlin.time.Duration.Companion.milliseconds
3535
* This will start the Workflow runtime (with params as passed) rooted at whatever Workflow
3636
* it is called on and then create a [WorkflowTurbine] for its renderings and run [testCase] on that.
3737
* [testCase] can thus drive the test scenario and assert against renderings.
38+
*
39+
* The default [RuntimeConfig] will be the one specified via [JvmTestRuntimeConfigTools].
3840
*/
3941
@OptIn(ExperimentalCoroutinesApi::class)
40-
public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.headlessIntegrationTest(
42+
public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.renderForTest(
4143
props: StateFlow<PropsT>,
4244
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
4345
interceptors: List<WorkflowInterceptor> = emptyList(),
44-
runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG,
46+
runtimeConfig: RuntimeConfig = JvmTestRuntimeConfigTools.getTestRuntimeConfig(),
4547
onOutput: suspend (OutputT) -> Unit = {},
4648
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
4749
testCase: suspend WorkflowTurbine<RenderingT>.() -> Unit
@@ -82,18 +84,18 @@ public fun <PropsT, OutputT, RenderingT> Workflow<PropsT, OutputT, RenderingT>.h
8284
}
8385

8486
/**
85-
* Version of [headlessIntegrationTest] that does not require props. For Workflows that have [Unit]
87+
* Version of [renderForTest] that does not require props. For Workflows that have [Unit]
8688
* props type.
8789
*/
8890
@OptIn(ExperimentalCoroutinesApi::class)
89-
public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.headlessIntegrationTest(
91+
public fun <OutputT, RenderingT> Workflow<Unit, OutputT, RenderingT>.renderForTest(
9092
coroutineContext: CoroutineContext = UnconfinedTestDispatcher(),
9193
interceptors: List<WorkflowInterceptor> = emptyList(),
92-
runtimeConfig: RuntimeConfig = RuntimeConfigOptions.DEFAULT_CONFIG,
94+
runtimeConfig: RuntimeConfig = JvmTestRuntimeConfigTools.getTestRuntimeConfig(),
9395
onOutput: suspend (OutputT) -> Unit = {},
9496
testTimeout: Long = WORKFLOW_TEST_DEFAULT_TIMEOUT_MS,
9597
testCase: suspend WorkflowTurbine<RenderingT>.() -> Unit
96-
): Unit = headlessIntegrationTest(
98+
): Unit = renderForTest(
9799
props = MutableStateFlow(Unit).asStateFlow(),
98100
coroutineContext = coroutineContext,
99101
interceptors = interceptors,

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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.headlessIntegrationTest
10+
import com.squareup.workflow1.testing.renderForTest
1111
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import kotlinx.coroutines.Job
1313
import kotlinx.coroutines.awaitCancellation
@@ -84,7 +84,7 @@ class WorkflowsLifecycleTests(
8484
}
8585

8686
@Test fun sideEffectsStartedWhenExpected() {
87-
workflowWithSideEffects.headlessIntegrationTest(
87+
workflowWithSideEffects.renderForTest(
8888
runtimeConfig = runtimeConfig
8989
) {
9090
// One time starts but does not stop the side effect.
@@ -98,7 +98,7 @@ class WorkflowsLifecycleTests(
9898
}
9999

100100
@Test fun sideEffectsStoppedWhenExpected() {
101-
workflowWithSideEffects.headlessIntegrationTest(
101+
workflowWithSideEffects.renderForTest(
102102
runtimeConfig = runtimeConfig
103103
) {
104104
// Twice will start and stop the side effect.
@@ -112,7 +112,7 @@ class WorkflowsLifecycleTests(
112112
}
113113

114114
@Test fun childSessionWorkflowStartedWhenExpected() {
115-
workflowWithChildSession.headlessIntegrationTest(
115+
workflowWithChildSession.renderForTest(
116116
runtimeConfig = runtimeConfig
117117
) {
118118
// One time starts but does not stop the child session workflow.
@@ -139,7 +139,7 @@ class WorkflowsLifecycleTests(
139139
@Test
140140
fun sideEffectsStartAndStoppedWhenHandledSynchronously() {
141141
val dispatcher = UnconfinedTestDispatcher()
142-
workflowWithSideEffects.headlessIntegrationTest(
142+
workflowWithSideEffects.renderForTest(
143143
coroutineContext = dispatcher,
144144
runtimeConfig = runtimeConfig
145145
) {
@@ -163,7 +163,7 @@ class WorkflowsLifecycleTests(
163163
}
164164

165165
@Test fun childSessionWorkflowStoppedWhenExpected() {
166-
workflowWithChildSession.headlessIntegrationTest(
166+
workflowWithChildSession.renderForTest(
167167
runtimeConfig = runtimeConfig
168168
) {
169169
// Twice will start and stop the child session workflow.
@@ -185,7 +185,7 @@ class WorkflowsLifecycleTests(
185185
@Test
186186
fun childSessionWorkflowStartAndStoppedWhenHandledSynchronously() {
187187
val dispatcher = UnconfinedTestDispatcher()
188-
workflowWithChildSession.headlessIntegrationTest(
188+
workflowWithChildSession.renderForTest(
189189
coroutineContext = dispatcher,
190190
runtimeConfig = runtimeConfig
191191
) {

0 commit comments

Comments
 (0)