package com.contentsquare.rn.eventEmitter import androidx.test.ext.junit.runners.AndroidJUnit4 import com.contentsquare.rn.eventEmitter.CSEventEmitterModuleImpl.CSEmitterEvents import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.modules.core.DeviceEventManagerModule import io.mockk.clearAllMocks import io.mockk.every import io.mockk.just import io.mockk.mockk import io.mockk.runs import io.mockk.verify import org.junit.After import org.junit.Test import org.junit.runner.RunWith import org.robolectric.annotation.Config @RunWith(AndroidJUnit4::class) @Config(manifest = Config.NONE) class CSEventEmitterTest { // mocks private val mockReactApplicationContext = mockk() private val mockRCTDeviceEventEmitter = mockk() private val underTest = CSEventEmitterModuleImpl(mockReactApplicationContext) @After fun tearDown() { clearAllMocks() } @Test fun `Given a listener has been added when session replay link is send then emit SESSION_REPLAY_LINK_CHANGE event`() { val testUrl = "testurl.com" every { mockReactApplicationContext.hint(DeviceEventManagerModule.RCTDeviceEventEmitter::class) .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) } returns mockRCTDeviceEventEmitter every { mockRCTDeviceEventEmitter.emit(any(), any()) } just runs underTest.addListener(CSEmitterEvents.SESSION_REPLAY_LINK_CHANGE.event) underTest.sendSessionReplayLink(testUrl) verify { mockRCTDeviceEventEmitter.emit(CSEmitterEvents.SESSION_REPLAY_LINK_CHANGE.event, testUrl) } } @Test fun `Given a listener has not been added when session replay link is send then it should not emit an event`() { val testUrl = "testurl.com" every { mockReactApplicationContext.hint(DeviceEventManagerModule.RCTDeviceEventEmitter::class) .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) } returns mockRCTDeviceEventEmitter every { mockRCTDeviceEventEmitter.emit(any(), any()) } just runs underTest.sendSessionReplayLink(testUrl) verify(exactly = 0) { mockRCTDeviceEventEmitter.emit(CSEmitterEvents.SESSION_REPLAY_LINK_CHANGE.event, testUrl) } } @Test fun `Given a listener has been added it should not emit an event not corresponding to that listener`() { val testUrl = "testurl.com" every { mockReactApplicationContext.hint(DeviceEventManagerModule.RCTDeviceEventEmitter::class) .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) } returns mockRCTDeviceEventEmitter every { mockRCTDeviceEventEmitter.emit(any(), any()) } just runs underTest.addListener("ANY_LISTENER") underTest.sendSessionReplayLink(testUrl) verify(exactly = 0) { mockRCTDeviceEventEmitter.emit(CSEmitterEvents.SESSION_REPLAY_LINK_CHANGE.event, testUrl) } } }