Skip to content

Commit 196c32b

Browse files
committed
Refactored a11y snapshot hadling to be optionally enabled
1 parent 44f3f0f commit 196c32b

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

Sources/TestingExtensions/SnapshotTestBase.swift

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import SwiftUI
1313
import XCTest
1414
import AccessibilitySnapshot
1515

16+
extension SnapshotTestBase {
17+
public typealias DeviceConfiguration = (name: String, device: ViewImageConfig)
18+
}
19+
20+
extension SnapshotTestBase {
21+
/// Configuration of Accessibility snapshots.
22+
public enum A11ySnapshotConfiguration {
23+
case disabled
24+
case enabled
25+
case enabledWithDevices([DeviceConfiguration])
26+
}
27+
}
28+
1629
open class SnapshotTestBase: XCTestCase {
1730
public var allowAnimations: Bool = false
1831

@@ -21,7 +34,7 @@ open class SnapshotTestBase: XCTestCase {
2134
UIView.setAnimationsEnabled(allowAnimations)
2235
}
2336

24-
open var defaultDevices: [(name: String, device: ViewImageConfig)] {
37+
open var defaultDevices: [DeviceConfiguration] {
2538
[
2639
("iPhone8", .iPhone8),
2740
("iPhone13proMax", .iPhone13ProMax),
@@ -30,16 +43,27 @@ open class SnapshotTestBase: XCTestCase {
3043
]
3144
}
3245

33-
open var accessibilityDevices: [(name: String, device: ViewImageConfig)] {
46+
open var a11yDefaultDevices: [DeviceConfiguration] {
3447
[
3548
("iPhone13pro", .iPhone13)
3649
]
3750
}
38-
51+
52+
/// Asserts snapshots on the given devices (or default) respecting it's parameters.
53+
/// - Parameters:
54+
/// - view: The view to be snapshotted
55+
/// - devices: Specified devices, if not given it'll take default devices
56+
/// - a11ySnapshotConfiguration: Accessibility snapshot configuration, if enabled it adds a accessibility snapshot
57+
/// - style: `UIUserInterfaceStyle` to be applied
58+
/// - imageDiffPrecision: Precision of the compared images, 1 means it matches 100%, range is between 0 and 1.
59+
/// - file: The file this was executed from, it will be taken as the snapshot's file name.
60+
/// - testName: The test name taken as a part of the snapshot's file name.
61+
/// - line: The line number on which failure occurred. Defaults to the line number on which this
62+
/// function was called.
3963
open func assertSnapshotDevices<V: View>(
4064
_ view: V,
41-
devices: [(name: String, device: ViewImageConfig)]? = nil,
42-
accessibilityDevices: [(name: String, device: ViewImageConfig)]? = nil,
65+
devices: [DeviceConfiguration]? = nil,
66+
a11ySnapshotConfiguration: A11ySnapshotConfiguration = .disabled,
4367
style: [UIUserInterfaceStyle] = [.unspecified],
4468
imageDiffPrecision: Float = 1.0,
4569
file: StaticString = #file,
@@ -74,7 +98,19 @@ open class SnapshotTestBase: XCTestCase {
7498
}
7599
}
76100

77-
(accessibilityDevices ?? self.accessibilityDevices).forEach { config in
101+
let a11ySnapshotDevices: [DeviceConfiguration]? = {
102+
switch a11ySnapshotConfiguration {
103+
case .disabled:
104+
return nil
105+
case .enabled:
106+
return a11yDefaultDevices
107+
case .enabledWithDevices(let specifiedDevices):
108+
return specifiedDevices
109+
}
110+
}()
111+
112+
guard let a11ySnapshotDevices else { return }
113+
a11ySnapshotDevices.forEach { config in
78114
let vc = UIHostingController(rootView: view)
79115
assertSnapshot(
80116
of: vc,

0 commit comments

Comments
 (0)