Skip to content

Commit

Permalink
Cache action result allowing non-idempotent actions to perform well i…
Browse files Browse the repository at this point in the history
…n test chains
  • Loading branch information
0legg committed Apr 5, 2023
1 parent f9cba38 commit 6425cc9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(

private var explicitWorkerExpectationsRequired: Boolean = false
private var explicitSideEffectExpectationsRequired: Boolean = false
private val stateAndOutput: Pair<StateT, WorkflowOutput<OutputT>?> by lazy {
val action = processedAction ?: noAction()
action.applyTo(props, state)
}
override val actionSink: Sink<WorkflowAction<PropsT, StateT, OutputT>> get() = this

override fun expectWorkflow(
Expand Down Expand Up @@ -286,7 +290,7 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
block: (newState: StateT, output: WorkflowOutput<OutputT>?) -> Unit
): RenderTestResult<PropsT, StateT, OutputT, RenderingT> {
return verifyAction {
val (state, output) = it.applyTo(props, state)
val (state, output) = stateAndOutput
block(state, output)
}
}
Expand All @@ -297,8 +301,7 @@ internal class RealRenderTester<PropsT, StateT, OutputT, RenderingT>(
override fun testNextRenderWithProps(
newProps: PropsT
): RenderTester<PropsT, StateT, OutputT, RenderingT> {
val action = processedAction ?: noAction()
val (stateAfterRender, _) = action.applyTo(props, state)
val (stateAfterRender, _) = stateAndOutput
val newState = if (props != newProps) {
workflow.onPropsChanged(props, newProps, stateAfterRender)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit, Unit, Unit>
Expand Down Expand Up @@ -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<Unit, Nothing, Unit> {
runningWorker(worker) {
action { actionCount++ }
}
}

workflow.testRender(Unit)
.expectWorker(typeOf<Worker<Unit>>(), output = WorkflowOutput(Unit))
.render()
.verifyActionResult { _, _ -> }
.verifyActionResult { _, _ -> }
.testNextRender()

assertEquals(1, actionCount)
}

@Test fun `render is executed multiple times`() {
var renderCount = 0
val workflow = Workflow.stateless<Unit, Nothing, Unit> { renderCount++ }
Expand Down

0 comments on commit 6425cc9

Please sign in to comment.