Skip to content

Commit 1b29e16

Browse files
ajaysubraevan-masseau
authored andcommitted
Using token endpoint for profile requests when a push token is present in SDK state (#168)
* making token request if token is present * minor refactor * fixed tests * added some tests * removed push state when resetting * updated tests * fixed side effects tests * removed unused comment * updated readme * finally fixed tests * Fix some setup/teardown in tests to take care of isolation issues. (#169) Co-authored-by: Evan Masseau <> --------- Co-authored-by: Evan C Masseau <5167687+evan-masseau@users.noreply.github.com>
1 parent 8395cec commit 1b29e16

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

sdk/analytics/src/test/java/com/klaviyo/analytics/state/SideEffectTests.kt

+45-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import io.mockk.every
1515
import io.mockk.mockk
1616
import io.mockk.slot
1717
import io.mockk.verify
18+
import org.junit.After
1819
import org.junit.Assert.assertFalse
1920
import org.junit.Assert.assertNull
21+
import org.junit.Before
2022
import org.junit.Test
2123

2224
class SideEffectTests : BaseTest() {
@@ -27,7 +29,6 @@ class SideEffectTests : BaseTest() {
2729
private val capturedStateObserver = slot<StateObserver>()
2830
private val capturedPushState = slot<String?>()
2931
private val apiClientMock: ApiClient = mockk<ApiClient>().apply {
30-
Registry.register<ApiClient>(this)
3132
every { onApiRequest(any(), capture(capturedApiObserver)) } returns Unit
3233
every { enqueueProfile(capture(capturedProfile)) } returns Unit
3334
every { enqueueEvent(any(), any()) } returns Unit
@@ -39,6 +40,19 @@ class SideEffectTests : BaseTest() {
3940
every { pushState = captureNullable(capturedPushState) } returns Unit
4041
every { getAsProfile(withAttributes = any()) } returns profile
4142
every { resetAttributes() } returns Unit
43+
every { pushToken } returns null
44+
}
45+
46+
@Before
47+
override fun setup() {
48+
super.setup()
49+
Registry.register<ApiClient>(apiClientMock)
50+
}
51+
52+
@After
53+
override fun cleanup() {
54+
Registry.unregister<ApiClient>()
55+
super.cleanup()
4256
}
4357

4458
@Test
@@ -86,7 +100,7 @@ class SideEffectTests : BaseTest() {
86100
}
87101

88102
@Test
89-
fun `Resetting profile enqueues API call immediately`() {
103+
fun `Resetting profile enqueues Profiles API call immediately`() {
90104
StateSideEffects(
91105
stateMock.apply {
92106
every { getAsProfile(withAttributes = any()) } returns Profile(
@@ -116,6 +130,35 @@ class SideEffectTests : BaseTest() {
116130
verify(exactly = 2) { apiClientMock.enqueueProfile(any()) }
117131
}
118132

133+
@Test
134+
fun `Resetting profile enqueues Push Token API call immediately when push token is in state`() {
135+
every { stateMock.pushToken } returns PUSH_TOKEN
136+
137+
StateSideEffects(
138+
stateMock.apply {
139+
every { getAsProfile(withAttributes = any()) } returns Profile(
140+
properties = mapOf(
141+
ProfileKey.ANONYMOUS_ID to ANON_ID,
142+
ProfileKey.FIRST_NAME to "Kermit"
143+
)
144+
)
145+
},
146+
apiClientMock
147+
)
148+
149+
capturedStateObserver.captured(PROFILE_ATTRIBUTES, null)
150+
151+
every { stateMock.getAsProfile(withAttributes = any()) } returns Profile(
152+
properties = mapOf(
153+
ProfileKey.ANONYMOUS_ID to "new_anon_id"
154+
)
155+
)
156+
157+
capturedStateObserver.captured(null, null)
158+
159+
verify(exactly = 1) { apiClientMock.enqueuePushToken(PUSH_TOKEN, any()) }
160+
}
161+
119162
@Test
120163
fun `Attributes do enqueue a profile API request`() {
121164
StateSideEffects(stateMock, apiClientMock)

0 commit comments

Comments
 (0)