|
| 1 | +import XCTest |
| 2 | + |
| 3 | +final class RNSentryStartTests: XCTestCase { |
| 4 | + |
| 5 | + func testStartDoesNotThrowWithoutConfigure() { |
| 6 | + RNSentrySDK.start(configureOptions: nil) |
| 7 | + } |
| 8 | + |
| 9 | + func assertReactDefaults(_ actualOptions: Options?) { |
| 10 | + XCTAssertFalse(actualOptions!.enableCaptureFailedRequests) |
| 11 | + XCTAssertNil(actualOptions!.tracesSampleRate) |
| 12 | + XCTAssertNil(actualOptions!.tracesSampler) |
| 13 | + XCTAssertFalse(actualOptions!.enableTracing) |
| 14 | + } |
| 15 | + |
| 16 | + func testStartSetsReactDeafults() { |
| 17 | + var actualOptions: Options? |
| 18 | + |
| 19 | + RNSentrySDK.start { options in |
| 20 | + actualOptions = options |
| 21 | + } |
| 22 | + |
| 23 | + XCTAssertNotNil(actualOptions, "start have not provided default options or have not executed configure callback") |
| 24 | + assertReactDefaults(actualOptions) |
| 25 | + } |
| 26 | + |
| 27 | + func testAutoStartSetsReactDefaults() throws { |
| 28 | + try startFromRN(options: [ |
| 29 | + "dsn": "https://[email protected]/123456" |
| 30 | + ]) |
| 31 | + |
| 32 | + let actualOptions = PrivateSentrySDKOnly.options |
| 33 | + assertReactDefaults(actualOptions) |
| 34 | + } |
| 35 | + |
| 36 | + func testStartEnablesHybridTracing() throws { |
| 37 | + let testCases: [() throws -> Void] = [ |
| 38 | + { |
| 39 | + RNSentrySDK.start { options in |
| 40 | + options .dsn = "https://[email protected]/123456" |
| 41 | + } |
| 42 | + }, |
| 43 | + { |
| 44 | + try self.startFromRN(options: [ |
| 45 | + "dsn": "https://[email protected]/123456" |
| 46 | + ]) |
| 47 | + }, |
| 48 | + { |
| 49 | + RNSentrySDK.start { options in |
| 50 | + options .dsn = "https://[email protected]/123456" |
| 51 | + options.enableAutoPerformanceTracing = true |
| 52 | + } |
| 53 | + }, |
| 54 | + { |
| 55 | + try self.startFromRN(options: [ |
| 56 | + "dsn": "https://[email protected]/123456", |
| 57 | + "enableAutoPerformanceTracing": true |
| 58 | + ]) |
| 59 | + } |
| 60 | + ] |
| 61 | + |
| 62 | + // Test each implementation |
| 63 | + for startMethod in testCases { |
| 64 | + try startMethod() |
| 65 | + |
| 66 | + let actualOptions = PrivateSentrySDKOnly.options |
| 67 | + |
| 68 | + XCTAssertTrue(PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode) |
| 69 | + XCTAssertTrue(PrivateSentrySDKOnly.framesTrackingMeasurementHybridSDKMode) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func testStartDisablesHybridTracing() throws { |
| 74 | + let testCases: [() throws -> Void] = [ |
| 75 | + { |
| 76 | + RNSentrySDK.start { options in |
| 77 | + options .dsn = "https://[email protected]/123456" |
| 78 | + options.enableAutoPerformanceTracing = false |
| 79 | + } |
| 80 | + }, |
| 81 | + { |
| 82 | + try self.startFromRN(options: [ |
| 83 | + "dsn": "https://[email protected]/123456", |
| 84 | + "enableAutoPerformanceTracing": false |
| 85 | + ]) |
| 86 | + } |
| 87 | + ] |
| 88 | + |
| 89 | + for startMethod in testCases { |
| 90 | + try startMethod() |
| 91 | + |
| 92 | + let actualOptions = PrivateSentrySDKOnly.options |
| 93 | + |
| 94 | + XCTAssertFalse(PrivateSentrySDKOnly.appStartMeasurementHybridSDKMode) |
| 95 | + XCTAssertFalse(PrivateSentrySDKOnly.framesTrackingMeasurementHybridSDKMode) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + func testStartIgnoresUnhandledJsExceptions() throws { |
| 100 | + let testCases: [() throws -> Void] = [ |
| 101 | + { |
| 102 | + RNSentrySDK.start { options in |
| 103 | + options .dsn = "https://[email protected]/123456" |
| 104 | + } |
| 105 | + }, |
| 106 | + { |
| 107 | + try self.startFromRN(options: [ |
| 108 | + "dsn": "https://[email protected]/123456" |
| 109 | + ]) |
| 110 | + } |
| 111 | + ] |
| 112 | + |
| 113 | + for startMethod in testCases { |
| 114 | + try startMethod() |
| 115 | + |
| 116 | + let actualOptions = PrivateSentrySDKOnly.options |
| 117 | + |
| 118 | + let actualEvent = actualOptions.beforeSend!(createUnhandledJsExceptionEvent()) |
| 119 | + |
| 120 | + XCTAssertNil(actualEvent) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + func testStartSetsNativeEventOrigin() throws { |
| 125 | + let testCases: [() throws -> Void] = [ |
| 126 | + { |
| 127 | + RNSentrySDK.start { options in |
| 128 | + options .dsn = "https://[email protected]/123456" |
| 129 | + } |
| 130 | + }, |
| 131 | + { |
| 132 | + try self.startFromRN(options: [ |
| 133 | + "dsn": "https://[email protected]/123456" |
| 134 | + ]) |
| 135 | + } |
| 136 | + ] |
| 137 | + |
| 138 | + for startMethod in testCases { |
| 139 | + try startMethod() |
| 140 | + |
| 141 | + let actualOptions = PrivateSentrySDKOnly.options |
| 142 | + |
| 143 | + let actualEvent = actualOptions.beforeSend!(createNativeEvent()) |
| 144 | + |
| 145 | + XCTAssertNotNil(actualEvent) |
| 146 | + XCTAssertNotNil(actualEvent!.tags) |
| 147 | + XCTAssertEqual(actualEvent!.tags!["event.origin"], "ios") |
| 148 | + XCTAssertEqual(actualEvent!.tags!["event.environment"], "native") |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + func testStartDoesNotOverwriteUserBeforeSend() { |
| 153 | + var executed = false |
| 154 | + |
| 155 | + RNSentrySDK.start { options in |
| 156 | + options .dsn = "https://[email protected]/123456" |
| 157 | + options.beforeSend = { event in |
| 158 | + executed = true |
| 159 | + return event |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + PrivateSentrySDKOnly.options.beforeSend!(genericEvent()) |
| 164 | + |
| 165 | + XCTAssertTrue(executed) |
| 166 | + } |
| 167 | + |
| 168 | + func testStartSetsHybridSdkName() throws { |
| 169 | + let testCases: [() throws -> Void] = [ |
| 170 | + { |
| 171 | + RNSentrySDK.start { options in |
| 172 | + options .dsn = "https://[email protected]/123456" |
| 173 | + } |
| 174 | + }, |
| 175 | + { |
| 176 | + try self.startFromRN(options: [ |
| 177 | + "dsn": "https://[email protected]/123456" |
| 178 | + ]) |
| 179 | + } |
| 180 | + ] |
| 181 | + |
| 182 | + for startMethod in testCases { |
| 183 | + try startMethod() |
| 184 | + |
| 185 | + let actualEvent = captuteTestEvent() |
| 186 | + |
| 187 | + XCTAssertNotNil(actualEvent) |
| 188 | + XCTAssertNotNil(actualEvent!.sdk) |
| 189 | + XCTAssertEqual(actualEvent!.sdk!["name"] as! String, NATIVE_SDK_NAME) |
| 190 | + |
| 191 | + let packages = actualEvent!.sdk!["packages"] as! [[String: String]] |
| 192 | + let reactPackage = packages.first { $0["name"] == REACT_NATIVE_SDK_PACKAGE_NAME } |
| 193 | + |
| 194 | + XCTAssertNotNil(reactPackage) |
| 195 | + XCTAssertEqual(reactPackage!["name"], REACT_NATIVE_SDK_PACKAGE_NAME) |
| 196 | + XCTAssertEqual(reactPackage!["version"], REACT_NATIVE_SDK_PACKAGE_VERSION) |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + func startFromRN(options: [String: Any]) throws { |
| 201 | + var error: NSError? |
| 202 | + RNSentryStart.start(options: options, error: &error) |
| 203 | + |
| 204 | + if let error = error { |
| 205 | + throw error |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + func createUnhandledJsExceptionEvent() -> Event { |
| 210 | + let event = Event() |
| 211 | + event.exceptions = [] |
| 212 | + event.exceptions!.append(Exception(value: "Test", type: "Unhandled JS Exception: undefined is not a function")) |
| 213 | + return event |
| 214 | + } |
| 215 | + |
| 216 | + func createNativeEvent() -> Event { |
| 217 | + let event = Event() |
| 218 | + event.sdk = [ |
| 219 | + "name": NATIVE_SDK_NAME, |
| 220 | + "version": "1.2.3" |
| 221 | + ] |
| 222 | + return event |
| 223 | + } |
| 224 | + |
| 225 | + func genericEvent() -> Event { |
| 226 | + return Event() |
| 227 | + } |
| 228 | + |
| 229 | + func captuteTestEvent() -> Event? { |
| 230 | + var actualEvent: Event? |
| 231 | + |
| 232 | + // This is the closest to the sent event we can get using the actual Sentry start method |
| 233 | + let originalBeforeSend = PrivateSentrySDKOnly.options.beforeSend |
| 234 | + PrivateSentrySDKOnly.options.beforeSend = { event in |
| 235 | + if let originalBeforeSend = originalBeforeSend { |
| 236 | + let processedEvent = originalBeforeSend(event) |
| 237 | + actualEvent = processedEvent |
| 238 | + return processedEvent |
| 239 | + } |
| 240 | + actualEvent = event |
| 241 | + return event |
| 242 | + } |
| 243 | + |
| 244 | + SentrySDK.capture(message: "Test") |
| 245 | + |
| 246 | + return actualEvent |
| 247 | + } |
| 248 | +} |
0 commit comments