Skip to content

Commit a661857

Browse files
test: integration test updates
1 parent 8d9e8bc commit a661857

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/FirebaseSwiftUIExampleTests.swift

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,58 @@ struct FirebaseSwiftUIExampleTests {
105105
#expect(service.errorMessage.isEmpty)
106106
#expect(service.signedInCredential == nil)
107107
#expect(service.currentUser == nil)
108-
try await service.createUser(withEmail: createEmail(), password: kPassword)
109-
try await Task.sleep(nanoseconds: 4_000_000_000)
108+
try await service.createUser(email: createEmail(), password: kPassword)
109+
110+
try await waitForStateChange {
111+
service.authenticationState == .authenticated
112+
}
110113
#expect(service.authenticationState == .authenticated)
114+
115+
try await waitForStateChange {
116+
service.currentUser != nil
117+
}
118+
#expect(service.currentUser != nil)
111119
#expect(service.authView == .authPicker)
112120
#expect(service.errorMessage.isEmpty)
113-
#expect(service.currentUser != nil)
114-
// TODO: - reinstate once this PR is merged: https://github.com/firebase/FirebaseUI-iOS/pull/1256
115-
// #expect(service.signedInCredential is AuthCredential)
116121
}
117122

118123
@Test
119124
@MainActor
120125
func testSignInUser() async throws {
121126
let service = try await prepareFreshAuthService()
122127
let email = createEmail()
123-
try await service.createUser(withEmail: email, password: kPassword)
128+
try await service.createUser(email: email, password: kPassword)
124129
try await service.signOut()
125-
try await Task.sleep(nanoseconds: 2_000_000_000)
130+
131+
try await waitForStateChange {
132+
service.authenticationState == .unauthenticated
133+
}
126134
#expect(service.authenticationState == .unauthenticated)
135+
136+
try await waitForStateChange {
137+
service.currentUser == nil
138+
}
139+
#expect(service.currentUser == nil)
127140
#expect(service.authView == .authPicker)
128141
#expect(service.errorMessage.isEmpty)
129142
#expect(service.signedInCredential == nil)
130-
#expect(service.currentUser == nil)
131143

132-
try await service.signIn(withEmail: email, password: kPassword)
144+
try await service.signIn(email: email, password: kPassword)
133145

146+
try await waitForStateChange {
147+
service.authenticationState == .authenticated
148+
}
134149
#expect(service.authenticationState == .authenticated)
150+
151+
try await waitForStateChange {
152+
service.currentUser != nil
153+
}
154+
#expect(service.currentUser != nil)
155+
try await waitForStateChange {
156+
service.signedInCredential != nil
157+
}
158+
#expect(service.signedInCredential != nil)
135159
#expect(service.authView == .authPicker)
136160
#expect(service.errorMessage.isEmpty)
137-
#expect(service.currentUser != nil)
138-
// TODO: - reinstate once this PR is merged: https://github.com/firebase/FirebaseUI-iOS/pull/1256
139-
// #expect(service.signedInCredential is AuthCredential)
140161
}
141162
}

samples/swiftui/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/TestHarness.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,22 @@ func createEmail() -> String {
6161
let after = UUID().uuidString.prefix(6)
6262
return "\(before)@\(after).com"
6363
}
64+
65+
func waitForStateChange(timeout: TimeInterval = 10.0,
66+
condition: @escaping () -> Bool) async throws {
67+
let startTime = Date()
68+
69+
while !condition() {
70+
if Date().timeIntervalSince(startTime) > timeout {
71+
throw TestError.timeout("Timeout waiting for condition to be met")
72+
}
73+
74+
// Small delay to prevent busy waiting
75+
try await Task.sleep(nanoseconds: 50_000_000) // 50ms
76+
}
77+
}
78+
79+
// Add this simple error type
80+
enum TestError: Error {
81+
case timeout(String)
82+
}

0 commit comments

Comments
 (0)