Skip to content

Commit d48a247

Browse files
authored
test: Add SwiftUI test to reproduce SwiftUI lifecycle only bugs (#6268)
* test: Add SwiftUI test * Updates
1 parent 0309548 commit d48a247

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Samples/iOS-SwiftUI/iOS-SwiftUI-UITests/LaunchUITests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Sentry
12
import SentrySampleShared
23
import SentrySampleUITestShared
34
import XCTest
@@ -48,6 +49,15 @@ class LaunchUITests: XCTestCase {
4849

4950
XCTAssertEqual(app.staticTexts["TTDInfo"].label, "TTID and TTFD found")
5051
}
52+
53+
func testCaptureErrorReturnsValidId() {
54+
let app = newAppSession()
55+
app.safelyLaunch()
56+
app.buttons["Capture Error"].tap()
57+
let errorId = app.staticTexts["errorId"].label
58+
let sentryErrorId = SentryId(uuidString: errorId)
59+
XCTAssertNotEqual(sentryErrorId.sentryIdString, SentryId.empty.sentryIdString, "The id should not be empty, since empty is used to indicate the capture did not work.")
60+
}
5161

5262
func newAppSession() -> XCUIApplication {
5363
let app = XCUIApplication()

Samples/iOS-SwiftUI/iOS-SwiftUI/ContentView.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DataBag {
1616
struct ContentView: View {
1717

1818
@State var TTDInfo: String = ""
19+
@State var errorId: SentryId?
1920

2021
var addBreadcrumbAction: () -> Void = {
2122
let crumb = Breadcrumb(level: SentryLevel.info, category: "Debug")
@@ -46,9 +47,9 @@ struct ContentView: View {
4647
#endif // SDK_V9
4748
}
4849

49-
var captureErrorAction: () -> Void = {
50+
func captureErrorAction() {
5051
let error = NSError(domain: "SampleErrorDomain", code: 1, userInfo: [NSLocalizedDescriptionKey: "Object does not exist"])
51-
SentrySDK.capture(error: error) { (scope) in
52+
errorId = SentrySDK.capture(error: error) { (scope) in
5253
scope.setTag(value: "value", key: "myTag")
5354
}
5455
}
@@ -239,7 +240,10 @@ struct ContentView: View {
239240
.background(Color.white)
240241
}
241242
SecondView()
242-
243+
if let errorId {
244+
Text(errorId.sentryIdString)
245+
.accessibilityIdentifier("errorId")
246+
}
243247
Text(TTDInfo)
244248
.accessibilityIdentifier("TTDInfo")
245249
}

0 commit comments

Comments
 (0)