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

Fetch contacts in NewGroupComponent

parent aef5715c
No related branches found
No related tags found
2 merge requests!153Release 1.1.0,!149[Messenger example] create new group
This commit is part of merge request !149. Comments created here will be created in the context of that merge request.
import AppCore
import ComposableArchitecture import ComposableArchitecture
import Foundation
import XXModels
public struct NewGroupComponent: ReducerProtocol { public struct NewGroupComponent: ReducerProtocol {
public struct State: Equatable { 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 { public enum Action: Equatable {
case start case start
case didFetchContacts([XXModels.Contact])
case didFinish case didFinish
} }
public init() {} 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> { public func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action { switch action {
case .start: 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 return .none
case .didFinish: case .didFinish:
......
import Combine
import ComposableArchitecture import ComposableArchitecture
import CustomDump
import XCTest import XCTest
import XXModels
@testable import NewGroupFeature @testable import NewGroupFeature
final class NewGroupComponentTests: XCTestCase { 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() { func testStart() {
let contactsSubject = PassthroughSubject<[XXModels.Contact], Error>()
let store = TestStore( let store = TestStore(
initialState: NewGroupComponent.State(), initialState: NewGroupComponent.State(),
reducer: NewGroupComponent() 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(.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() { func testFinish() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment