Skip to content

Commit

Permalink
Fix account required sheet and make test environment more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Jun 3, 2024
1 parent 3d4216b commit f94e8b2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
44 changes: 21 additions & 23 deletions ENGAGEHF/Account/InvitationCodeModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ class InvitationCodeModule: Module, EnvironmentAccessible {
}
}

func setupTestEnvironment(account: Account, invitationCode: String) async throws {
try await verifyOnboardingCode(invitationCode)
try await setupTestAccount(account: account)
}

@MainActor
private func setupTestAccount(account: Account) async throws {
func setupTestEnvironment(account: Account, invitationCode: String) async throws {
let email = "[email protected]"
let password = "123456789"

// let the initial stateChangeDelegate of FirebaseAuth get called. Otherwise, we will interfere with that.
try await Task.sleep(for: .milliseconds(500))

if let details = account.details,
details.email == email {
return

Check warning on line 89 in ENGAGEHF/Account/InvitationCodeModule.swift

View check run for this annotation

Codecov / codecov/patch

ENGAGEHF/Account/InvitationCodeModule.swift#L89

Added line #L89 was not covered by tests
Expand All @@ -95,25 +93,25 @@ class InvitationCodeModule: Module, EnvironmentAccessible {
preconditionFailure("Failed to retrieve a user-id-password based account service for test account setup!")

Check warning on line 93 in ENGAGEHF/Account/InvitationCodeModule.swift

View check run for this annotation

Codecov / codecov/patch

ENGAGEHF/Account/InvitationCodeModule.swift#L93

Added line #L93 was not covered by tests
}

do {
try await service.login(userId: email, password: password)
return // account was already established previously
} catch {
// probably doesn't exists. We try to create a new one below
}

try await verifyOnboardingCode(invitationCode)
try await setupTestAccount(service: service, email: email, password: password)
}

Check warning on line 105 in ENGAGEHF/Account/InvitationCodeModule.swift

View check run for this annotation

Codecov / codecov/patch

ENGAGEHF/Account/InvitationCodeModule.swift#L104-L105

Added lines #L104 - L105 were not covered by tests

private func setupTestAccount(service: any UserIdPasswordAccountService, email: String, password: String) async throws {
do {
// let the initial stateChangeDelegate of FirebaseAuth get called. Otherwise, we will interfere with that.
try await Task.sleep(for: .milliseconds(500))

do {
let details = SignupDetails.Builder()
.set(\.userId, value: email)
.set(\.name, value: PersonNameComponents(givenName: "Leland", familyName: "Stanford"))
.set(\.password, value: password)
.build()
try await service.signUp(signupDetails: details)
} catch {
if "\(error)".contains("accountAlreadyInUse") {
try await service.login(userId: email, password: password)
} else {
throw error
}
}
let details = SignupDetails.Builder()
.set(\.userId, value: email)
.set(\.name, value: PersonNameComponents(givenName: "Leland", familyName: "Stanford"))
.set(\.password, value: password)
.build()
try await service.signUp(signupDetails: details)
} catch {
logger.error("Failed setting up test account : \(error)")
throw error

Check warning on line 117 in ENGAGEHF/Account/InvitationCodeModule.swift

View check run for this annotation

Codecov / codecov/patch

ENGAGEHF/Account/InvitationCodeModule.swift#L116-L117

Added lines #L116 - L117 were not covered by tests
Expand Down
10 changes: 8 additions & 2 deletions ENGAGEHF/Home.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ struct HomeView: View {
private var bluetoothEnabled: Bool {
!ProcessInfo.processInfo.isPreviewSimulator
}


@Environment(\.dismiss) private var dismiss


@Environment(MeasurementManager.self) private var measurementManager
@Environment(WeightScaleDevice.self) private var weightScale: WeightScaleDevice?
Expand All @@ -53,7 +55,11 @@ struct HomeView: View {
.accountRequired(Self.accountEnabled) {
OnboardingStack {
InvitationCodeView()
AccountSheet()
AccountSetup { _ in
dismiss()

Check warning on line 59 in ENGAGEHF/Home.swift

View check run for this annotation

Codecov / codecov/patch

ENGAGEHF/Home.swift#L59

Added line #L59 was not covered by tests
} header: {
AccountSetupHeader()
}
}
}
.verifyRequiredAccountDetails(Self.accountEnabled)
Expand Down
4 changes: 4 additions & 0 deletions ENGAGEHFUITests/OnboardingUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ extension XCUIApplication {
sleep(2)

// Login
if buttons["I Already Have an Account"].waitForExistence(timeout: 2.0) {
buttons["I Already Have an Account"].tap()
}

XCTAssert(textFields["E-Mail Address"].waitForExistence(timeout: 5))
textFields["E-Mail Address"].tap()
textFields["E-Mail Address"].typeText(email)
Expand Down

0 comments on commit f94e8b2

Please sign in to comment.