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

Save contact

parent 4fbe3b68
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!68Messenger example - send auth request
......@@ -79,7 +79,18 @@ public let contactReducer = Reducer<ContactState, ContactAction, ContactEnvironm
return .none
case .saveFactsTapped:
return .none
guard let xxContact = state.xxContact else { return .none }
return .fireAndForget { [state] in
var dbContact = state.dbContact ?? XXModels.Contact(id: state.id)
dbContact.marshaled = xxContact.data
dbContact.username = xxContact.username
dbContact.email = xxContact.email
dbContact.phone = xxContact.phone
_ = try! env.db().saveContact(dbContact)
}
.subscribe(on: env.bgQueue)
.receive(on: env.mainQueue)
.eraseToEffect()
case .sendRequestTapped:
return .none
......
......@@ -159,6 +159,7 @@ public struct ContactView: View {
}
}
.navigationTitle("Contact")
.task { viewStore.send(.start) }
}
}
}
......
......@@ -2,6 +2,7 @@ import Combine
import ComposableArchitecture
import CustomDump
import XCTest
import XXClient
import XXModels
@testable import ContactFeature
......@@ -46,15 +47,51 @@ final class ContactFeatureTests: XCTestCase {
}
func testSaveFacts() {
let dbContact: XXModels.Contact = .init(
id: "contact-id".data(using: .utf8)!
)
var xxContact: XXClient.Contact = .unimplemented("contact-data".data(using: .utf8)!)
xxContact.getFactsFromContact.run = { _ in
[
Fact(fact: "contact-username", type: 0),
Fact(fact: "contact-email", type: 1),
Fact(fact: "contact-phone", type: 2),
]
}
let store = TestStore(
initialState: ContactState(
id: "contact-id".data(using: .utf8)!
id: "contact-id".data(using: .utf8)!,
dbContact: dbContact,
xxContact: xxContact
),
reducer: contactReducer,
environment: .unimplemented
)
var dbDidSaveContact: [XXModels.Contact] = []
store.environment.mainQueue = .immediate
store.environment.bgQueue = .immediate
store.environment.db.run = {
var db: Database = .failing
db.saveContact.run = { contact in
dbDidSaveContact.append(contact)
return contact
}
return db
}
store.send(.saveFactsTapped)
var expectedSavedContact = dbContact
expectedSavedContact.marshaled = xxContact.data
expectedSavedContact.username = "contact-username"
expectedSavedContact.email = "contact-email"
expectedSavedContact.phone = "contact-phone"
XCTAssertNoDifference(dbDidSaveContact, [expectedSavedContact])
}
func testSendRequest() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment