Skip to content
Snippets Groups Projects
Commit dd53d9f1 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Handle send errors in ChatFeature

parent 311fbed0
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!87Messenger example - chat
......@@ -39,12 +39,14 @@ public struct ChatState: Equatable, Identifiable {
myContactId: Data? = nil,
messages: IdentifiedArrayOf<Message> = [],
failure: String? = nil,
sendFailure: String? = nil,
text: String = ""
) {
self.id = id
self.myContactId = myContactId
self.messages = messages
self.failure = failure
self.sendFailure = sendFailure
self.text = text
}
......@@ -52,6 +54,7 @@ public struct ChatState: Equatable, Identifiable {
public var myContactId: Data?
public var messages: IdentifiedArrayOf<Message>
public var failure: String?
public var sendFailure: String?
@BindableState public var text: String
}
......@@ -59,6 +62,8 @@ public enum ChatAction: Equatable, BindableAction {
case start
case didFetchMessages(IdentifiedArrayOf<ChatState.Message>)
case sendTapped
case sendFailed(String)
case dismissSendFailureTapped
case binding(BindingAction<ChatState>)
}
......@@ -152,8 +157,7 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment>
text: text,
to: recipientId,
onError: { error in
// TODO: handle error
print("^^^ ERROR: \(error)")
subscriber.send(.sendFailed(error.localizedDescription))
},
completion: {
subscriber.send(completion: .finished)
......@@ -166,6 +170,14 @@ public let chatReducer = Reducer<ChatState, ChatAction, ChatEnvironment>
.receive(on: env.mainQueue)
.eraseToEffect()
case .sendFailed(let failure):
state.sendFailure = failure
return .none
case .dismissSendFailureTapped:
state.sendFailure = nil
return .none
case .binding(_):
return .none
}
......
......@@ -169,4 +169,42 @@ final class ChatFeatureTests: XCTestCase {
sendMessageCompletion?()
}
func testSendFailure() {
var sendMessageOnError: SendMessage.OnError?
var sendMessageCompletion: SendMessage.Completion?
let store = TestStore(
initialState: ChatState(
id: .contact("contact-id".data(using: .utf8)!),
text: "Hello"
),
reducer: chatReducer,
environment: .unimplemented
)
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.sendMessage.run = { _, _, onError, completion in
sendMessageOnError = onError
sendMessageCompletion = completion
}
store.send(.sendTapped) {
$0.text = ""
}
let error = NSError(domain: "test", code: 123)
sendMessageOnError?(error)
store.receive(.sendFailed(error.localizedDescription)) {
$0.sendFailure = error.localizedDescription
}
sendMessageCompletion?()
store.send(.dismissSendFailureTapped) {
$0.sendFailure = nil
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment