|
2 | 2 | @_spi(Internals) import ComposableArchitecture |
3 | 3 | import XCTest |
4 | 4 |
|
| 5 | +#if canImport(Testing) |
| 6 | + import Testing |
| 7 | +#endif |
| 8 | + |
5 | 9 | final class StoreTests: BaseTCATestCase { |
6 | 10 | var cancellables: Set<AnyCancellable> = [] |
7 | 11 |
|
@@ -1172,6 +1176,53 @@ final class StoreTests: BaseTCATestCase { |
1172 | 1176 | } |
1173 | 1177 | } |
1174 | 1178 |
|
| 1179 | +#if canImport(Testing) |
| 1180 | + @Suite |
| 1181 | + struct ModernStoreTests { |
| 1182 | + @Reducer |
| 1183 | + fileprivate struct TaskTreeFeature { |
| 1184 | + let clock: TestClock<Duration> |
| 1185 | + @ObservableState |
| 1186 | + struct State { var count = 0 } |
| 1187 | + enum Action { case tap, response1, response2 } |
| 1188 | + var body: some ReducerOf<Self> { |
| 1189 | + Reduce { state, action in |
| 1190 | + switch action { |
| 1191 | + case .tap: |
| 1192 | + return Effect.run { send in |
| 1193 | + await send(.response1) |
| 1194 | + } |
| 1195 | + case .response1: |
| 1196 | + state.count = 42 |
| 1197 | + return Effect.run { send in |
| 1198 | + try await clock.sleep(for: .seconds(1)) |
| 1199 | + await send(.response2) |
| 1200 | + } |
| 1201 | + case .response2: |
| 1202 | + state.count = 1729 |
| 1203 | + return .none |
| 1204 | + } |
| 1205 | + } |
| 1206 | + } |
| 1207 | + } |
| 1208 | + |
| 1209 | + @MainActor |
| 1210 | + @Test |
| 1211 | + func cancellation() async throws { |
| 1212 | + let clock = TestClock() |
| 1213 | + let store = Store(initialState: TaskTreeFeature.State()) { TaskTreeFeature(clock: clock) } |
| 1214 | + let task = store.send(.tap) |
| 1215 | + try await Task.sleep(for: .seconds(0.1)) |
| 1216 | + #expect(store.count == 42) |
| 1217 | + task.cancel() |
| 1218 | + await clock.run() |
| 1219 | + withKnownIssue("Cancelling the root effect should not cancel the child effects.") { |
| 1220 | + #expect(store.count == 1729) |
| 1221 | + } |
| 1222 | + } |
| 1223 | + } |
| 1224 | +#endif |
| 1225 | + |
1175 | 1226 | private struct Count: TestDependencyKey { |
1176 | 1227 | var value: Int |
1177 | 1228 | static let liveValue = Count(value: 0) |
|
0 commit comments