Update the template screen to use the new(ish) Observation framework. (#4077)

* Update the template screen to use the new(ish) Observation framework.

* Add a variant of deferFulfillment that supports observables.

* Update snapshot fulfilment to work with either a publisher or a stream.
This commit is contained in:
Doug
2025-04-30 16:49:23 +01:00
committed by GitHub
parent 0620bd45fc
commit 61a427b8e5
12 changed files with 297 additions and 25 deletions
+10 -4
View File
@@ -57,15 +57,21 @@ class PreviewTests: XCTestCase {
let preferenceReadingView = preview.content
.onPreferenceChange(SnapshotPrecisionPreferenceKey.self) { preferences.precision = $0 }
.onPreferenceChange(SnapshotPerceptualPrecisionPreferenceKey.self) { preferences.perceptualPrecision = $0 }
.onPreferenceChange(SnapshotFulfillmentPublisherPreferenceKey.self) { preferences.fulfillmentPublisher = $0?.publisher }
.onPreferenceChange(SnapshotFulfillmentPreferenceKey.self) { preferences.fulfillmentSource = $0?.source }
// Render an image of the view in order to trigger the preference updates to occur.
let imageRenderer = ImageRenderer(content: preferenceReadingView)
_ = imageRenderer.uiImage
if let fulfillmentPublisher = preferences.fulfillmentPublisher {
let deferred = deferFulfillment(fulfillmentPublisher) { $0 == true }
switch preferences.fulfillmentSource {
case .publisher(let publisher):
let deferred = deferFulfillment(publisher) { $0 == true }
try await deferred.fulfill()
case .stream(let stream):
let deferred = deferFulfillment(stream) { $0 == true }
try await deferred.fulfill()
case .none:
break
}
var sanitizedSuiteName = String(testName.suffix(testName.count - "test".count).dropLast(2))
@@ -152,7 +158,7 @@ class PreviewTests: XCTestCase {
private class SnapshotPreferences: @unchecked Sendable {
var precision: Float = 1
var perceptualPrecision: Float = 1
var fulfillmentPublisher: AnyPublisher<Bool, Never>?
var fulfillmentSource: SnapshotFulfillmentPreferenceKey.Source?
}
// MARK: - SnapshotTesting + Extensions