Skip to content

Commit 38dbb8a

Browse files
author
Adrián García
authored
Add testing package (#2)
* Make the sample work * Test publish mini-rx and mini-rx-android * Add StateMerger and Activity extensions in rx package. Add Rx ViewModels in rx-android package * Move mini-rx packages to mini-rx2 to be more explicit about library version. Adjust depdendencies * Create test package * Apply kotlin plugin * Change class modules visibility * Add TestDispatcherInterceptor
1 parent 6da5d5b commit 38dbb8a

File tree

7 files changed

+93
-2
lines changed

7 files changed

+93
-2
lines changed

app/src/main/java/org/sample/SampleActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class SampleActivity : FluxActivity() {
3232

3333
dispatcher.dispatch(ActionTwo("2"))
3434
}
35-
}
35+
}

mini-testing/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

mini-testing/build.gradle

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apply plugin: 'kotlin'
2+
apply plugin: 'java-library'
3+
apply from: "../jitpack.gradle"
4+
5+
dependencies {
6+
implementation project(":mini-common")
7+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
8+
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
9+
10+
api "junit:junit:4.12"
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package mini.testing
2+
3+
import mini.Store
4+
import org.junit.rules.TestRule
5+
import org.junit.runner.Description
6+
import org.junit.runners.model.Statement
7+
8+
/**
9+
* [TestRule] that resets the state of each Store (retrieved via function) after an evaluation.
10+
*/
11+
class CleanStateRule(val storesFn: () -> List<Store<*>>) : TestRule {
12+
override fun apply(base: Statement, description: Description): Statement {
13+
return object : Statement() {
14+
fun reset() {
15+
val stores = storesFn()
16+
stores.forEach { it.resetState() }
17+
}
18+
19+
override fun evaluate() {
20+
reset()
21+
base.evaluate() //Execute the test
22+
reset()
23+
}
24+
}
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package mini.testing
2+
3+
import mini.BaseAction
4+
import mini.Chain
5+
import mini.Interceptor
6+
import java.util.*
7+
8+
/**
9+
* [Interceptor] class for testing purposes which mute all the received actions.
10+
*/
11+
internal class TestDispatcherInterceptor : Interceptor {
12+
override fun invoke(action: Any, chain: Chain): Any {
13+
println("Muted: $action")
14+
mutedActions.add(action)
15+
return TestOnlyAction
16+
}
17+
18+
private val mutedActions = LinkedList<Any>()
19+
20+
val actions: List<Any> get() = mutedActions
21+
}
22+
23+
/**
24+
* Action for testing purposes.
25+
*/
26+
internal object TestOnlyAction : BaseAction()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package mini.testing
2+
3+
import mini.Dispatcher
4+
import org.junit.rules.TestRule
5+
import org.junit.runner.Description
6+
import org.junit.runners.model.Statement
7+
8+
/**
9+
* This [TestRule] evaluates every action received with the [TestDispatcherInterceptor] to
10+
* intercept all the actions dispatched during a test and block them, getting them not reaching the store.
11+
*/
12+
class TestDispatcherRule(val dispatcherFn: () -> Dispatcher) : TestRule {
13+
private val testInterceptor = TestDispatcherInterceptor()
14+
val actions: List<Any> get() = testInterceptor.actions
15+
16+
override fun apply(base: Statement, description: Description): Statement {
17+
return object : Statement() {
18+
override fun evaluate() {
19+
val dispatcher = dispatcherFn()
20+
dispatcher.addInterceptor(testInterceptor)
21+
base.evaluate() //Execute the test
22+
dispatcher.removeInterceptor(testInterceptor)
23+
}
24+
}
25+
}
26+
27+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':mini-processor', ':mini-common', ':mini-android', ':mini-rx2', ':app', ':mini-rx2-android', ':mini-kodein', ':mini-kodein-android'
1+
include ':app', ':mini-processor', ':mini-common', ':mini-android', ':mini-rx2', ':mini-rx2-android', ':mini-kodein', ':mini-kodein-android', ':mini-testing'

0 commit comments

Comments
 (0)