-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sample snapshot test for a SwiftUI View
- Loading branch information
Showing
5 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
@testable import GravatarUI | ||
import SnapshotTesting | ||
import SwiftUI | ||
import XCTest | ||
|
||
final class AvatarPickerProfileViewTests: XCTestCase { | ||
override func invokeTest() { | ||
withSnapshotTesting(record: .failed) { | ||
super.invokeTest() | ||
} | ||
} | ||
|
||
@MainActor | ||
func testAvatarPickerProfileView() throws { | ||
let testView = AvatarPickerProfileView( | ||
avatarURL: .constant(nil), | ||
model: .constant( | ||
.init( | ||
displayName: "Shelly Kimbrough", | ||
location: "San Antonio, TX", | ||
profileURL: URL(string: "https://gravatar.com") | ||
) | ||
), | ||
isLoading: .constant(false) | ||
) | ||
// put a border around so the bounds can be visible in the snapshots. | ||
.markBounds() | ||
|
||
assertSnapshots( | ||
of: testView, | ||
as: [ | ||
.testStrategy(userInterfaceStyle: .light), | ||
.testStrategy(userInterfaceStyle: .dark), | ||
] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import SnapshotTesting | ||
import SwiftUI | ||
import XCTest | ||
|
||
extension Snapshotting where Value: SwiftUI.View, Format == UIImage { | ||
static func testStrategy(userInterfaceStyle: UIUserInterfaceStyle = .light) -> Self { | ||
let deviceConfig: ViewImageConfig = .iPhone13 | ||
let traits = UITraitCollection(traitsFrom: [ | ||
UITraitCollection(displayScale: deviceConfig.traits.displayScale), | ||
UITraitCollection(userInterfaceStyle: userInterfaceStyle), | ||
]) | ||
|
||
return .image( | ||
layout: .device(config: deviceConfig), | ||
traits: traits | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import SwiftUI | ||
|
||
extension View { | ||
func markBounds() -> some View { | ||
self | ||
.padding(1) // to prevent borders from intersecting with content | ||
.clipShape(RoundedRectangle(cornerSize: .zero)) | ||
.overlay( | ||
RoundedRectangle(cornerSize: .zero) | ||
.stroke(.red, lineWidth: 1) | ||
) | ||
} | ||
} |
Binary file added
BIN
+43.9 KB
...ts/__Snapshots__/AvatarPickerProfileViewTests/testAvatarPickerProfileView.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+50.7 KB
...ts/__Snapshots__/AvatarPickerProfileViewTests/testAvatarPickerProfileView.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.