-
-
Notifications
You must be signed in to change notification settings - Fork 337
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native): Set current screen on native scope (#3927)
- Loading branch information
1 parent
5290b95
commit 8a28af8
Showing
11 changed files
with
271 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...AndroidTester/app/src/test/java/io/sentry/rnsentryandroidtester/RNSentryBreadcrumbTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.sentry.rnsentryandroidtester | ||
|
||
import com.facebook.react.bridge.JavaOnlyMap | ||
import io.sentry.react.RNSentryBreadcrumb | ||
import junit.framework.TestCase.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
|
||
@RunWith(JUnit4::class) | ||
class RNSentryBreadcrumbTest { | ||
|
||
@Test | ||
fun nullForMissingCategory() { | ||
val map = JavaOnlyMap.of() | ||
val actual = RNSentryBreadcrumb.getCurrentScreenFrom(map) | ||
assertEquals(null, actual) | ||
} | ||
|
||
|
||
@Test | ||
fun nullForNonNavigationCategory() { | ||
val map = JavaOnlyMap.of( | ||
"category", "unknown" | ||
) | ||
val actual = RNSentryBreadcrumb.getCurrentScreenFrom(map) | ||
assertEquals(null, actual) | ||
} | ||
|
||
|
||
@Test | ||
fun nullForMissingData() { | ||
val map = JavaOnlyMap.of( | ||
"category", "navigation" | ||
) | ||
val actual = RNSentryBreadcrumb.getCurrentScreenFrom(map) | ||
assertEquals(null, actual) | ||
} | ||
|
||
|
||
@Test | ||
fun nullForNonStringDataToKey() { | ||
val map = JavaOnlyMap.of( | ||
"category", "unknown", | ||
"data", mapOf( | ||
"to" to 123, | ||
), | ||
) | ||
val actual = RNSentryBreadcrumb.getCurrentScreenFrom(map) | ||
assertEquals(null, actual) | ||
} | ||
|
||
@Test | ||
fun screenNameForValidNavigationBreadcrumb() { | ||
val map = JavaOnlyMap.of( | ||
"category", "navigation", | ||
"data", JavaOnlyMap.of( | ||
"to", "newScreen", | ||
), | ||
) | ||
val actual = RNSentryBreadcrumb.getCurrentScreenFrom(map) | ||
assert(actual is String) | ||
assertEquals("newScreen", actual) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryBreadcrumbTests.m
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryBreadcrumbTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import XCTest | ||
import Sentry | ||
|
||
class RNSentryBreadcrumbTests: XCTestCase { | ||
|
||
func testGeneratesSentryBreadcrumbFromNSDictionary() { | ||
let actualCrumb = RNSentryBreadcrumb.from([ | ||
"level": "error", | ||
"category": "testCategory", | ||
"type": "testType", | ||
"message": "testMessage", | ||
"data": [ | ||
"test": "data" | ||
] | ||
]) | ||
|
||
XCTAssertEqual(actualCrumb!.level, SentryLevel.error) | ||
XCTAssertEqual(actualCrumb!.category, "testCategory") | ||
XCTAssertEqual(actualCrumb!.type, "testType") | ||
XCTAssertEqual(actualCrumb!.message, "testMessage") | ||
XCTAssertEqual((actualCrumb!.data)!["test"] as! String, "data") | ||
} | ||
|
||
func testUsesInfoAsDefaultSentryLevel() { | ||
let actualCrumb = RNSentryBreadcrumb.from([ | ||
"message": "testMessage" | ||
]) | ||
|
||
XCTAssertEqual(actualCrumb!.level, SentryLevel.info) | ||
} | ||
|
||
func testNullForMissingCategory() { | ||
let map: [String: Any] = [:] | ||
let actual = RNSentryBreadcrumb.getCurrentScreen(from: map) | ||
XCTAssertNil(actual) | ||
} | ||
|
||
func testNullForNonNavigationCategory() { | ||
let map: [String: Any] = ["category": "unknown"] | ||
let actual = RNSentryBreadcrumb.getCurrentScreen(from: map) | ||
XCTAssertNil(actual) | ||
} | ||
|
||
func testNullForMissingData() { | ||
let map: [String: Any] = ["category": "navigation"] | ||
let actual = RNSentryBreadcrumb.getCurrentScreen(from: map) | ||
XCTAssertNil(actual) | ||
} | ||
|
||
func testNullForNonStringDataToKey() { | ||
let map: [String: Any] = [ | ||
"category": "unknown", | ||
"data": ["to": 123] | ||
] | ||
let actual = RNSentryBreadcrumb.getCurrentScreen(from: map) | ||
XCTAssertNil(actual) | ||
} | ||
|
||
func testScreenNameForValidNavigationBreadcrumb() { | ||
let map: [String: Any] = [ | ||
"category": "navigation", | ||
"data": ["to": "newScreen"] | ||
] | ||
let actual = RNSentryBreadcrumb.getCurrentScreen(from: map) | ||
XCTAssertEqual(actual, "newScreen") | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
RNSentryCocoaTester/RNSentryCocoaTesterTests/RNSentryCocoaTesterTests-Bridging-Header.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#import "RNSentryBreadcrumb.h" |
87 changes: 87 additions & 0 deletions
87
android/src/main/java/io/sentry/react/RNSentryBreadcrumb.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.sentry.react; | ||
|
||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.Map; | ||
|
||
import io.sentry.Breadcrumb; | ||
import io.sentry.SentryLevel; | ||
|
||
public class RNSentryBreadcrumb { | ||
|
||
@Nullable | ||
public static String getCurrentScreenFrom(ReadableMap from) { | ||
final @Nullable String maybeCategory = from.hasKey("category") ? from.getString("category") : null; | ||
if (maybeCategory == null || !maybeCategory.equals("navigation")) { | ||
return null; | ||
} | ||
|
||
final @Nullable ReadableMap maybeData = from.hasKey("data") ? from.getMap("data") : null; | ||
if (maybeData == null) { | ||
return null; | ||
} | ||
|
||
try { | ||
// getString might throw if cast to string fails (data.to is not enforced by TS to be a string) | ||
return maybeData.hasKey("to") ? maybeData.getString("to") : null; | ||
} catch (Throwable exception) { | ||
return null; | ||
} | ||
} | ||
|
||
@NotNull | ||
public static Breadcrumb fromMap(ReadableMap from) { | ||
final @NotNull Breadcrumb breadcrumb = new Breadcrumb(); | ||
|
||
if (from.hasKey("message")) { | ||
breadcrumb.setMessage(from.getString("message")); | ||
} | ||
|
||
if (from.hasKey("type")) { | ||
breadcrumb.setType(from.getString("type")); | ||
} | ||
|
||
if (from.hasKey("category")) { | ||
breadcrumb.setCategory(from.getString("category")); | ||
} | ||
|
||
if (from.hasKey("level")) { | ||
switch (from.getString("level")) { | ||
case "fatal": | ||
breadcrumb.setLevel(SentryLevel.FATAL); | ||
break; | ||
case "warning": | ||
breadcrumb.setLevel(SentryLevel.WARNING); | ||
break; | ||
case "debug": | ||
breadcrumb.setLevel(SentryLevel.DEBUG); | ||
break; | ||
case "error": | ||
breadcrumb.setLevel(SentryLevel.ERROR); | ||
break; | ||
case "info": | ||
default: | ||
breadcrumb.setLevel(SentryLevel.INFO); | ||
break; | ||
} | ||
} | ||
|
||
|
||
if (from.hasKey("data")) { | ||
final ReadableMap data = from.getMap("data"); | ||
for (final Map.Entry<String, Object> entry : data.toHashMap().entrySet()) { | ||
final Object value = entry.getValue(); | ||
// data is ConcurrentHashMap and can't have null values | ||
if (value != null) { | ||
breadcrumb.setData(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
} | ||
|
||
return breadcrumb; | ||
} | ||
|
||
} |
Oops, something went wrong.