square / workflow-swift

A Swift and Kotlin library for making composable state machines, and UIs driven by those state machines.

Home Page:https://square.github.io/workflow

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code Coverage

Koshub opened this issue · comments

From the Tutorial example. Could you please clarify how to test the selected code:
Screenshot 2020-10-18 at 23 11 42

Hi @Koshub, are you familiar with RenderTester?

If so, you should produce the relevant output when you expect a workflow, like here:

    func testTodoWorkflow() {
        let someScreen = TodoListScreen(todoTitles: []) { _ in }.asAnyScreen()
        let initialState = TodoWorkflow.State(todos: [.init(title: "Hello", note: "")],
                                              step: .edit(index: 0))
        
        TodoWorkflow(name: "")
            .renderTester(initialState: initialState)
            .expectWorkflow(type: TodoListWorkflow.self,
                            producingRendering: .init(screen: someScreen))
            .expectWorkflow(type: TodoEditWorkflow.self,
                            producingRendering: .init(screen: someScreen),
                            producingOutput: .discard)
            .render { _ in }
    }

@dhavalshreyas thank you for your reply. As I understood from your example there is an assurance that the discard output is producing. But what I suppose to get is to make sure when the .save event is emitted it produces exactly the .saveChanges action and with the right index and updatedTodo associated data. How to achieve that with RenderTester ?

You can use RenderTester's verify/assert APIs for this:

    func testTodoWorkflow() {
        let someScreen = TodoListScreen(todoTitles: []) { _ in }.asAnyScreen()
        let initialState = TodoWorkflow.State(
            todos: [.init(title: "Hello", note: "")],
            step: .edit(index: 0)
        )

        TodoWorkflow(name: "")
            .renderTester(initialState: initialState)
            .expectWorkflow(
                type: TodoListWorkflow.self,
                producingRendering: .init(screen: someScreen)
            )
            .expectWorkflow(
                type: TodoEditWorkflow.self,
                producingRendering: .init(screen: someScreen),
                producingOutput: .save(.init(title: "Hello", note: "Notes"))
            )
            .render { _ in }
            .verifyAction(type: TodoWorkflow.EditAction.self) { action in
                guard case .saveChanges(let index, let todo) = action else {
                    XCTFail()
                    return
                }
                XCTAssert(index == 0)
                XCTAssert(todo.note == "Notes")
            }
            .verifyState { state in
                XCTAssert(state.todos.first?.title == "Hello")
                XCTAssert(state.todos.first?.note == "Notes")
            }
    }

Great. That is what I wanted to see. Thanks a lot!