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 --- 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 ++++--- 4 files changed, 15 insertions(+), 17 deletions(-) delete mode 100644 sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/test/TestUtils.kt (limited to 'sw-ui') 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