ReduKt Test Thunk provides tools to test thunks. It's an extension of ReduKt Test and provides very similar API.
Testing thunks is very similar to testing middlewares described here, and you should start there. Simple examples below shows thunk testing flow:
- For
Thunk
:
val CustomThunk = Thunk<AppState> {
if (currentState.condition) dispatch(ActionA)
}
class CustomThunkTest {
private val tester = CustomThunk.tester(initialState = AppState())
@Test
fun test() = tester.test {
// given
currentState = AppState(condition = true)
// when
testExecute()
// then
assertSingleActionEquals(ActionA)
}
}
- For
CoThunk
(requiresrunTest
from kotlinx-coroutines-test):
val CustomCoThunk = CoThunk<AppState> {
if (currentState.condition) {
delay(1_000)
dispatch(ActionA)
}
}
class CustomCoThunkTest {
private val tester = CustomCoThunk.tester(initialState = AppState())
@Test
fun test() = runTest {
tester.test {
// given
currentState = AppState(condition = true)
// when
testExecute() // suspends until thunk function completes
// then
assertSingleActionEquals(ActionA)
}
}
}