Skip to content

Commit 829c006

Browse files
committed
add unit test
1 parent 98d8362 commit 829c006

File tree

2 files changed

+56
-1
lines changed
  • analytics-kotlin-live/src

2 files changed

+56
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.segment.analytics.liveplugins.kotlin
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import com.segment.analytics.liveplugins.kotlin.utils.MemorySharedPreferences
5+
import com.segment.analytics.substrata.kotlin.JSScope
6+
import kotlinx.coroutines.ExperimentalCoroutinesApi
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Assert.assertNull
9+
import org.junit.Before
10+
import org.junit.Test
11+
import org.junit.runner.RunWith
12+
13+
14+
@OptIn(ExperimentalCoroutinesApi::class)
15+
@RunWith(AndroidJUnit4::class)
16+
class JSStorageTest {
17+
18+
private lateinit var engine: JSScope
19+
private lateinit var jsStorage: JSStorage
20+
private var exceptionThrown: Throwable? = null
21+
22+
@Before
23+
fun setUp() {
24+
exceptionThrown = null
25+
26+
engine = JSScope{ exception ->
27+
exceptionThrown = exception
28+
}
29+
jsStorage = JSStorage(MemorySharedPreferences(), engine)
30+
// Setup the engine similar to LivePlugins.configureEngine
31+
engine.sync {
32+
export(jsStorage, "Storage", "storage")
33+
}
34+
}
35+
36+
@Test
37+
fun testJSStorageWithInt() {
38+
// set from js
39+
var value = engine.await {
40+
evaluate("""storage.setValue("int", 1)""")
41+
evaluate("""storage.getValue("int")""")
42+
}
43+
assertNull(exceptionThrown)
44+
assertEquals(1, value)
45+
assertEquals(1, jsStorage.getValue("int"))
46+
47+
// set from native
48+
jsStorage.setValue("int", 2)
49+
value = engine.await {
50+
evaluate("""storage.getValue("int")""")
51+
}
52+
assertEquals(2, value)
53+
assertEquals(2, jsStorage.getValue("int"))
54+
}
55+
}

analytics-kotlin-live/src/main/java/com/segment/analytics/liveplugins/kotlin/JSStorage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class JSStorage {
6565
}
6666
}
6767

68-
private fun save(key: String, value: Any, type: String) {
68+
private inline fun <reified T> save(key: String, value: T, type: String) {
6969
val jsonObject = buildJsonObject {
7070
put(PROP_TYPE, type)
7171
put(PROP_VALUE, Json.encodeToString(value))

0 commit comments

Comments
 (0)