Move unit tests to JVM with Robolectric

This commit is contained in:
William Brawner 2022-11-16 17:46:19 -07:00
parent d1b934dea1
commit 3f3b30f2b4
6 changed files with 37 additions and 11 deletions

22
.github/workflows/tests.yml vendored Normal file
View file

@ -0,0 +1,22 @@
name: Tests
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew assemblePlayDebug
- name: Run unit tests
run: ./gradlew testPlayDebugUnitTest

View file

@ -24,6 +24,9 @@ android {
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
unitTests {
includeAndroidResources = true
}
}
buildTypes {
release {
@ -56,6 +59,7 @@ dependencies {
def okhttp_version = "4.10.0"
implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp_version"
testImplementation 'org.robolectric:robolectric:4.9'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

View file

@ -12,8 +12,8 @@ import java.util.concurrent.atomic.AtomicReference
* must ensure that [init] is called prior to sending events.
*/
object Plausible {
internal val client: AtomicReference<PlausibleClient?> = AtomicReference(null)
internal val config: AtomicReference<PlausibleConfig?> = AtomicReference(null)
private val client: AtomicReference<PlausibleClient?> = AtomicReference(null)
private val config: AtomicReference<PlausibleConfig?> = AtomicReference(null)
fun init(context: Context) {
val config = AndroidResourcePlausibleConfig(context)

View file

@ -1,15 +1,15 @@
package com.wbrawner.plausible.android
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.File
@RunWith(AndroidJUnit4::class)
@RunWith(RobolectricTestRunner::class)
internal class NetworkFirstPlausibleClientTest {
private lateinit var client: NetworkFirstPlausibleClient
@ -27,7 +27,7 @@ internal class NetworkFirstPlausibleClientTest {
@Ignore("Not yet implemented")
@Test
fun events_are_sent_to_server() = networkFirstClientTest {
fun `events are sent to server`() = networkFirstClientTest {
client.event(
domain = "test.example.com",
name = "eventUrl",

View file

@ -1,16 +1,16 @@
package com.wbrawner.plausible.android
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.wbrawner.plausible.android.fake.FakePlausibleClient
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.io.File
internal const val SCREEN_WIDTH = 123
@RunWith(AndroidJUnit4::class)
@RunWith(RobolectricTestRunner::class)
internal class PlausibleTest {
lateinit var client: FakePlausibleClient
lateinit var config: PlausibleConfig
@ -27,14 +27,14 @@ internal class PlausibleTest {
}
@Test
fun enable_is_sent_to_config() {
fun `enable is set on config via Plausible`() {
assertTrue(config.enable)
Plausible.enable(false)
assertFalse(config.enable)
}
@Test
fun user_agent_is_sent_to_config() {
fun `user agent is set on config via Plausible`() {
val oldUserAgent = config.userAgent
Plausible.setUserAgent("test user agent")
assertNotEquals(oldUserAgent, config.userAgent)
@ -42,7 +42,7 @@ internal class PlausibleTest {
}
@Test
fun events_are_sent_to_client() {
fun `events are sent to client`() {
config.domain = "test.example.com"
Plausible.event("eventName", "eventUrl", "referrer", mapOf("prop1" to "propVal"))
assertEquals(1, client.events.size)
@ -50,7 +50,7 @@ internal class PlausibleTest {
assertEquals("test.example.com", event.domain)
assertEquals("eventName", event.name)
assertEquals("app://localhost/eventUrl", event.url)
assertEquals(123, event.screenWidth)
assertEquals(SCREEN_WIDTH, event.screenWidth)
assertEquals("referrer", event.referrer)
assertEquals("{\"prop1\":\"propVal\"}", event.props)
}