|
| 1 | +package io.sentry.react.expo |
| 2 | + |
| 3 | +import io.sentry.Sentry |
| 4 | +import io.sentry.exception.ExceptionMechanismException |
| 5 | +import org.junit.After |
| 6 | +import org.junit.Assert.assertEquals |
| 7 | +import org.junit.Assert.assertFalse |
| 8 | +import org.junit.Assert.assertNotNull |
| 9 | +import org.junit.Assert.assertTrue |
| 10 | +import org.junit.Test |
| 11 | +import org.junit.runner.RunWith |
| 12 | +import org.junit.runners.JUnit4 |
| 13 | +import org.mockito.MockedStatic |
| 14 | +import org.mockito.Mockito.mockStatic |
| 15 | +import org.mockito.kotlin.any |
| 16 | +import org.mockito.kotlin.argumentCaptor |
| 17 | +import org.mockito.kotlin.never |
| 18 | +import org.mockito.kotlin.verify |
| 19 | + |
| 20 | +@RunWith(JUnit4::class) |
| 21 | +class SentryReactNativeHostHandlerTest { |
| 22 | + private var sentryMock: MockedStatic<Sentry>? = null |
| 23 | + |
| 24 | + @After |
| 25 | + fun tearDown() { |
| 26 | + sentryMock?.close() |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `does not capture when in developer support mode`() { |
| 31 | + sentryMock = |
| 32 | + mockStatic(Sentry::class.java).also { |
| 33 | + it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) |
| 34 | + } |
| 35 | + |
| 36 | + val handler = SentryReactNativeHostHandler() |
| 37 | + handler.onReactInstanceException(true, RuntimeException("test")) |
| 38 | + |
| 39 | + sentryMock!!.verify({ Sentry.captureException(any()) }, never()) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `does not capture when sentry is not enabled`() { |
| 44 | + sentryMock = |
| 45 | + mockStatic(Sentry::class.java).also { |
| 46 | + it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(false) |
| 47 | + } |
| 48 | + |
| 49 | + val handler = SentryReactNativeHostHandler() |
| 50 | + handler.onReactInstanceException(false, RuntimeException("test")) |
| 51 | + |
| 52 | + sentryMock!!.verify({ Sentry.captureException(any()) }, never()) |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `captures exception with unhandled mechanism when sentry is enabled`() { |
| 57 | + sentryMock = |
| 58 | + mockStatic(Sentry::class.java).also { |
| 59 | + it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) |
| 60 | + } |
| 61 | + |
| 62 | + val handler = SentryReactNativeHostHandler() |
| 63 | + val originalException = IllegalStateException("Fabric crash") |
| 64 | + |
| 65 | + handler.onReactInstanceException(false, originalException) |
| 66 | + |
| 67 | + val captor = argumentCaptor<Throwable>() |
| 68 | + sentryMock!!.verify { Sentry.captureException(captor.capture()) } |
| 69 | + |
| 70 | + val captured = captor.firstValue |
| 71 | + assertTrue( |
| 72 | + "Expected ExceptionMechanismException but got ${captured::class.java}", |
| 73 | + captured is ExceptionMechanismException, |
| 74 | + ) |
| 75 | + |
| 76 | + val mechanismException = captured as ExceptionMechanismException |
| 77 | + val mechanism = mechanismException.exceptionMechanism |
| 78 | + assertEquals("expoReactHost", mechanism.type) |
| 79 | + assertFalse("Mechanism should be unhandled", mechanism.isHandled!!) |
| 80 | + assertEquals(originalException, mechanismException.throwable) |
| 81 | + assertNotNull(mechanismException.thread) |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + fun `does not throw when sentry capture fails`() { |
| 86 | + sentryMock = |
| 87 | + mockStatic(Sentry::class.java).also { |
| 88 | + it.`when`<Boolean> { Sentry.isEnabled() }.thenReturn(true) |
| 89 | + it.`when`<Any> { Sentry.captureException(any()) }.thenThrow(RuntimeException("Sentry internal error")) |
| 90 | + } |
| 91 | + |
| 92 | + val handler = SentryReactNativeHostHandler() |
| 93 | + // Should not throw |
| 94 | + handler.onReactInstanceException(false, IllegalStateException("test")) |
| 95 | + } |
| 96 | +} |
0 commit comments