Skip to content

Commit

Permalink
fix: set gist user token incase identifier exists (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Jan 25, 2023
1 parent 688e4a5 commit 44cc4d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class ModuleMessagingInApp internal constructor(
private val trackRepository: TrackRepository
get() = diGraph.trackRepository

private val identifier: String?
get() = diGraph.sitePreferenceRepository.getIdentifier()

private val hooksManager: HooksManager by lazy { diGraph.hooksManager }

private val gistProvider by lazy { diGraph.gistProvider }
Expand Down Expand Up @@ -89,5 +92,9 @@ class ModuleMessagingInApp internal constructor(
application = diGraph.context.applicationContext as Application,
organizationId = organizationId
)

// if identifier is already present, set the userToken again so in case if the customer was already identified and
// module was added later on, we can notify gist about it.
identifier?.let { gistProvider.setUserToken(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import io.customer.messaginginapp.provider.InAppMessagesProvider
import io.customer.messaginginapp.type.InAppEventListener
import io.customer.sdk.hooks.HookModule
import io.customer.sdk.hooks.HooksManager
import io.customer.sdk.repository.preference.SitePreferenceRepository
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.*

@RunWith(AndroidJUnit4::class)
internal class ModuleMessagingInAppTest : BaseTest() {
Expand All @@ -21,6 +19,8 @@ internal class ModuleMessagingInAppTest : BaseTest() {
private val gistInAppMessagesProvider: InAppMessagesProvider = mock()
private val hooksManager: HooksManager = mock()
private val eventListenerMock: InAppEventListener = mock()
private val prefRepository: SitePreferenceRepository
get() = di.sitePreferenceRepository

@Before
override fun setup() {
Expand All @@ -30,7 +30,8 @@ internal class ModuleMessagingInAppTest : BaseTest() {
di.overrideDependency(HooksManager::class.java, hooksManager)

module = ModuleMessagingInApp(
moduleConfig = MessagingInAppModuleConfig.Builder().setEventListener(eventListenerMock).build(),
moduleConfig = MessagingInAppModuleConfig.Builder().setEventListener(eventListenerMock)
.build(),
overrideDiGraph = di,
organizationId = "test"
)
Expand All @@ -52,4 +53,28 @@ internal class ModuleMessagingInAppTest : BaseTest() {
// verify given event listener gets registered
verify(gistInAppMessagesProvider).setListener(eventListenerMock)
}

@Test
fun initialize_givenProfilePreviouslyIdentified_expectGistToSetUserToken() {
prefRepository.saveIdentifier("identifier")

module.initialize()

// verify gist is initialized
verify(gistInAppMessagesProvider).initProvider(any(), eq("test"))

// verify gist sets userToken
verify(gistInAppMessagesProvider).setUserToken(eq("identifier"))
}

@Test
fun initialize_givenNoProfileIdentified_expectGistNoUserSet() {
module.initialize()

// verify gist is initialized
verify(gistInAppMessagesProvider).initProvider(any(), eq("test"))

// verify gist doesn't userToken
verify(gistInAppMessagesProvider, never()).setUserToken(any())
}
}

0 comments on commit 44cc4d1

Please sign in to comment.