mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Retrofit deferFulfillment onto snapshot tests. (#3641)
* Retrofit `deferFulfillment` onto snapshot tests. * Convert a bunch of preview tests to the new fulfillment publisher * Convert more tests * Remove unneeded delays from the remaining tests * Remove snapshotting delay option.
This commit is contained in:
@@ -36,3 +36,17 @@ extension Publisher where Output == String, Failure == Never {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Published.Publisher {
|
||||
/// Returns the next output from the publisher skipping the current value stored into it (which is readable from the @Published property itself).
|
||||
/// - Returns: the next output from the publisher
|
||||
var nextValue: Output? {
|
||||
get async {
|
||||
var iterator = values.makeAsyncIterator()
|
||||
|
||||
// skips the publisher's current value
|
||||
_ = await iterator.next()
|
||||
return await iterator.next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,33 +14,43 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
public struct SnapshotDelayPreferenceKey: PreferenceKey {
|
||||
public static var defaultValue: TimeInterval = 0.0
|
||||
struct SnapshotPrecisionPreferenceKey: PreferenceKey {
|
||||
static var defaultValue: Float = 1.0
|
||||
|
||||
public static func reduce(value: inout TimeInterval, nextValue: () -> TimeInterval) {
|
||||
static func reduce(value: inout Float, nextValue: () -> Float) {
|
||||
value = nextValue()
|
||||
}
|
||||
}
|
||||
|
||||
public struct SnapshotPrecisionPreferenceKey: PreferenceKey {
|
||||
public static var defaultValue: Float = 1.0
|
||||
struct SnapshotPerceptualPrecisionPreferenceKey: PreferenceKey {
|
||||
static var defaultValue: Float = 0.98
|
||||
|
||||
public static func reduce(value: inout Float, nextValue: () -> Float) {
|
||||
static func reduce(value: inout Float, nextValue: () -> Float) {
|
||||
value = nextValue()
|
||||
}
|
||||
}
|
||||
|
||||
public struct SnapshotPerceptualPrecisionPreferenceKey: PreferenceKey {
|
||||
public static var defaultValue: Float = 0.98
|
||||
struct FulfillmentPublisherEquatableWrapper: Equatable {
|
||||
let publisher: AnyPublisher<Bool, Never>?
|
||||
|
||||
// Publisher equatability complicates things but, luckily, we're only interesting in them changing from nil
|
||||
static func == (lhs: FulfillmentPublisherEquatableWrapper, rhs: FulfillmentPublisherEquatableWrapper) -> Bool {
|
||||
lhs.publisher != nil && rhs.publisher != nil
|
||||
}
|
||||
}
|
||||
|
||||
public static func reduce(value: inout Float, nextValue: () -> Float) {
|
||||
struct SnapshotFulfillmentPublisherPreferenceKey: PreferenceKey {
|
||||
static var defaultValue: FulfillmentPublisherEquatableWrapper?
|
||||
|
||||
static func reduce(value: inout FulfillmentPublisherEquatableWrapper?, nextValue: () -> FulfillmentPublisherEquatableWrapper?) {
|
||||
value = nextValue()
|
||||
}
|
||||
}
|
||||
|
||||
public extension SwiftUI.View {
|
||||
extension SwiftUI.View {
|
||||
/// Use this modifier when you want to apply snapshot-specific preferences,
|
||||
/// like delay and precision, to the view.
|
||||
/// These preferences can then be retrieved and used elsewhere in your view hierarchy.
|
||||
@@ -49,10 +59,11 @@ public extension SwiftUI.View {
|
||||
/// - delay: The delay time in seconds that you want to set as a preference to the View.
|
||||
/// - precision: The percentage of pixels that must match.
|
||||
/// - perceptualPrecision: The percentage a pixel must match the source pixel to be considered a match. 98-99% mimics the precision of the human eye.
|
||||
@inlinable
|
||||
func snapshotPreferences(delay: TimeInterval = .zero, precision: Float = 1.0, perceptualPrecision: Float = 0.98) -> some SwiftUI.View {
|
||||
preference(key: SnapshotDelayPreferenceKey.self, value: delay)
|
||||
.preference(key: SnapshotPrecisionPreferenceKey.self, value: precision)
|
||||
func snapshotPreferences(expect fulfillmentPublisher: (any Publisher<Bool, Never>)? = nil,
|
||||
precision: Float = 1.0,
|
||||
perceptualPrecision: Float = 0.98) -> some SwiftUI.View {
|
||||
preference(key: SnapshotPrecisionPreferenceKey.self, value: precision)
|
||||
.preference(key: SnapshotPerceptualPrecisionPreferenceKey.self, value: perceptualPrecision)
|
||||
.preference(key: SnapshotFulfillmentPublisherPreferenceKey.self, value: FulfillmentPublisherEquatableWrapper(publisher: fulfillmentPublisher?.eraseToAnyPublisher()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// Copyright 2023, 2024 New Vector Ltd.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Please see LICENSE in the repository root for full details.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import XCTest
|
||||
|
||||
extension XCTestCase {
|
||||
/// XCTest utility that assists in subscribing to a publisher and deferring the fulfilment and results until some other actions have been performed.
|
||||
/// - Parameters:
|
||||
/// - publisher: The publisher to wait on.
|
||||
/// - timeout: A timeout after which we give up.
|
||||
/// - message: An optional custom expectation message
|
||||
/// - until: callback that evaluates outputs until some condition is reached
|
||||
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the publisher.
|
||||
func deferFulfillment<P: Publisher>(_ publisher: P,
|
||||
timeout: TimeInterval = 10,
|
||||
message: String? = nil,
|
||||
until condition: @escaping (P.Output) -> Bool) -> DeferredFulfillment<P.Output> {
|
||||
var result: Result<P.Output, Error>?
|
||||
let expectation = expectation(description: message ?? "Awaiting publisher")
|
||||
var hasFullfilled = false
|
||||
let cancellable = publisher
|
||||
.sink { completion in
|
||||
switch completion {
|
||||
case .failure(let error):
|
||||
result = .failure(error)
|
||||
expectation.fulfill()
|
||||
case .finished:
|
||||
break
|
||||
}
|
||||
} receiveValue: { value in
|
||||
if condition(value), !hasFullfilled {
|
||||
result = .success(value)
|
||||
expectation.fulfill()
|
||||
hasFullfilled = true
|
||||
}
|
||||
}
|
||||
|
||||
return DeferredFulfillment<P.Output> {
|
||||
await self.fulfillment(of: [expectation], timeout: timeout)
|
||||
cancellable.cancel()
|
||||
let unwrappedResult = try XCTUnwrap(result, "Awaited publisher did not produce any output")
|
||||
return try unwrappedResult.get()
|
||||
}
|
||||
}
|
||||
|
||||
/// XCTest utility that assists in subscribing to a publisher and deferring the fulfilment and results until some other actions have been performed.
|
||||
/// - Parameters:
|
||||
/// - publisher: The publisher to wait on.
|
||||
/// - keyPath: the key path for the expected values
|
||||
/// - transitionValues: the values through which the keypath needs to transition through
|
||||
/// - timeout: A timeout after which we give up.
|
||||
/// - message: An optional custom expectation message
|
||||
/// - Returns: The deferred fulfilment to be executed after some actions and that returns the result of the publisher.
|
||||
func deferFulfillment<P: Publisher, K: KeyPath<P.Output, V>, V: Equatable>(_ publisher: P,
|
||||
keyPath: K,
|
||||
transitionValues: [V],
|
||||
timeout: TimeInterval = 10,
|
||||
message: String? = nil) -> DeferredFulfillment<P.Output> {
|
||||
var expectedOrder = transitionValues
|
||||
let deferred = deferFulfillment<P>(publisher, timeout: timeout, message: message) { value in
|
||||
let receivedValue = value[keyPath: keyPath]
|
||||
if let index = expectedOrder.firstIndex(where: { $0 == receivedValue }), index == 0 {
|
||||
expectedOrder.remove(at: index)
|
||||
}
|
||||
|
||||
return expectedOrder.isEmpty
|
||||
}
|
||||
|
||||
return deferred
|
||||
}
|
||||
|
||||
/// XCTest utility that assists in subscribing to a publisher and deferring the failure for a particular value until some other actions have been performed.
|
||||
/// - Parameters:
|
||||
/// - publisher: The publisher to wait on.
|
||||
/// - timeout: A timeout after which we give up.
|
||||
/// - message: An optional custom expectation message
|
||||
/// - until: callback that evaluates outputs until some condition is reached
|
||||
/// - Returns: The deferred fulfilment to be executed after some actions. The publisher's result is not returned from this fulfilment.
|
||||
func deferFailure<P: Publisher>(_ publisher: P,
|
||||
timeout: TimeInterval,
|
||||
message: String? = nil,
|
||||
until condition: @escaping (P.Output) -> Bool) -> DeferredFulfillment<Void> where P.Failure == Never {
|
||||
let expectation = expectation(description: message ?? "Awaiting publisher")
|
||||
expectation.isInverted = true
|
||||
var hasFulfilled = false
|
||||
let cancellable = publisher
|
||||
.sink { value in
|
||||
if condition(value), !hasFulfilled {
|
||||
expectation.fulfill()
|
||||
hasFulfilled = true
|
||||
}
|
||||
}
|
||||
|
||||
return DeferredFulfillment<Void> {
|
||||
await self.fulfillment(of: [expectation], timeout: timeout)
|
||||
cancellable.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
struct DeferredFulfillment<T> {
|
||||
let closure: () async throws -> T
|
||||
@discardableResult func fulfill() async throws -> T {
|
||||
try await closure()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user