summaryrefslogtreecommitdiff
path: root/sw-ui-kt/src/test/kotlin/org
diff options
context:
space:
mode:
authorJoffrey Bion <joffrey.bion@gmail.com>2019-08-08 22:57:57 +0200
committerjoffrey-bion <joffrey.bion@gmail.com>2019-10-22 22:17:05 +0200
commit91d1b6c735440699f32c8476e7016b0f08dd897c (patch)
tree4282632de23ce54f30843662ca1cf3cf1ad57f4e /sw-ui-kt/src/test/kotlin/org
parentUpgrade Kotlin & Kotlin Wrappers version (diff)
downloadseven-wonders-91d1b6c735440699f32c8476e7016b0f08dd897c.tar.gz
seven-wonders-91d1b6c735440699f32c8476e7016b0f08dd897c.tar.bz2
seven-wonders-91d1b6c735440699f32c8476e7016b0f08dd897c.zip
WIP sagas
Diffstat (limited to 'sw-ui-kt/src/test/kotlin/org')
-rw-r--r--sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt94
1 files changed, 94 insertions, 0 deletions
diff --git a/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt b/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt
new file mode 100644
index 00000000..d8efcc3a
--- /dev/null
+++ b/sw-ui-kt/src/test/kotlin/org/luxons/sevenwonders/ui/redux/sagas/SagasFrameworkTest.kt
@@ -0,0 +1,94 @@
+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>
+)
+
+@UseExperimental(ExperimentalCoroutinesApi::class)
+class SagaContextTest {
+
+ @Test
+ fun dispatch(): dynamic = GlobalScope.promise {
+
+ val redux = configureTestStore(State("initial"))
+
+ val saga = saga<State, RAction, WrapperAction> {
+ dispatch(UpdateData("Bob"))
+ }
+ redux.sagas.runSaga(saga)
+
+ assertEquals(State("Bob"), redux.store.getState(), "state is not as expected")
+ }
+
+ @Test
+ fun next(): dynamic = GlobalScope.promise {
+ val redux = configureTestStore(State("initial"))
+
+ val saga = saga<State, RAction, WrapperAction> {
+ val action = next<SideEffectAction>()
+ dispatch(UpdateData("effect-${action.data}"))
+ }
+ redux.sagas.startSaga(saga)
+
+ assertEquals(State("initial"), redux.store.getState())
+
+ redux.store.dispatch(SideEffectAction("data"))
+ delay(50)
+ assertEquals(State("effect-data"), redux.store.getState())
+ }
+
+ @Test
+ fun onEach(): dynamic = GlobalScope.promise {
+
+ val redux = configureTestStore(State("initial"))
+
+ val saga = saga<State, RAction, WrapperAction> {
+ onEach<SideEffectAction> {
+ dispatch(UpdateData("effect-${it.data}"))
+ }
+ }
+ val job = redux.sagas.startSaga(saga)
+
+ assertEquals(State("initial"), redux.store.getState())
+
+ redux.store.dispatch(SideEffectAction("data"))
+ delay(50)
+ assertEquals(State("effect-data"), redux.store.getState())
+ job.cancel()
+ }
+}
bgstack15