Skip to content

Commit

Permalink
Last few snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Aug 8, 2024
1 parent 74c9523 commit 8d51d1b
Show file tree
Hide file tree
Showing 21 changed files with 96 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,48 @@ public struct AccountServiceButton<Label: View>: View {
}

public init(
_ titleKey: LocalizedStringKey,
_ titleKey: LocalizedStringResource,
systemImage: String = "person.crop.square",
action: @escaping () async -> Void
) where Label == SwiftUI.Label<Text, Image> {
self.init(titleKey, systemImage: systemImage, state: .constant(.idle), action: action)
}

public init( // swiftlint:disable:this function_default_parameter_at_end
_ titleKey: LocalizedStringKey,
_ titleKey: LocalizedStringResource,
systemImage: String = "person.crop.square",
state: Binding<ViewState>,
action: @escaping () async throws -> Void
) where Label == SwiftUI.Label<Text, Image> {
self.init(state: state, action: action) {
SwiftUI.Label(titleKey, systemImage: systemImage)
SwiftUI.Label {
Text(titleKey)
} icon: {
Image(systemName: systemImage) // swiftlint:disable:this accessibility_label_for_image
}
}
}

public init(
_ titleKey: LocalizedStringKey,
_ titleKey: LocalizedStringResource,
image: ImageResource,
action: @escaping () async -> Void
) where Label == SwiftUI.Label<Text, Image> {
self.init(titleKey, image: image, state: .constant(.idle), action: action)
}

