Skip to content

STRUCT

RenderTester

Testing helper for validating the behavior of calls to render.

Usage: expect workflows and side effects then validate with a call to render and the resulting RenderTesterResult. Side-effects may be performed against the rendering to validate the behavior of actions.

To directly test actions and their effects, use the WorkflowActionTester.

workflow
    .renderTester(initialState: TestWorkflow.State())
    .expect(
        worker: TestWorker(),
        producingOutput: TestWorker.Output.success
    )
    .expectWorkflow(
        type: ChildWorkflow.self,
        key: "key",
        rendering: "rendering",
        // ⚠️ N.B. Only one output per call to `render` may be produced,
        // even if multiple child Workflows are expected in a call
        // to `render`. This is an invariant enforced by `RenderTester`
        // and the real runtime.
        producingOutput: nil
    )
    .render { rendering in
        XCTAssertEqual("expected text on rendering", rendering.text)
    }
    .assert(state: TestWorkflow.State())
    .assert(output: TestWorkflow.Output.finished)

Validating the rendering only from the initial state provided by the workflow:

workflow
    .renderTester()
    .render { rendering in
        XCTAssertEqual("expected text on rendering", rendering.text)
    }

Validate the state was updated from a callback on the rendering:

workflow
    .renderTester()
    .render { rendering in
        XCTAssertEqual("expected text on rendering", rendering.text)
        rendering.updateText("updated")
    }
    .assert(
        state: TestWorkflow.State(text: "updated")
    )

Validate an output was received from the workflow. The action() on the rendering will cause an action that will return an output.

workflow
    .renderTester()
    .render { rendering in
        rendering.action()
    }
    .assert(
       output: .success
    )

Validate a worker is running, and simulate the effect of its output:

workflow
    .renderTester(initialState: TestWorkflow.State(loadingState: .loading))
    .expect(
        worker: TestWorker(),
        output: TestWorker.Output.success
    )
    .render { _ in }

Validate a child workflow is run, and simulate the effect of its output:

workflow
    .renderTester(initialState: TestWorkflow.State(loadingState: .loading))
    .expectWorkflow(
        type: ChildWorkflow.self,
        rendering: "rendering",
        producingOutput: ChildWorkflow.Output.success
    )
    .render { _ in }

Methods

expectWorkflow(type:key:producingRendering:producingOutput:file:line:assertions:)

Expect the given workflow type in the next rendering.

  • Parameters:
  • type: The type of the expected workflow.
  • key: The key of the expected workflow (if specified).
  • rendering: The rendering result that should be returned when the workflow of this type is rendered.
  • output: An output that should be returned after the workflow of this type is rendered, if any.
  • assertions: Additional assertions for the given workflow, if any. You may use this to assert the properties of the requested workflow are as expected.

expectWorkflowIgnoringOutput(type:key:producingRendering:file:line:assertions:)

Expect the given workflow type in the next rendering, with its output being ignored by a call to ignoringOutput().

  • Parameters:
  • type: The type of the expected workflow.
  • key: The key of the expected workflow (if specified).
  • rendering: The rendering result that should be returned when the workflow of this type is rendered.
  • assertions: Additional assertions for the given workflow, if any. You may use this to assert the properties of the requested workflow are as expected.

expectSideEffect(key:file:line:)

Expect a side-effect for the given key.

  • Parameter key: The key to expect.

expectSideEffect(key:producingAction:file:line:)

Expect a side-effect for the given key, and produce the given action when it is requested.

  • Parameters:
  • key: The key to expect.
  • action: The action to produce when this side-effect is requested.

render(file:line:assertions:)

Render the workflow under test. At this point, you should have set up all expectations.

The given assertions closure will be called with the produced rendering, allowing you to assert its properties or perform actions on it (such as closures that are wired up to a Sink inside the workflow.

  • Parameters:
  • assertions: A closure called with the produced rendering for verification
  • Returns: A RenderTesterResult that can be used to verify expected resulting state or outputs.