mirror of
https://github.com/vector-im/element-ios.git
synced 2024-09-29 07:42:40 +00:00
Allow defer in xcAwait. simplify sending state actions from a publisher. Fix tests.
This commit is contained in:
parent
fb0f023964
commit
ed82cec9f8
6 changed files with 55 additions and 45 deletions
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -34,6 +34,25 @@ extension XCTestCase {
|
|||
_ publisher: T,
|
||||
timeout: TimeInterval = 10
|
||||
) 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>?
|
||||
let expectation = self.expectation(description: "Awaiting publisher")
|
||||
|
||||
|
@ -52,12 +71,14 @@ extension XCTestCase {
|
|||
result = .success(value)
|
||||
}
|
||||
)
|
||||
waitForExpectations(timeout: timeout)
|
||||
cancellable.cancel()
|
||||
let unwrappedResult = try XCTUnwrap(
|
||||
result,
|
||||
"Awaited publisher did not produce any output"
|
||||
)
|
||||
return try unwrappedResult.get()
|
||||
return {
|
||||
self.waitForExpectations(timeout: timeout)
|
||||
cancellable.cancel()
|
||||
let unwrappedResult = try XCTUnwrap(
|
||||
result,
|
||||
"Awaited publisher did not produce any output"
|
||||
)
|
||||
return try unwrappedResult.get()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
///
|
||||
/// 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.
|
||||
/// 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.
|
||||
@available(iOS 14, *)
|
||||
class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
|
||||
|
||||
typealias Context = ViewModelContext<State, ViewAction>
|
||||
|
@ -105,7 +105,8 @@ class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
|
|||
self.context = Context(initialViewState: initialViewState)
|
||||
self.state = CurrentValueSubject(initialViewState)
|
||||
// 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)
|
||||
// Receive events from the view and pass on to the `ViewModel` for processing.
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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`
|
||||
///
|
||||
/// A redux style reducer, all modifications to state happen here.
|
||||
|
|
|
@ -37,6 +37,6 @@ class MockTemplateUserProfileService: TemplateUserProfileServiceProtocol {
|
|||
}
|
||||
|
||||
func simulateUpdate(presence: TemplateUserProfilePresence) {
|
||||
self.presenceSubject.send(presence)
|
||||
self.presenceSubject.value = presence
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,29 +26,32 @@ class TemplateUserProfileViewModelTests: XCTestCase {
|
|||
static let displayName = "Alice"
|
||||
}
|
||||
var service: MockTemplateUserProfileService!
|
||||
var viewModel: TemplateUserProfileViewModel!
|
||||
var viewModel: TemplateUserProfileViewModelProtocol!
|
||||
var context: TemplateUserProfileViewModelType.Context!
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
override func setUpWithError() throws {
|
||||
service = MockTemplateUserProfileService(displayName: Constants.displayName, presence: Constants.presenceInitialValue)
|
||||
viewModel = TemplateUserProfileViewModel(templateUserProfileService: service)
|
||||
viewModel = TemplateUserProfileViewModel.makeTemplateUserProfileViewModel(templateUserProfileService: service)
|
||||
context = viewModel.context
|
||||
}
|
||||
|
||||
func testInitialState() {
|
||||
XCTAssertEqual(viewModel.viewState.displayName, Constants.displayName)
|
||||
XCTAssertEqual(viewModel.viewState.presence, Constants.presenceInitialValue)
|
||||
XCTAssertEqual(context.viewState.displayName, Constants.displayName)
|
||||
XCTAssertEqual(context.viewState.presence, Constants.presenceInitialValue)
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
||||
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 newPresenceValue2: TemplateUserProfilePresence = .idle
|
||||
service.simulateUpdate(presence: newPresenceValue1)
|
||||
service.simulateUpdate(presence: newPresenceValue2)
|
||||
XCTAssertEqual(try xcAwait(presencePublisher), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
|
||||
XCTAssertEqual(try awaitDeferred(), [Constants.presenceInitialValue, newPresenceValue1, newPresenceValue2])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -57,9 +57,10 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
|
|||
}
|
||||
|
||||
private func setupPresenceObserving() {
|
||||
templateUserProfileService.presenceSubject
|
||||
let presenceUpdatePublisher = templateUserProfileService.presenceSubject
|
||||
.map(TemplateUserProfileStateAction.updatePresence)
|
||||
.sinkDispatchTo(self)
|
||||
.eraseToAnyPublisher()
|
||||
dispatch(actionPublisher: presenceUpdatePublisher)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
|
Loading…
Reference in a new issue