package com.contentsquare.rn.csq import com.contentsquare.CSQ import com.contentsquare.rn.utils.ReactNativeUiThreadUtil import com.facebook.react.bridge.ReactApplicationContext import io.mockk.every import io.mockk.mockk import io.mockk.mockkStatic import io.mockk.slot import io.mockk.unmockkAll import io.mockk.verify import org.junit.After import org.junit.Before import org.junit.Test class CSQPrivacyTest { private val mockReactContext: ReactApplicationContext = mockk() private val mockReactNativeUiThreadUtil: ReactNativeUiThreadUtil = mockk() private lateinit var underTest: CSQPrivacy @Before fun setup() { mockkStatic(CSQ::class) val slot = slot() every { mockReactNativeUiThreadUtil.runOnUiThread(capture(slot)) } answers { slot.captured.run() Unit } underTest = CSQPrivacy(mockReactContext, mockReactNativeUiThreadUtil) } @After fun tearDown() { unmockkAll() } @Test fun `optIn should execute CSQ optIn with context on UI thread`() { // Given every { CSQ.optIn(any()) } returns Unit // When underTest.optIn() // Then verify(exactly = 1) { CSQ.optIn(mockReactContext) } verify(exactly = 1) { mockReactNativeUiThreadUtil.runOnUiThread(any()) } } @Test fun `optOut should execute CSQ optOut on UI thread`() { // Given every { CSQ.optOut() } returns Unit // When underTest.optOut() // Then verify(exactly = 1) { CSQ.optOut() } verify(exactly = 1) { mockReactNativeUiThreadUtil.runOnUiThread(any()) } } @Test fun `identify should execute CSQ identify with proper identity on UI thread`() { // Given val mockIdentity = "test_identity" every { CSQ.identify(mockIdentity) } returns Unit // When underTest.identify(mockIdentity) // Then verify(exactly = 1) { CSQ.identify(mockIdentity) } verify(exactly = 1) { mockReactNativeUiThreadUtil.runOnUiThread(any()) } } @Test fun `resetIdentity should execute CSQ resetIdentity on UI thread`() { // Given every { CSQ.resetIdentity() } returns Unit // When underTest.resetIdentity() // Then verify(exactly = 1) { CSQ.resetIdentity() } verify(exactly = 1) { mockReactNativeUiThreadUtil.runOnUiThread(any()) } } @Test fun `sendUserIdentifier should execute CSQ sendUserIdentifier with proper user identifier on UI thread`() { // Given val mockUserIdentifier = "test_user_identifier" every { CSQ.sendUserIdentifier(mockUserIdentifier) } returns Unit // When underTest.sendUserIdentifier(mockUserIdentifier) // Then verify(exactly = 1) { CSQ.sendUserIdentifier(mockUserIdentifier) } verify(exactly = 1) { mockReactNativeUiThreadUtil.runOnUiThread(any()) } } }