|
| 1 | +package io.sentry.react |
| 2 | + |
| 3 | +import android.content.pm.PackageInfo |
| 4 | +import android.content.pm.PackageManager |
| 5 | +import com.facebook.react.bridge.Arguments |
| 6 | +import com.facebook.react.bridge.Promise |
| 7 | +import com.facebook.react.bridge.ReactApplicationContext |
| 8 | +import com.facebook.react.bridge.WritableMap |
| 9 | +import io.sentry.ILogger |
| 10 | +import io.sentry.SentryLevel |
| 11 | +import org.junit.After |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Before |
| 14 | +import org.junit.Test |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.junit.runners.JUnit4 |
| 17 | +import org.mockito.ArgumentCaptor |
| 18 | +import org.mockito.Captor |
| 19 | +import org.mockito.Mockito.* |
| 20 | +import org.mockito.MockitoAnnotations |
| 21 | +import org.mockito.MockedStatic |
| 22 | +import org.mockito.Mockito.mockStatic |
| 23 | +import org.mockito.kotlin.whenever |
| 24 | + |
| 25 | +@RunWith(JUnit4::class) |
| 26 | +class RNSentryModuleImplTest { |
| 27 | + |
| 28 | + private lateinit var module: RNSentryModuleImpl |
| 29 | + private lateinit var promise: Promise |
| 30 | + private lateinit var logger: ILogger |
| 31 | + private var argumentsMock: MockedStatic<Arguments>? = null |
| 32 | + |
| 33 | + @Captor |
| 34 | + private lateinit var writableMapCaptor: ArgumentCaptor<WritableMap> |
| 35 | + |
| 36 | + @Before |
| 37 | + fun setUp() { |
| 38 | + MockitoAnnotations.openMocks(this) |
| 39 | + val reactContext = mock(ReactApplicationContext::class.java) |
| 40 | + promise = mock(Promise::class.java) |
| 41 | + logger = mock(ILogger::class.java) |
| 42 | + val packageManager = mock(PackageManager::class.java) |
| 43 | + val packageInfo = mock(PackageInfo::class.java) |
| 44 | + |
| 45 | + whenever(reactContext.packageManager).thenReturn(packageManager) |
| 46 | + whenever(packageManager.getPackageInfo(anyString(), anyInt())).thenReturn(packageInfo) |
| 47 | + |
| 48 | + module = RNSentryModuleImpl(reactContext) |
| 49 | + |
| 50 | + // Mock the Arguments class |
| 51 | + argumentsMock = mockStatic(Arguments::class.java) |
| 52 | + val writableMap = mock(WritableMap::class.java) |
| 53 | + whenever(Arguments.createMap()).thenReturn(writableMap) |
| 54 | + } |
| 55 | + |
| 56 | + @After |
| 57 | + fun tearDown() { |
| 58 | + argumentsMock?.close() |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `fetchNativeAppStart resolves promise with null when app is not launched in the foreground`() { |
| 63 | + // Mock the app start measurement |
| 64 | + val appStartMeasurement = mapOf<String, Any>() |
| 65 | + |
| 66 | + // Call the method |
| 67 | + module.fetchNativeAppStart(promise, appStartMeasurement, logger, false) |
| 68 | + |
| 69 | + // Verify a warning log is emitted |
| 70 | + verify(logger, org.mockito.kotlin.times(1)).log( |
| 71 | + SentryLevel.WARNING, |
| 72 | + "Invalid app start data: app not launched in foreground." |
| 73 | + ) |
| 74 | + |
| 75 | + // Verify the promise is resolved with null |
| 76 | + verify(promise).resolve(null) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun `fetchNativeAppStart resolves promise with app start data when app is launched in the foreground`() { |
| 81 | + // Mock the app start measurement |
| 82 | + val appStartMeasurement = mapOf<String, Any>() |
| 83 | + |
| 84 | + // Call the method |
| 85 | + module.fetchNativeAppStart(promise, appStartMeasurement, logger, true) |
| 86 | + |
| 87 | + // Verify no logs are emitted |
| 88 | + verify(logger, org.mockito.kotlin.times(0)).log(any(), any()) |
| 89 | + |
| 90 | + // Verify the promise is resolved with the expected data |
| 91 | + verify(promise).resolve(any(WritableMap::class.java)) |
| 92 | + verify(promise).resolve(writableMapCaptor.capture()) |
| 93 | + val capturedMap = writableMapCaptor.value |
| 94 | + assertEquals(false, capturedMap.getBoolean("has_fetched")) |
| 95 | + } |
| 96 | +} |
0 commit comments