From 57100e24f47250acc0e872b751732faa2e3ff1ba Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Sun, 30 Apr 2023 23:20:51 +0200 Subject: Use kotlinx-coroutines-test in sw-ui --- gradle/libs.versions.toml | 1 + sw-ui/build.gradle.kts | 3 +++ .../sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt | 13 ++++++++----- .../kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt | 9 --------- .../org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt | 7 ++++--- 5 files changed, 16 insertions(+), 17 deletions(-) delete mode 100644 sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c489e60a..32c06674 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -23,6 +23,7 @@ kotlin-blueprintjs-icons = "4.13.0-8" javax-annotation-api = { module = "javax.annotation:javax.annotation-api", version.ref = "javax-annotation-api" } kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlinx-coroutines" } +kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "kotlinx-coroutines" } kotlinx-coroutines-reactor = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-reactor", version.ref = "kotlinx-coroutines" } kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } diff --git a/sw-ui/build.gradle.kts b/sw-ui/build.gradle.kts index 6fb7d8b8..bb2d0701 100644 --- a/sw-ui/build.gradle.kts +++ b/sw-ui/build.gradle.kts @@ -15,6 +15,9 @@ kotlin { dependencies { implementation(projects.swClient) + implementation(libs.kotlinx.coroutines.core) + implementation(libs.kotlinx.coroutines.test) + implementation(project.dependencies.enforcedPlatform(libs.kotlin.wrappers.bom)) implementation(libs.kotlin.wrappers.react.base) implementation(libs.kotlin.wrappers.react.dom) diff --git a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt index 7471d577..f810c8b9 100644 --- a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt +++ b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt @@ -1,7 +1,7 @@ package org.luxons.sevenwonders.ui.redux.sagas import kotlinx.coroutines.* -import org.luxons.sevenwonders.ui.test.runSuspendingTest +import kotlinx.coroutines.test.* import redux.RAction import redux.Store import redux.WrapperAction @@ -37,10 +37,11 @@ private data class TestRedux( val sagas: SagaManager, ) +@OptIn(ExperimentalCoroutinesApi::class) // for runTest class SagaContextTest { @Test - fun dispatch() = runSuspendingTest { + fun dispatch_dispatchesToStore() = runTest { val redux = configureTestStore(State("initial")) redux.sagas.runSaga { @@ -51,13 +52,14 @@ class SagaContextTest { } @Test - fun next() = runSuspendingTest { + fun next_waitsForNextAction() = runTest { val redux = configureTestStore(State("initial")) val job = redux.sagas.launchSaga(this) { val action = next() dispatch(UpdateData("effect-${action.data}")) } + advanceUntilIdle() // make sure the saga is launched assertEquals(State("initial"), redux.store.getState()) @@ -67,7 +69,7 @@ class SagaContextTest { } @Test - fun onEach() = runSuspendingTest { + fun onEach() = runTest { val redux = configureTestStore(State("initial")) val job = redux.sagas.launchSaga(this) { @@ -75,11 +77,12 @@ class SagaContextTest { dispatch(UpdateData("effect-${it.data}")) } } + advanceUntilIdle() // make sure the saga is launched assertEquals(State("initial"), redux.store.getState()) redux.store.dispatch(SideEffectAction("data")) - delay(50) + yield() assertEquals(State("effect-data"), redux.store.getState()) job.cancel() } diff --git a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt deleted file mode 100644 index 7e06c572..00000000 --- a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt +++ /dev/null @@ -1,9 +0,0 @@ -package org.luxons.sevenwonders.ui.test - -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.promise - -@OptIn(DelicateCoroutinesApi::class) // OK in JS tests -fun runSuspendingTest(testBody: suspend CoroutineScope.() -> Unit) = GlobalScope.promise { testBody() } diff --git a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt index c605e41c..ef8dfb62 100644 --- a/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt +++ b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt @@ -1,14 +1,15 @@ package org.luxons.sevenwonders.ui.utils -import kotlinx.coroutines.delay -import org.luxons.sevenwonders.ui.test.runSuspendingTest +import kotlinx.coroutines.* +import kotlinx.coroutines.test.* import kotlin.test.Test import kotlin.test.assertEquals class CoroutineUtilsTest { + @OptIn(ExperimentalCoroutinesApi::class) // for runTest @Test - fun awaitFirstTest() = runSuspendingTest { + fun awaitFirstTest() = runTest { val s = awaitFirst( { delay(100); "1" }, { delay(200); "2" }, -- cgit