diff --git a/Examples/xx-messenger/Sources/ContactFeature/ContactFeature.swift b/Examples/xx-messenger/Sources/ContactFeature/ContactFeature.swift index 37388359cf5266757bd2e0585773df078556cd2d..7bbb5f3c592294772f207085701fe72d5bbd237c 100644 --- a/Examples/xx-messenger/Sources/ContactFeature/ContactFeature.swift +++ b/Examples/xx-messenger/Sources/ContactFeature/ContactFeature.swift @@ -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 diff --git a/Examples/xx-messenger/Sources/ContactFeature/ContactView.swift b/Examples/xx-messenger/Sources/ContactFeature/ContactView.swift index ca5eb328b000e13c65cccb29a971875d7b561552..6bf5015054fbf40955de738479897b07a7499632 100644 --- a/Examples/xx-messenger/Sources/ContactFeature/ContactView.swift +++ b/Examples/xx-messenger/Sources/ContactFeature/ContactView.swift @@ -159,6 +159,7 @@ public struct ContactView: View { } } .navigationTitle("Contact") + .task { viewStore.send(.start) } } } } diff --git a/Examples/xx-messenger/Tests/ContactFeatureTests/ContactFeatureTests.swift b/Examples/xx-messenger/Tests/ContactFeatureTests/ContactFeatureTests.swift index b0d3e8b68dbfd6313acbf026cc53e0f5fe738584..f14c9e395b290056985e6aed67fa29b051e0b6f6 100644 --- a/Examples/xx-messenger/Tests/ContactFeatureTests/ContactFeatureTests.swift +++ b/Examples/xx-messenger/Tests/ContactFeatureTests/ContactFeatureTests.swift @@ -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() {