Skip to content

Commit b559b99

Browse files
author
Joey Heck
committed
setup unit testing for javascript in database
1 parent 8d28273 commit b559b99

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

firebase-database/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ version = project.property("firebase-database.version") as String
1010
plugins {
1111
id("com.android.library")
1212
kotlin("multiplatform")
13+
kotlin("plugin.serialization") version "1.5.31"
1314
}
1415

1516
repositories {
@@ -22,11 +23,17 @@ android {
2223
defaultConfig {
2324
minSdk = property("minSdkVersion") as Int
2425
targetSdk = property("targetSdkVersion") as Int
26+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
27+
multiDexEnabled = true
2528
}
2629
sourceSets {
2730
getByName("main") {
2831
manifest.srcFile("src/androidMain/AndroidManifest.xml")
2932
}
33+
getByName("androidTest"){
34+
java.srcDir(file("src/androidAndroidTest/kotlin"))
35+
manifest.srcFile("src/androidAndroidTest/AndroidManifest.xml")
36+
}
3037
}
3138
testOptions {
3239
unitTests.apply {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:JvmName("tests")
6+
package dev.gitlive.firebase.database
7+
8+
import androidx.test.platform.app.InstrumentationRegistry
9+
import kotlinx.coroutines.runBlocking
10+
11+
actual val emulatorHost: String = "10.0.2.2"
12+
13+
actual val context: Any = InstrumentationRegistry.getInstrumentation().targetContext
14+
15+
actual fun runTest(test: suspend () -> Unit) = runBlocking { test() }
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import dev.gitlive.firebase.*
8+
import kotlinx.coroutines.flow.first
9+
import kotlinx.serialization.*
10+
import kotlin.test.*
11+
12+
expect val emulatorHost: String
13+
expect val context: Any
14+
expect fun runTest(test: suspend () -> Unit)
15+
16+
class database {
17+
18+
@BeforeTest
19+
fun initializeFirebase() {
20+
Firebase
21+
.takeIf { Firebase.apps(context).isEmpty() }
22+
?.apply {
23+
initialize(
24+
context,
25+
FirebaseOptions(
26+
applicationId = "1:846484016111:ios:dd1f6688bad7af768c841a",
27+
apiKey = "AIzaSyCK87dcMFhzCz_kJVs2cT2AVlqOTLuyWV0",
28+
databaseUrl = "https://fir-kotlin-sdk.firebaseio.com",
29+
storageBucket = "fir-kotlin-sdk.appspot.com",
30+
projectId = "fir-kotlin-sdk",
31+
gcmSenderId = "846484016111"
32+
)
33+
)
34+
Firebase.database.useEmulator(emulatorHost, 8080)
35+
}
36+
}
37+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import kotlinx.coroutines.*
8+
import platform.Foundation.*
9+
10+
actual val emulatorHost: String = "localhost"
11+
12+
actual val context: Any = Unit
13+
14+
actual fun runTest(test: suspend () -> Unit) = runBlocking {
15+
val testRun = MainScope().async { test() }
16+
while (testRun.isActive) {
17+
NSRunLoop.mainRunLoop.runMode(
18+
NSDefaultRunLoopMode,
19+
beforeDate = NSDate.create(timeInterval = 1.0, sinceDate = NSDate())
20+
)
21+
yield()
22+
}
23+
testRun.await()
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.gitlive.firebase.database
6+
7+
import kotlinx.coroutines.GlobalScope
8+
import kotlinx.coroutines.promise
9+
10+
actual val emulatorHost: String = "localhost"
11+
12+
actual val context: Any = Unit
13+
14+
actual fun runTest(test: suspend () -> Unit) = GlobalScope
15+
.promise {
16+
try {
17+
test()
18+
} catch (e: Throwable) {
19+
e.log()
20+
throw e
21+
}
22+
}.asDynamic()
23+
24+
internal fun Throwable.log() {
25+
console.error(this)
26+
cause?.let {
27+
console.error("Caused by:")
28+
it.log()
29+
}
30+
}
31+

0 commit comments

Comments
 (0)