|
| 1 | +import Combine |
| 2 | +import ComposableArchitecture |
| 3 | +import XCTest |
| 4 | + |
| 5 | +final class RuntimeWarningTests: XCTestCase { |
| 6 | + func testStoreCreationMainThread() { |
| 7 | + XCTExpectFailure { |
| 8 | + $0.compactDescription == """ |
| 9 | + A store initialized on a non-main thread. … |
| 10 | +
|
| 11 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 12 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 13 | + """ |
| 14 | + } |
| 15 | + |
| 16 | + Task { |
| 17 | + _ = Store<Int, Void>(initialState: 0, reducer: .empty, environment: ()) |
| 18 | + } |
| 19 | + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) |
| 20 | + } |
| 21 | + |
| 22 | + func testEffectFinishedMainThread() { |
| 23 | + XCTExpectFailure { |
| 24 | + $0.compactDescription == """ |
| 25 | + An effect completed on a non-main thread. … |
| 26 | +
|
| 27 | + Effect returned from: |
| 28 | + Action.tap |
| 29 | +
|
| 30 | + Make sure to use ".receive(on:)" on any effects that execute on background threads to \ |
| 31 | + receive their output on the main thread. |
| 32 | +
|
| 33 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 34 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 35 | + """ |
| 36 | + } |
| 37 | + |
| 38 | + enum Action { case tap, response } |
| 39 | + let store = Store( |
| 40 | + initialState: 0, |
| 41 | + reducer: Reducer<Int, Action, Void> { state, action, _ in |
| 42 | + switch action { |
| 43 | + case .tap: |
| 44 | + return Empty() |
| 45 | + .receive(on: DispatchQueue(label: "background")) |
| 46 | + .eraseToEffect() |
| 47 | + case .response: |
| 48 | + return .none |
| 49 | + } |
| 50 | + }, |
| 51 | + environment: () |
| 52 | + ) |
| 53 | + ViewStore(store).send(.tap) |
| 54 | + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) |
| 55 | + } |
| 56 | + |
| 57 | + func testStoreScopeMainThread() { |
| 58 | + XCTExpectFailure { |
| 59 | + [ |
| 60 | + """ |
| 61 | + "Store.scope" was called on a non-main thread. … |
| 62 | +
|
| 63 | + The "Store" class is not thread-safe, and so all interactions with an instance of \ |
| 64 | + "Store" (including all of its scopes and derived view stores) must be done on the main \ |
| 65 | + thread. |
| 66 | + """, |
| 67 | + """ |
| 68 | + A store initialized on a non-main thread. … |
| 69 | +
|
| 70 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 71 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 72 | + """ |
| 73 | + ].contains($0.compactDescription) |
| 74 | + } |
| 75 | + |
| 76 | + let store = Store<Int, Void>(initialState: 0, reducer: .empty, environment: ()) |
| 77 | + Task { |
| 78 | + _ = store.scope(state: { $0 }) |
| 79 | + } |
| 80 | + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) |
| 81 | + } |
| 82 | + |
| 83 | + func testViewStoreSendMainThread() { |
| 84 | + XCTExpectFailure { |
| 85 | + [ |
| 86 | + """ |
| 87 | + "ViewStore.send" was called on a non-main thread with: () … |
| 88 | +
|
| 89 | + The "Store" class is not thread-safe, and so all interactions with an instance of \ |
| 90 | + "Store" (including all of its scopes and derived view stores) must be done on the main \ |
| 91 | + thread. |
| 92 | + """, |
| 93 | + """ |
| 94 | + An effect completed on a non-main thread. … |
| 95 | +
|
| 96 | + Effect returned from: |
| 97 | + () |
| 98 | +
|
| 99 | + Make sure to use ".receive(on:)" on any effects that execute on background threads to \ |
| 100 | + receive their output on the main thread. |
| 101 | +
|
| 102 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 103 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 104 | + """ |
| 105 | + ].contains($0.compactDescription) |
| 106 | + } |
| 107 | + |
| 108 | + let store = Store(initialState: 0, reducer: Reducer<Int, Void, Void>.empty, environment: ()) |
| 109 | + Task { |
| 110 | + ViewStore(store).send(()) |
| 111 | + } |
| 112 | + _ = XCTWaiter.wait(for: [.init()], timeout: 0.1) |
| 113 | + } |
| 114 | + |
| 115 | + func testEffectEmitMainThread() { |
| 116 | + XCTExpectFailure { |
| 117 | + [ |
| 118 | + """ |
| 119 | + An effect completed on a non-main thread. … |
| 120 | +
|
| 121 | + Effect returned from: |
| 122 | + Action.response |
| 123 | +
|
| 124 | + Make sure to use ".receive(on:)" on any effects that execute on background threads to \ |
| 125 | + receive their output on the main thread. |
| 126 | +
|
| 127 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 128 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 129 | + """, |
| 130 | + """ |
| 131 | + An effect published an action on a non-main thread. … |
| 132 | +
|
| 133 | + Effect published: |
| 134 | + Action.response |
| 135 | +
|
| 136 | + Effect returned from: |
| 137 | + Action.tap |
| 138 | +
|
| 139 | + Make sure to use ".receive(on:)" on any effects that execute on background threads to \ |
| 140 | + receive their output on the main thread. |
| 141 | +
|
| 142 | + The "Store" class is not thread-safe, and so all interactions with an instance of "Store" \ |
| 143 | + (including all of its scopes and derived view stores) must be done on the main thread. |
| 144 | + """ |
| 145 | + ] |
| 146 | + .contains($0.compactDescription) |
| 147 | + } |
| 148 | + |
| 149 | + enum Action { case tap, response } |
| 150 | + let store = Store( |
| 151 | + initialState: 0, |
| 152 | + reducer: Reducer<Int, Action, Void> { state, action, _ in |
| 153 | + switch action { |
| 154 | + case .tap: |
| 155 | + return .run { subscriber in |
| 156 | + DispatchQueue(label: "background").async { |
| 157 | + subscriber.send(.response) |
| 158 | + } |
| 159 | + return AnyCancellable { |
| 160 | + } |
| 161 | + } |
| 162 | + case .response: |
| 163 | + return .none |
| 164 | + } |
| 165 | + }, |
| 166 | + environment: () |
| 167 | + ) |
| 168 | + ViewStore(store).send(.tap) |
| 169 | + _ = XCTWaiter.wait(for: [.init()], timeout: 0.2) |
| 170 | + } |
| 171 | + |
| 172 | + func testBindingUnhandledAction() { |
| 173 | + struct State: Equatable { |
| 174 | + @BindableState var value = 0 |
| 175 | + } |
| 176 | + enum Action: BindableAction, Equatable { |
| 177 | + case binding(BindingAction<State>) |
| 178 | + } |
| 179 | + let store = Store( |
| 180 | + initialState: .init(), |
| 181 | + reducer: Reducer<State, Action, ()>.empty, |
| 182 | + environment: () |
| 183 | + ) |
| 184 | + |
| 185 | + var line: UInt! |
| 186 | + XCTExpectFailure { |
| 187 | + line = #line |
| 188 | + ViewStore(store).binding(\.$value).wrappedValue = 42 |
| 189 | + } issueMatcher: { |
| 190 | + $0.compactDescription == """ |
| 191 | + A binding action sent from a view store at "ComposableArchitectureTests/RuntimeWarningTests.swift:\(line+1)" was not handled: |
| 192 | +
|
| 193 | + Action: |
| 194 | + Action.binding(.set(_, 42)) |
| 195 | +
|
| 196 | + To fix this, invoke the "binding()" method on your feature's reducer. |
| 197 | + """ |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments