Allow defer in xcAwait. simplify sending state actions from a publisher. Fix tests.

This commit is contained in:
David Langley 2021-09-15 16:09:41 +01:00
parent fb0f023964
commit ed82cec9f8
6 changed files with 55 additions and 45 deletions

View file

@ -1,26 +0,0 @@
import Foundation
import Combine
@available(iOS 14.0, *)
extension Publisher {
func sinkDispatchTo<S, SA, IA>(_ store: StateStoreViewModel<S, SA, IA>) where SA == Output, Failure == Never {
return self
.subscribe(on: DispatchQueue.main)
.sink { [weak store] (output) in
guard let store = store else { return }
store.dispatch(action: output)
}
.store(in: &store.cancellables)
}
func dispatchTo<S, SA, IA>(_ store: StateStoreViewModel<S, SA, IA>) -> Publishers.HandleEvents<Publishers.SubscribeOn<Self, DispatchQueue>> where SA == Output, Failure == Never {
return self
.subscribe(on: DispatchQueue.main)
.handleEvents(receiveOutput: { [weak store] action in
guard let store = store else { return }
store.dispatch(action: action)
})
}
}

View file

@ -34,6 +34,25 @@ extension XCTestCase {
_ publisher: T, _ publisher: T,
timeout: TimeInterval = 10 timeout: TimeInterval = 10
) throws -> T.Output { ) throws -> T.Output {
return try xcAwaitDeferred(publisher, timeout: timeout)()
}
/// XCTest utility that allows for a deferred wait of results from publishers, so that the output can be used for assertions.
///
/// ```
/// let collectedEvents = somePublisher.collect(3).first()
/// let awaitDeferred = xcAwaitDeferred(collectedEvents)
/// // Do some other work that publishes to somePublisher
/// XCTAssertEqual(try awaitDeferred(), [expected, values, here])
/// ```
/// - Parameters:
/// - publisher: The publisher to wait on.
/// - timeout: A timeout after which we give up.
/// - Returns: A closure that starts the waiting of results when called. The closure will return the unwrapped result.
func xcAwaitDeferred<T: Publisher>(
_ publisher: T,
timeout: TimeInterval = 10
) -> (() throws -> (T.Output)) {
var result: Result<T.Output, Error>? var result: Result<T.Output, Error>?
let expectation = self.expectation(description: "Awaiting publisher") let expectation = self.expectation(description: "Awaiting publisher")
@ -52,12 +71,14 @@ extension XCTestCase {
result = .success(value) result = .success(value)
} }
) )
waitForExpectations(timeout: timeout) return {
cancellable.cancel() self.waitForExpectations(timeout: timeout)
let unwrappedResult = try XCTUnwrap( cancellable.cancel()
result, let unwrappedResult = try XCTUnwrap(
"Awaited publisher did not produce any output" result,
) "Awaited publisher did not produce any output"
return try unwrappedResult.get() )
return try unwrappedResult.get()
}
} }
} }

View file

@ -72,13 +72,13 @@ class ViewModelContext<ViewState:BindableState, ViewAction>: ObservableObject {
} }
} }
@available(iOS 14, *)
/// A common ViewModel implementation for handling of `State`, `StateAction`s and `ViewAction`s /// A common ViewModel implementation for handling of `State`, `StateAction`s and `ViewAction`s
/// ///
/// Generic type State is constrained to the BindableState protocol in that it may contain (but doesn't have to) /// Generic type State is constrained to the BindableState protocol in that it may contain (but doesn't have to)
/// a specific portion of state that can be safely bound to. /// a specific portion of state that can be safely bound to.
/// If we decide to add more features to our state management (like doing state processing off the main thread) /// If we decide to add more features to our state management (like doing state processing off the main thread)
/// we can do it in this centralised place. /// we can do it in this centralised place.
@available(iOS 14, *)
class StateStoreViewModel<State: BindableState, StateAction, ViewAction> { class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
typealias Context = ViewModelContext<State, ViewAction> typealias Context = ViewModelContext<State, ViewAction>
@ -105,7 +105,8 @@ class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
self.context = Context(initialViewState: initialViewState) self.context = Context(initialViewState: initialViewState)
self.state = CurrentValueSubject(initialViewState) self.state = CurrentValueSubject(initialViewState)
// Connect the state to context viewState, that view uses for observing (but not modifying directly) the state. // Connect the state to context viewState, that view uses for observing (but not modifying directly) the state.
self.state.weakAssign(to: \.context.viewState, on: self) self.state
.weakAssign(to: \.context.viewState, on: self)
.store(in: &cancellables) .store(in: &cancellables)
// Receive events from the view and pass on to the `ViewModel` for processing. // Receive events from the view and pass on to the `ViewModel` for processing.
self.context.viewActions.sink { [weak self] action in self.context.viewActions.sink { [weak self] action in
@ -121,6 +122,16 @@ class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
Self.reducer(state: &state.value, action: action) Self.reducer(state: &state.value, action: action)
} }
/// Send state actions from a publisher to modify the state within the reducer.
/// - Parameter actionPublisher: The publisher that produces actions to be sent to the reducer
func dispatch(actionPublisher: AnyPublisher<StateAction, Never>) {
actionPublisher.sink { [weak self] action in
guard let self = self else { return }
Self.reducer(state: &self.state.value, action: action)
}
.store(in: &cancellables)
}
/// Override to handle mutations to the `State` /// Override to handle mutations to the `State`
/// ///
/// A redux style reducer, all modifications to state happen here. /// A redux style reducer, all modifications to state happen here.

View file

@ -37,6 +37,6 @@ class MockTemplateUserProfileService: TemplateUserProfileServiceProtocol {
} }
func simulateUpdate(presence: TemplateUserProfilePresence) { func simulateUpdate(presence: TemplateUserProfilePresence) {
self.presenceSubject.send(presence) self.presenceSubject.value = presence
} }
} }

View file

@ -26,29 +26,32 @@ class TemplateUserProfileViewModelTests: XCTestCase {
static let displayName = "Alice" static let displayName = "Alice"
} }
var service: MockTemplateUserProfileService! var service: MockTemplateUserProfileService!
var viewModel: TemplateUserProfileViewModel! var viewModel: TemplateUserProfileViewModelProtocol!
var context: TemplateUserProfileViewModelType.Context!
var cancellables = Set<AnyCancellable>() var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws { override func setUpWithError() throws {
service = MockTemplateUserProfileService(displayName: Constants.displayName, presence: Constants.presenceInitialValue) service = MockTemplateUserProfileService(displayName: Constants.displayName, presence: Constants.presenceInitialValue)
viewModel = TemplateUserProfileViewModel(templateUserProfileService: service) viewModel = TemplateUserProfileViewModel.makeTemplateUserProfileViewModel(templateUserProfileService: service)
context = viewModel.context
} }
func testInitialState() { func testInitialState() {
XCTAssertEqual(viewModel.viewState.displayName, Constants.displayName) XCTAssertEqual(context.viewState.displayName, Constants.displayName)
XCTAssertEqual(viewModel.viewState.presence, Constants.presenceInitialValue) XCTAssertEqual(context.viewState.presence, Constants.presenceInitialValue)
} }
func testFirstPresenceReceived() throws { func testFirstPresenceReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(1).first() let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(1).first()
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue]) XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue])
} }
func testPresenceUpdatesReceived() throws { func testPresenceUpdatesReceived() throws {
let presencePublisher = viewModel.$viewState.map(\.presence).removeDuplicates().collect(3).first() let presencePublisher = context.$viewState.map(\.presence).removeDuplicates().collect(3).first()
let awaitDeferred = xcAwaitDeferred(presencePublisher)
let newPresenceValue1: TemplateUserProfilePresence = .online let newPresenceValue1: TemplateUserProfilePresence = .online
let newPresenceValue2: TemplateUserProfilePresence = .idle let newPresenceValue2: TemplateUserProfilePresence = .idle
service.simulateUpdate(presence: newPresenceValue1) service.simulateUpdate(presence: newPresenceValue1)
service.simulateUpdate(presence: newPresenceValue2) service.simulateUpdate(presence: newPresenceValue2)
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2]) XCTAssertEqual(try awaitDeferred(), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
} }
} }

View file

@ -57,9 +57,10 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
} }
private func setupPresenceObserving() { private func setupPresenceObserving() {
templateUserProfileService.presenceSubject let presenceUpdatePublisher = templateUserProfileService.presenceSubject
.map(TemplateUserProfileStateAction.updatePresence) .map(TemplateUserProfileStateAction.updatePresence)
.sinkDispatchTo(self) .eraseToAnyPublisher()
dispatch(actionPublisher: presenceUpdatePublisher)
} }
// MARK: - Public // MARK: - Public