Skip to content

Commit

Permalink
remove duplicated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Sep 15, 2023
1 parent ec942ec commit f550815
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ package io.customer.messaginginapp

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.commontest.BaseIntegrationTest
import io.customer.messaginginapp.di.inAppMessaging
import io.customer.messaginginapp.provider.InAppMessagesProvider
import io.customer.messaginginapp.type.InAppEventListener
import io.customer.sdk.CustomerIO
import io.customer.sdk.CustomerIOConfig
import io.customer.sdk.data.model.Region
import io.customer.sdk.extensions.random
import io.customer.sdk.hooks.HookModule
import io.customer.sdk.hooks.HooksManager
import io.customer.sdk.module.CustomerIOModule
import io.customer.sdk.repository.preference.SitePreferenceRepository
import java.lang.reflect.Field
import org.amshove.kluent.shouldBe
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -44,6 +51,27 @@ internal class ModuleMessagingInAppTest : BaseIntegrationTest() {
modules[ModuleMessagingInApp.moduleName] = module
}

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

// verify gist is initialized
verify(gistInAppMessagesProvider).initProvider(
any(),
eq(cioConfig.siteId),
eq(cioConfig.region.code)
)

// verify hook was added
verify(hooksManager).add(eq(HookModule.MessagingInApp), any())

// verify events
verify(gistInAppMessagesProvider).subscribeToEvents(any(), any(), any())

// verify given event listener gets registered
verify(gistInAppMessagesProvider).setListener(eventListenerMock)
}

@Test
fun initialize_givenProfilePreviouslyIdentified_expectGistToSetUserToken() {
prefRepository.saveIdentifier("identifier")
Expand All @@ -60,4 +88,59 @@ internal class ModuleMessagingInAppTest : BaseIntegrationTest() {
// 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(cioConfig.siteId),
eq(cioConfig.region.code)
)

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

@Test
fun initialize_givenComponentInitializedWithOrganizationId_expectOrganizationIdToBeIgnored() {
// since `organizationId` is a private member, to check if its being used we have to use reflection
// this test will be deleted when the deprecated variable is removed

val orgId = String.random
val module = ModuleMessagingInApp(
organizationId = orgId
)
val fields = ModuleMessagingInApp::class.java.declaredFields
var organizationId: Field? = null
for (field in fields) {
if (field.name == "organizationId") {
organizationId = field
break
}
}
organizationId?.isAccessible = true
(organizationId?.get(module)) shouldBe null
}

@Test
fun whenDismissMessageCalledOnCustomerIO_thenDismissMessageIsCalledOnGist() {
// initialize the SDK
val customerIO = CustomerIO.Builder(
siteId = siteId,
apiKey = String.random,
region = Region.US,
appContext = application
).apply {
overrideDiGraph = di
}.build()

// call dismissMessage on the CustomerIO instance
customerIO.inAppMessaging().dismissMessage()

// verify that the module's dismissMessage method was called
verify(gistInAppMessagesProvider).dismissMessage()
}
}

This file was deleted.

57 changes: 57 additions & 0 deletions sdk/src/androidTest/java/io/customer/sdk/CustomerIOSharedTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,48 @@ package io.customer.sdk

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.commontest.BaseIntegrationTest
import io.customer.sdk.di.CustomerIOSharedComponent
import io.customer.sdk.di.CustomerIOStaticComponent
import io.customer.sdk.repository.preference.CustomerIOStoredValues
import io.customer.sdk.repository.preference.SharedPreferenceRepository
import io.customer.sdk.util.LogcatLogger
import io.customer.sdk.util.StaticSettingsProvider
import org.amshove.kluent.shouldBeEqualTo
import org.amshove.kluent.shouldNotBe
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@RunWith(AndroidJUnit4::class)
class CustomerIOSharedTest : BaseIntegrationTest() {
@Test
fun verifyInstanceAccessedMultipleTimes_givenNoSpecialCondition_expectSameInstance() {
val instance1 = CustomerIOShared.instance()
val instance2 = CustomerIOShared.instance()

instance1 shouldBeEqualTo instance2
}

@Test
fun verifySharedComponentInstanceAccessedMultipleTimes_givenNoSpecialCondition_expectSameInstance() {
val instance1 = CustomerIOShared.instance().initializeAndGetSharedComponent(context)
val instance2 = CustomerIOShared.instance().initializeAndGetSharedComponent(context)

instance1 shouldBeEqualTo instance2
}

@Test
fun verifyDIGraphProvided_givenInstanceNotInitializedBefore_expectProvidedDIGraph() {
val instance = with(CustomerIOShared) {
clearInstance()
createInstance(diStaticGraph = staticDIComponent)
}

instance.diStaticGraph shouldBeEqualTo staticDIComponent
}

@Test
fun verifyAttachedWithSDK_givenNoSpecificEnvironment_expectProvidedLogLevel() {
val diGraph = CustomerIOStaticComponent()
Expand All @@ -26,4 +57,30 @@ class CustomerIOSharedTest : BaseIntegrationTest() {

(instance.diStaticGraph.logger as LogcatLogger).logLevel shouldBeEqualTo cioConfig.logLevel
}

@Test
fun verifyAttachedWithSDK_givenNoSpecificEnvironment_expectSharedComponentToBeInitialized() {
val diGraph = CustomerIOStaticComponent()

val instance = CustomerIOShared.createInstance(diStaticGraph = diGraph)
instance.attachSDKConfig(sdkConfig = cioConfig, context = context)

val sharedGraph = instance.diSharedGraph
sharedGraph shouldNotBe null
}

@Test
fun verifyAttachedWithSDK_givenSharedComponentIsInitialized_expectConfigValuesToBeStored() {
val diGraph = CustomerIOStaticComponent()
val diIOSharedComponent = CustomerIOSharedComponent(context)

val sharedPreferenceRepository: SharedPreferenceRepository = mock()
diIOSharedComponent.overrideDependency(SharedPreferenceRepository::class.java, sharedPreferenceRepository)

val instance = CustomerIOShared.createInstance(diStaticGraph = diGraph)
instance.diSharedGraph = diIOSharedComponent
instance.attachSDKConfig(sdkConfig = cioConfig, context = context)

verify(sharedPreferenceRepository).saveSettings(CustomerIOStoredValues(cioConfig))
}
}
Loading

0 comments on commit f550815

Please sign in to comment.