|
1 | 1 | @testable import SimplexArchitecture |
2 | 2 | import SwiftUI |
3 | 3 | import XCTest |
| 4 | +import CasePaths |
4 | 5 |
|
5 | 6 | final class StoreTests: XCTestCase { |
6 | 7 | fileprivate var store: Store<TestReducer>! |
@@ -95,15 +96,100 @@ final class StoreTests: XCTestCase { |
95 | 96 |
|
96 | 97 | XCTAssertNotNil(sendTask2.task) |
97 | 98 | } |
| 99 | + |
| 100 | + func testPullbackAction() throws { |
| 101 | + let parent = ParentView() |
| 102 | + parent.store.setContainerIfNeeded(for: parent, states: .init()) |
| 103 | + store.setContainerIfNeeded(for: TestView()) |
| 104 | + XCTAssertNil(store.pullbackAction) |
| 105 | + store.pullback(to: /ParentReducer.Action.child, parent: parent) |
| 106 | + store.sendIfNeeded(.c1) |
| 107 | + XCTAssertNotNil(store.pullbackAction) |
| 108 | + XCTAssertEqual(parent.store.container?.count, 1) |
| 109 | + } |
| 110 | + |
| 111 | + func testPullbackReducerAction() throws { |
| 112 | + let parent = ParentView() |
| 113 | + parent.store.setContainerIfNeeded(for: parent, states: .init()) |
| 114 | + store.setContainerIfNeeded(for: TestView()) |
| 115 | + XCTAssertNil(store.pullbackReducerAction) |
| 116 | + store.pullback(to: /ParentReducer.Action.childReducerAction, parent: parent) |
| 117 | + store.sendIfNeeded(.c4) |
| 118 | + XCTAssertNotNil(store.pullbackReducerAction) |
| 119 | + XCTAssertEqual(parent.store.container?.count, 1) |
| 120 | + } |
| 121 | + |
| 122 | + func testPullbackActionForId() throws { |
| 123 | + let parent = ParentView() |
| 124 | + parent.store.setContainerIfNeeded(for: parent, states: .init()) |
| 125 | + store.setContainerIfNeeded(for: TestView()) |
| 126 | + XCTAssertNil(store.pullbackAction) |
| 127 | + let uuid = UUID() |
| 128 | + store.pullback(to: /ParentReducer.Action.childId, parent: parent, id: uuid) |
| 129 | + store.sendIfNeeded(.c1) |
| 130 | + XCTAssertNotNil(store.pullbackAction) |
| 131 | + XCTAssertEqual(parent.store.container?.id, uuid) |
| 132 | + } |
| 133 | + |
| 134 | + func testPullbackReducerActionForId() throws { |
| 135 | + let parent = ParentView() |
| 136 | + parent.store.setContainerIfNeeded(for: parent, states: .init()) |
| 137 | + store.setContainerIfNeeded(for: TestView()) |
| 138 | + XCTAssertNil(store.pullbackReducerAction) |
| 139 | + let uuid = UUID() |
| 140 | + store.pullback(to: /ParentReducer.Action.childIdReducerAction, parent: parent, id: uuid) |
| 141 | + store.sendIfNeeded(.c4) |
| 142 | + XCTAssertNotNil(store.pullbackReducerAction) |
| 143 | + XCTAssertEqual(parent.store.container?.id, uuid) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +@ScopeState |
| 148 | +private struct ParentView: View { |
| 149 | + @State var count = 0 |
| 150 | + @State var id: UUID? |
| 151 | + let store: Store<ParentReducer> = .init(reducer: ParentReducer()) |
| 152 | + var body: some View { |
| 153 | + EmptyView() |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +private struct ParentReducer: ReducerProtocol { |
| 158 | + enum Action { |
| 159 | + case child(TestReducer.Action) |
| 160 | + case childReducerAction(TestReducer.ReducerAction) |
| 161 | + case childId(id: UUID, action: TestReducer.Action) |
| 162 | + case childIdReducerAction(id: UUID, action: TestReducer.ReducerAction) |
| 163 | + } |
| 164 | + |
| 165 | + func reduce(into state: StateContainer<ParentView>, action: Action) -> SideEffect<ParentReducer> { |
| 166 | + switch action { |
| 167 | + case .child: |
| 168 | + state.count += 1 |
| 169 | + return .none |
| 170 | + |
| 171 | + case .childReducerAction: |
| 172 | + state.count += 1 |
| 173 | + return .none |
| 174 | + |
| 175 | + case .childId(let id, _): |
| 176 | + state.id = id |
| 177 | + return .none |
| 178 | + |
| 179 | + case .childIdReducerAction(let id, _): |
| 180 | + state.id = id |
| 181 | + return .none |
| 182 | + } |
| 183 | + } |
98 | 184 | } |
99 | 185 |
|
100 | 186 | private struct TestReducer: ReducerProtocol { |
101 | | - enum ReducerAction { |
| 187 | + enum ReducerAction: Equatable, Pullbackable { |
102 | 188 | case c3 |
103 | 189 | case c4 |
104 | 190 | } |
105 | 191 |
|
106 | | - enum Action: Equatable { |
| 192 | + enum Action: Equatable, Pullbackable { |
107 | 193 | case c1 |
108 | 194 | case c2 |
109 | 195 | } |
|
0 commit comments