From 3f7726d3899e98f3f228de6ecac797a092ffb1a4 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Thu, 1 Dec 2022 11:19:36 +0100 Subject: [PATCH] Fetch contacts in NewGroupComponent --- .../NewGroupFeature/NewGroupComponent.swift | 27 ++++++++++- .../NewGroupComponentTests.swift | 48 ++++++++++++++++++- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/Examples/xx-messenger/Sources/NewGroupFeature/NewGroupComponent.swift b/Examples/xx-messenger/Sources/NewGroupFeature/NewGroupComponent.swift index db9193d0..9bac4d04 100644 --- a/Examples/xx-messenger/Sources/NewGroupFeature/NewGroupComponent.swift +++ b/Examples/xx-messenger/Sources/NewGroupFeature/NewGroupComponent.swift @@ -1,20 +1,45 @@ +import AppCore import ComposableArchitecture +import Foundation +import XXModels public struct NewGroupComponent: ReducerProtocol { public struct State: Equatable { - public init() {} + public init( + contacts: IdentifiedArrayOf<XXModels.Contact> = [] + ) { + self.contacts = contacts + } + + public var contacts: IdentifiedArrayOf<XXModels.Contact> } public enum Action: Equatable { case start + case didFetchContacts([XXModels.Contact]) case didFinish } public init() {} + @Dependency(\.app.dbManager.getDB) var db: DBManagerGetDB + @Dependency(\.app.mainQueue) var mainQueue: AnySchedulerOf<DispatchQueue> + @Dependency(\.app.bgQueue) var bgQueue: AnySchedulerOf<DispatchQueue> + public func reduce(into state: inout State, action: Action) -> EffectTask<Action> { switch action { case .start: + return Effect + .catching { try db() } + .flatMap { $0.fetchContactsPublisher(.init()) } + .assertNoFailure() + .map(Action.didFetchContacts) + .subscribe(on: bgQueue) + .receive(on: mainQueue) + .eraseToEffect() + + case .didFetchContacts(let contacts): + state.contacts = IdentifiedArray(uniqueElements: contacts) return .none case .didFinish: diff --git a/Examples/xx-messenger/Tests/NewGroupFeatureTests/NewGroupComponentTests.swift b/Examples/xx-messenger/Tests/NewGroupFeatureTests/NewGroupComponentTests.swift index 2db124d7..04af1dca 100644 --- a/Examples/xx-messenger/Tests/NewGroupFeatureTests/NewGroupComponentTests.swift +++ b/Examples/xx-messenger/Tests/NewGroupFeatureTests/NewGroupComponentTests.swift @@ -1,16 +1,62 @@ +import Combine import ComposableArchitecture +import CustomDump import XCTest +import XXModels @testable import NewGroupFeature final class NewGroupComponentTests: XCTestCase { + enum Action: Equatable { + case didFetchContacts(XXModels.Contact.Query) + } + + var actions: [Action]! + + override func setUp() { + actions = [] + } + + override func tearDown() { + actions = nil + } + func testStart() { + let contactsSubject = PassthroughSubject<[XXModels.Contact], Error>() + let store = TestStore( initialState: NewGroupComponent.State(), reducer: NewGroupComponent() ) + store.dependencies.app.mainQueue = .immediate + store.dependencies.app.bgQueue = .immediate + store.dependencies.app.dbManager.getDB.run = { + var db: Database = .unimplemented + db.fetchContactsPublisher.run = { query in + self.actions.append(.didFetchContacts(query)) + return contactsSubject.eraseToAnyPublisher() + } + return db + } + store.send(.start) - store.send(.didFinish) + + XCTAssertNoDifference(actions, [ + .didFetchContacts(.init()) + ]) + + let contacts: [XXModels.Contact] = [ + .init(id: "contact-1-id".data(using: .utf8)!), + .init(id: "contact-2-id".data(using: .utf8)!), + .init(id: "contact-3-id".data(using: .utf8)!), + ] + contactsSubject.send(contacts) + + store.receive(.didFetchContacts(contacts)) { + $0.contacts = IdentifiedArray(uniqueElements: contacts) + } + + contactsSubject.send(completion: .finished) } func testFinish() { -- GitLab