diff --git a/workflow-testing/src/main/java/com/squareup/workflow1/testing/RealRenderTester.kt b/workflow-testing/src/main/java/com/squareup/workflow1/testing/RealRenderTester.kt index 6603330d43..6a197e48dc 100644 --- a/workflow-testing/src/main/java/com/squareup/workflow1/testing/RealRenderTester.kt +++ b/workflow-testing/src/main/java/com/squareup/workflow1/testing/RealRenderTester.kt @@ -101,6 +101,10 @@ internal class RealRenderTester( private var explicitWorkerExpectationsRequired: Boolean = false private var explicitSideEffectExpectationsRequired: Boolean = false + private val stateAndOutput: Pair?> by lazy { + val action = processedAction ?: noAction() + action.applyTo(props, state) + } override val actionSink: Sink> get() = this override fun expectWorkflow( @@ -286,7 +290,7 @@ internal class RealRenderTester( block: (newState: StateT, output: WorkflowOutput?) -> Unit ): RenderTestResult { return verifyAction { - val (state, output) = it.applyTo(props, state) + val (state, output) = stateAndOutput block(state, output) } } @@ -297,8 +301,7 @@ internal class RealRenderTester( override fun testNextRenderWithProps( newProps: PropsT ): RenderTester { - val action = processedAction ?: noAction() - val (stateAfterRender, _) = action.applyTo(props, state) + val (stateAfterRender, _) = stateAndOutput val newState = if (props != newProps) { workflow.onPropsChanged(props, newProps, stateAfterRender) } else { diff --git a/workflow-testing/src/test/java/com/squareup/workflow1/testing/RealRenderTesterTest.kt b/workflow-testing/src/test/java/com/squareup/workflow1/testing/RealRenderTesterTest.kt index e1b12084de..fb8ee0eefa 100644 --- a/workflow-testing/src/test/java/com/squareup/workflow1/testing/RealRenderTesterTest.kt +++ b/workflow-testing/src/test/java/com/squareup/workflow1/testing/RealRenderTesterTest.kt @@ -38,7 +38,6 @@ import kotlin.test.assertSame import kotlin.test.assertTrue import kotlin.test.fail -@OptIn(ExperimentalStdlibApi::class) internal class RealRenderTesterTest { private interface OutputWhateverChild : Workflow @@ -1212,6 +1211,25 @@ internal class RealRenderTesterTest { } } + @Test fun `testNextRender and verifyActionResult call action handler only once`() { + val worker = Worker.from { } + var actionCount = 0 + val workflow = Workflow.stateless { + runningWorker(worker) { + action { actionCount++ } + } + } + + workflow.testRender(Unit) + .expectWorker(typeOf>(), output = WorkflowOutput(Unit)) + .render() + .verifyActionResult { _, _ -> } + .verifyActionResult { _, _ -> } + .testNextRender() + + assertEquals(1, actionCount) + } + @Test fun `render is executed multiple times`() { var renderCount = 0 val workflow = Workflow.stateless { renderCount++ }