public init(
_ titleKey: LocalizedStringKey,
_ titleKey: LocalizedStringResource,
image: ImageResource,
state: Binding<ViewState>,
action: @escaping () async throws -> Void
) where Label == SwiftUI.Label<Text, Image> {
self.init(state: state, action: action) {
SwiftUI.Label(titleKey, image: image)
SwiftUI.Label {
Text(titleKey)
} icon: {
Image(image) // swiftlint:disable:this accessibility_label_for_image
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public struct AccountSetupProviderView<Signup: View, PasswordReset: View>: View
}
}

@_disfavoredOverload
private init(
loginClosure: ((UserIdPasswordCredential) async throws -> Void)? = nil,
@ViewBuilder signup signupForm: () -> Signup = { EmptyView() },
Expand Down Expand Up @@ -141,18 +142,6 @@ public struct AccountSetupProviderView<Signup: View, PasswordReset: View>: View
}


/// A setup view that supports signup.
/// - Parameters:
/// - signupForm: The view that is shown as a sheet, if the user presses the signup button. Pass an `EmptyView` if signup isn't supported.
public init(
@ViewBuilder signup signupForm: () -> Signup
) where PasswordReset == EmptyView {
self.init(loginClosure: nil, signup: signupForm) {
EmptyView()
}
}


/// A setup view that supports login, signup and password reset.
/// - Parameters:
/// - login: A closure that is called once a user tries to login with their credentials.
Expand All @@ -165,7 +154,7 @@ public struct AccountSetupProviderView<Signup: View, PasswordReset: View>: View
resetPassword: @escaping (String) async throws -> Void
) where Signup == NavigationStack<NavigationPath, SignupForm<SignupFormHeader>>,
PasswordReset == NavigationStack<NavigationPath, PasswordResetView<SuccessfulPasswordResetView>> {
self.init(loginClosure: login) {
self.init(login: login) {
NavigationStack {
SignupForm(signup: signup)
}
Expand Down Expand Up @@ -202,7 +191,7 @@ public struct AccountSetupProviderView<Signup: View, PasswordReset: View>: View
resetPassword: @escaping (String) async throws -> Void
) where Signup == NavigationStack<NavigationPath, SignupForm<SignupFormHeader>>,
PasswordReset == NavigationStack<NavigationPath, PasswordResetView<SuccessfulPasswordResetView>> {
self.init(loginClosure: nil) {
self.init {
NavigationStack {
SignupForm(signup: signup)
}
Expand Down Expand Up @@ -239,12 +228,10 @@ public struct AccountSetupProviderView<Signup: View, PasswordReset: View>: View
public init(
signup: @escaping (AccountDetails) async throws -> Void
) where Signup == NavigationStack<NavigationPath, SignupForm<SignupFormHeader>>, PasswordReset == EmptyView {
self.init(loginClosure: nil) {
self.init {
NavigationStack {
SignupForm(signup: signup)
}
} passwordReset: {
EmptyView()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ struct SignupSetupView<Credential: Sendable>: View {

var body: some View {
VStack {
AccountServiceButton("UP_SIGNUP") {
AccountServiceButton(LocalizedStringResource("UP_SIGNUP", bundle: .atURL(from: .module))) {
presentingSignupSheet = true
}
.padding(.bottom, 12)
.padding(.bottom, 12)
if let loginClosure {
HStack {
Text("Already got an Account?", bundle: .module)
Expand All @@ -31,7 +31,7 @@ struct SignupSetupView<Credential: Sendable>: View {
Text("UP_LOGIN", bundle: .module)
}
}
.font(.footnote)
.font(.footnote)
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions Tests/SpeziAccountTests/AccountDetailsCacheTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ final class AccountDetailsCacheTests: XCTestCase {
XCTAssertNil(nilEntry2)
}

// TODO: snapshot test some of the display views + preferred setup views? + AccountHeader!

@MainActor
func testApplyModifications() async {
continueAfterFailure = true // ensure entries are cleared at the end
Expand Down
82 changes: 75 additions & 7 deletions Tests/SpeziAccountTests/SnapshotTesting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ final class SnapshotTesting: XCTestCase {
let viewTrueYes = BoolDisplayView<MockBoolKey>(label: .yesNo, true)
let viewFalseNo = BoolDisplayView<MockBoolKey>(label: .yesNo, false)

// TODO: doesn't test accessibility?
#if os(iOS)
assertSnapshot(of: viewTrue, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-viewTrue")
assertSnapshot(of: viewFalse, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-viewFalse")
Expand Down Expand Up @@ -52,26 +51,95 @@ final class SnapshotTesting: XCTestCase {
#endif
}

// TODO: for string and enum as well?
@MainActor
func testStringDisplayView() {
let view = StringDisplayView(\.userId, "Hello World")

#if os(iOS)
assertSnapshot(of: view, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone")
#endif
}

@MainActor
func testLocalizedStringDisplayView() {
let view = LocalizableStringDisplayView(\.genderIdentity, .male)

#if os(iOS)
assertSnapshot(of: view, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone")
#endif
}

@MainActor
func testAccountProviderViewLayout() {
func testAccountProviderViewLayoutVariations() {
let configuration = AccountConfiguration(service: InMemoryAccountService())
withDependencyResolution {
configuration
}

let view = AccountSetupProviderView { _ in
let view0 = AccountSetupProviderView { _ in
} signup: { _ in
} resetPassword: { _ in
}
.environment(configuration.account)

let viewSignup = view.preferredAccountSetupStyle(.signup)
let view1 = AccountSetupProviderView { _ in
} signup: { _ in
}
.environment(configuration.account)

let view2 = AccountSetupProviderView { (_: UserIdPasswordCredential) in
} resetPassword: { _ in
}
.environment(configuration.account)

let view3 = AccountSetupProviderView { (_: AccountDetails) in
} resetPassword: { _ in
}
.environment(configuration.account)

let view4 = AccountSetupProviderView { (_: UserIdPasswordCredential) in
}
.environment(configuration.account)

let view5 = AccountSetupProviderView { (_: AccountDetails) in
}
.environment(configuration.account)

let view0Signup = view0.preferredAccountSetupStyle(.signup)
let view1Signup = view1.preferredAccountSetupStyle(.signup)
let view2Signup = view2.preferredAccountSetupStyle(.signup)
let view3Signup = view3.preferredAccountSetupStyle(.signup)
let view4Signup = view4.preferredAccountSetupStyle(.signup)
let view5Signup = view5.preferredAccountSetupStyle(.signup)

#if os(iOS)
assertSnapshot(of: view0, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view0")
assertSnapshot(of: view1, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view1")
assertSnapshot(of: view2, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view2")
assertSnapshot(of: view3, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view3")
assertSnapshot(of: view4, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view4")
assertSnapshot(of: view5, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view5")

assertSnapshot(of: view0Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view0-signup")
assertSnapshot(of: view1Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view1-signup")
assertSnapshot(of: view2Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view2-signup")
assertSnapshot(of: view3Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view3-signup")
assertSnapshot(of: view4Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view4-signup")
assertSnapshot(of: view5Signup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-view5-signup")
#endif
}

@MainActor
func testAccountHeader() {
let configuration = AccountConfiguration(service: InMemoryAccountService())
withDependencyResolution {
configuration
}
let view = AccountHeader(caption: "Custom Caption")
.environment(configuration.account)

#if os(iOS)
assertSnapshot(of: view, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-login-variant")
assertSnapshot(of: viewSignup, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone-signup-variant")
assertSnapshot(of: view, as: .image(layout: .device(config: .iPhone13Pro)), named: "iphone")
#endif
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8d51d1b

Please sign in to comment.