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

Fetch contacts in NewGroupComponent

parent aef5715c
Branches
Tags
2 merge requests!153Release 1.1.0,!149[Messenger example] create new group
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:
......
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() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment