diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-04-06 18:55:25 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-04-06 18:55:58 +0200 |
commit | d4d20533556928f63c8759437f67e76336bab55e (patch) | |
tree | 34e7bb151b5d21497665131b6ab8d875254e7666 /sw-ui/src/test/kotlin | |
parent | Refactoring in GameScene.kt (diff) | |
download | seven-wonders-d4d20533556928f63c8759437f67e76336bab55e.tar.gz seven-wonders-d4d20533556928f63c8759437f67e76336bab55e.tar.bz2 seven-wonders-d4d20533556928f63c8759437f67e76336bab55e.zip |
Delete old React/TypeScript UI
Diffstat (limited to 'sw-ui/src/test/kotlin')
-rw-r--r-- | sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt | 91 | ||||
-rw-r--r-- | sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt | 24 |
2 files changed, 115 insertions, 0 deletions
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 new file mode 100644 index 00000000..8182401f --- /dev/null +++ b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt @@ -0,0 +1,91 @@ +package org.luxons.sevenwonders.ui.redux.sagas + +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.promise +import redux.RAction +import redux.Store +import redux.WrapperAction +import redux.applyMiddleware +import redux.compose +import redux.createStore +import redux.rEnhancer +import kotlin.test.Test +import kotlin.test.assertEquals + +private data class State(val data: String) + +private data class UpdateData(val newData: String): RAction +private class DuplicateData: RAction +private class SideEffectAction(val data: String): RAction + +private fun reduce(state: State, action: RAction): State = when (action) { + is UpdateData -> State(action.newData) + is DuplicateData -> State(state.data + state.data) + else -> state +} + +private fun configureTestStore(initialState: State): TestRedux { + val sagaMiddlewareFactory = SagaManager<State, RAction, WrapperAction>() + val sagaMiddleware = sagaMiddlewareFactory.createMiddleware() + val enhancers = compose(applyMiddleware(sagaMiddleware), rEnhancer()) + val store = createStore(::reduce, initialState, enhancers) + return TestRedux(store, sagaMiddlewareFactory) +} + +private data class TestRedux( + val store: Store<State, RAction, WrapperAction>, + val sagas: SagaManager<State, RAction, WrapperAction> +) + +@OptIn(ExperimentalCoroutinesApi::class) +class SagaContextTest { + + @Test + fun dispatch(): dynamic = GlobalScope.promise { + + val redux = configureTestStore(State("initial")) + + redux.sagas.runSaga { + dispatch(UpdateData("Bob")) + } + + assertEquals(State("Bob"), redux.store.getState(), "state is not as expected") + } + + @Test + fun next(): dynamic = GlobalScope.promise { + val redux = configureTestStore(State("initial")) + + val job = redux.sagas.launchSaga(this) { + val action = next<SideEffectAction>() + dispatch(UpdateData("effect-${action.data}")) + } + + assertEquals(State("initial"), redux.store.getState()) + + redux.store.dispatch(SideEffectAction("data")) + job.join() + assertEquals(State("effect-data"), redux.store.getState()) + } + + @Test + fun onEach(): dynamic = GlobalScope.promise { + + val redux = configureTestStore(State("initial")) + + val job = redux.sagas.launchSaga(this) { + onEach<SideEffectAction> { + dispatch(UpdateData("effect-${it.data}")) + } + } + + assertEquals(State("initial"), redux.store.getState()) + + redux.store.dispatch(SideEffectAction("data")) + delay(50) + assertEquals(State("effect-data"), redux.store.getState()) + job.cancel() + } +} 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 new file mode 100644 index 00000000..d633f6f2 --- /dev/null +++ b/sw-ui/src/test/kotlin/org/luxons/sevenwonders/ui/utils/CoroutineUtilsTest.kt @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.ui.utils + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.promise +import kotlin.test.Test +import kotlin.test.assertEquals + +class CoroutineUtilsTest { + + @Test + fun awaitFirstTest(): dynamic = GlobalScope.promise { + val s = awaitFirst( + { delay(100); "1" }, + { delay(200); "2" } + ) + assertEquals("1", s) + val s2 = awaitFirst( + { delay(150); "1" }, + { delay(50); "2" } + ) + assertEquals("2", s2) + } +} |