Skip to content

Commit 7f970d8

Browse files
committed
fix action tester
1 parent 72f82ce commit 7f970d8

File tree

6 files changed

+30
-20
lines changed

6 files changed

+30
-20
lines changed

Samples/Tutorial/Frameworks/Tutorial5Complete/Tests/TodoEditWorkflowTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class TodoEditWorkflowTests: XCTestCase {
2222
func testAction() throws {
2323
TodoEditWorkflow.Action
2424
// Start with a todo of "Title" "Note"
25-
.tester(withState: TodoEditWorkflow.State(todo: TodoModel(title: "Title", note: "Note")))
25+
.tester(workflow: TodoEditWorkflow(initialTodo: TodoModel(title: "Title", note: "Note")))
2626
.verifyState { state in
2727
XCTAssertEqual("Title", state.todo.title)
2828
XCTAssertEqual("Note", state.todo.note)

Samples/Tutorial/Frameworks/Tutorial5Complete/Tests/TodoListWorkflowTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import XCTest
2121
class TodoListWorkflowTests: XCTestCase {
2222
func testActions() throws {
2323
TodoListWorkflow.Action
24-
.tester(withState: TodoListWorkflow.State())
24+
.tester(workflow: TodoListWorkflow(name: "", todos: []))
2525
.send(action: .onBack)
2626
.verifyOutput { output in
2727
// The `.onBack` action should emit an output of `.back`.

Samples/Tutorial/Frameworks/Tutorial5Complete/Tests/WelcomeWorkflowTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import XCTest
2121
class WelcomeWorkflowTests: XCTestCase {
2222
func testNameUpdates() throws {
2323
WelcomeWorkflow.Action
24-
.tester(withState: WelcomeWorkflow.State(name: ""))
24+
.tester(workflow: WelcomeWorkflow())
2525
.send(action: .nameChanged(name: "myName"))
2626
// No output is expected when the name changes.
2727
.assertNoOutput()
@@ -33,7 +33,7 @@ class WelcomeWorkflowTests: XCTestCase {
3333

3434
func testLogIn() throws {
3535
WelcomeWorkflow.Action
36-
.tester(withState: WelcomeWorkflow.State(name: ""))
36+
.tester(workflow: WelcomeWorkflow())
3737
.send(action: .didLogIn)
3838
// Since the name is empty, `.didLogIn` will not emit an output.
3939
.assertNoOutput()

Workflow/Tests/AnyWorkflowActionTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ final class AnyWorkflowActionTests: XCTestCase {
8383
XCTAssertEqual(log, [])
8484

8585
var state: Void = ()
86-
_ = erased.apply(toState: &state)
86+
_ = erased.apply(toState: &state, workflow: ExampleWorkflow())
8787

8888
XCTAssertEqual(log, ["action invoked"])
8989
}

WorkflowTesting/Sources/WorkflowActionTester.swift

+16-6
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ import Workflow
1818
import XCTest
1919

2020
extension WorkflowAction {
21-
/// Returns a state tester containing `self`.
22-
public static func tester(withState state: WorkflowType.State) -> WorkflowActionTester<WorkflowType, Self> {
23-
WorkflowActionTester(state: state)
21+
// /// Returns a state tester containing `self`.
22+
// public static func tester(withState state: WorkflowType.State) -> WorkflowActionTester<WorkflowType, Self> {
23+
// WorkflowActionTester(state: state)
24+
// }
25+
26+
public static func tester(workflow: WorkflowType, state: WorkflowType.State) -> WorkflowActionTester<WorkflowType, Self> {
27+
WorkflowActionTester(workflow: workflow, state: state)
28+
}
29+
30+
public static func tester(workflow: WorkflowType) -> WorkflowActionTester<WorkflowType, Self> {
31+
tester(workflow: workflow, state: workflow.makeInitialState())
2432
}
2533
}
2634

@@ -61,11 +69,13 @@ extension WorkflowAction {
6169
/// ```
6270
public struct WorkflowActionTester<WorkflowType, Action> where Action: WorkflowAction, Action.WorkflowType == WorkflowType {
6371
/// The current state
72+
let workflow: WorkflowType
6473
let state: WorkflowType.State
6574
let output: WorkflowType.Output?
6675

6776
/// Initializes a new state tester
68-
fileprivate init(state: WorkflowType.State, output: WorkflowType.Output? = nil) {
77+
fileprivate init(workflow: WorkflowType, state: WorkflowType.State, output: WorkflowType.Output? = nil) {
78+
self.workflow = workflow
6979
self.state = state
7080
self.output = output
7181
}
@@ -78,8 +88,8 @@ public struct WorkflowActionTester<WorkflowType, Action> where Action: WorkflowA
7888
@discardableResult
7989
public func send(action: Action) -> WorkflowActionTester<WorkflowType, Action> {
8090
var newState = state
81-
let output = action.apply(toState: &newState)
82-
return WorkflowActionTester(state: newState, output: output)
91+
let output = action.apply(toState: &newState, workflow: workflow)
92+
return WorkflowActionTester(workflow: workflow, state: newState, output: output)
8393
}
8494

8595
/// Asserts that the action produced no output

WorkflowTesting/Tests/WorkflowActionTesterTests.swift

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import XCTest
2121
final class WorkflowActionTesterTests: XCTestCase {
2222
func test_stateTransitions() {
2323
TestAction
24-
.tester(withState: false)
24+
.tester(withState: false, workflow: TestWorkflow())
2525
.send(action: .toggleTapped)
2626
.verifyState { XCTAssertTrue($0) }
2727
}
2828

2929
func test_stateTransitions_throw() throws {
3030
try TestAction
31-
.tester(withState: false)
31+
.tester(withState: false, workflow: TestWorkflow())
3232
.send(action: .toggleTapped)
3333
.verifyState {
3434
try throwingNoop()
@@ -38,21 +38,21 @@ final class WorkflowActionTesterTests: XCTestCase {
3838

3939
func test_stateTransitions_equatable() {
4040
TestAction
41-
.tester(withState: false)
41+
.tester(withState: false, workflow: TestWorkflow())
4242
.send(action: .toggleTapped)
4343
.assert(state: true)
4444
}
4545

4646
func test_noOutputs() {
4747
TestAction
48-
.tester(withState: false)
48+
.tester(withState: false, workflow: TestWorkflow())
4949
.send(action: .toggleTapped)
5050
.assertNoOutput()
5151
}
5252

5353
func test_outputs() {
5454
TestAction
55-
.tester(withState: false)
55+
.tester(withState: false, workflow: TestWorkflow())
5656
.send(action: .exitTapped)
5757
.verifyOutput { output in
5858
XCTAssertEqual(output, .finished)
@@ -61,7 +61,7 @@ final class WorkflowActionTesterTests: XCTestCase {
6161

6262
func test_outputs_throw() throws {
6363
try TestAction
64-
.tester(withState: false)
64+
.tester(withState: false, workflow: TestWorkflow())
6565
.send(action: .exitTapped)
6666
.verifyOutput { output in
6767
try throwingNoop()
@@ -71,14 +71,14 @@ final class WorkflowActionTesterTests: XCTestCase {
7171

7272
func test_outputs_equatable() {
7373
TestAction
74-
.tester(withState: false)
74+
.tester(withState: false, workflow: TestWorkflow())
7575
.send(action: .exitTapped)
7676
.assert(output: .finished)
7777
}
7878

7979
func test_deprecated_methods() {
8080
TestAction
81-
.tester(withState: false)
81+
.tester(withState: false, workflow: TestWorkflow())
8282
.send(action: .exitTapped)
8383
.assert(output: .finished)
8484
.verifyState { state in
@@ -89,7 +89,7 @@ final class WorkflowActionTesterTests: XCTestCase {
8989
func test_testerExtension() {
9090
let state = true
9191
let tester = TestAction
92-
.tester(withState: true)
92+
.tester(withState: true, workflow: TestWorkflow())
9393
XCTAssertEqual(state, tester.state)
9494
XCTAssertNil(tester.output)
9595
}

0 commit comments

Comments
 (0)