Skip to content

Commit

Permalink
Added additional tests for Action/ActionBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
harana-bot committed Oct 18, 2023
1 parent 147f6fd commit a194cac
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 10 deletions.
52 changes: 42 additions & 10 deletions diode-core/shared/src/test/scala/diode/CircuitTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ object CircuitTests extends TestSuite {
import CircuitTestsModel._

// actions
case class SetS(s: String)
case object NoChange extends diode.Action

case class SetSSilent(s: String)
case class SetS(s: String) extends diode.Action

case class SetD(d: Data)
case class SetSSilent(s: String) extends diode.Action

case class SetEffect(s: String, effect: () => Future[AnyRef])
case class SetD(d: Data) extends diode.Action

case class SetEffectOnly(effect: () => Future[AnyRef])
case class SetEffect(s: String, effect: () => Future[AnyRef]) extends diode.Action

case class Delay(action: Any)
case class SetEffectOnly(effect: () => Future[AnyRef]) extends diode.Action

case class ThrowAction(ex: Throwable)
case class SetEffectActionOnly(action: Action) extends diode.Action

case class SetEffectActionBatchOnly(effect: ActionBatch) extends diode.Action

case class Delay(action: Any) extends diode.Action

case class ThrowAction(ex: Throwable) extends diode.Action

class AppCircuit(implicit val ec: ExecutionContext) extends Circuit[Model] {
import diode.ActionResult._
Expand All @@ -37,7 +43,11 @@ object CircuitTests extends TestSuite {
case SetSSilent(s) =>
ModelUpdateSilent(model.copy(s = s))
case SetEffectOnly(effect) =>
ModelUpdateEffect(model, effect)
EffectOnly(effect)
case SetEffectActionOnly(action) =>
EffectOnly(Effect.action(action))
case SetEffectActionBatchOnly(actionBatch) =>
EffectOnly(Effect.action(actionBatch))
case SetD(d) =>
ModelUpdate(model.copy(data = d))
case SetEffect(s, effect) =>
Expand Down Expand Up @@ -77,8 +87,8 @@ object CircuitTests extends TestSuite {
}
"ActionBatch" - {
val c = circuit
c.dispatch(ActionBatch(SetS("First"), SetD(Data(43, false))))
assert(c.model.s == "First")
c.dispatch(ActionBatch(SetS("First"), SetD(Data(43, false)), SetS("Second")))
assert(c.model.s == "Second")
assert(c.model.data.i == 43)
assert(c.model.data.b == false)
}
Expand Down Expand Up @@ -345,6 +355,28 @@ object CircuitTests extends TestSuite {
c.dispatch(effect)
assert(effectRun == 1)
}
"EffectActionOnly" - {
val c = circuit
val effect = SetEffectActionOnly(SetS("Action1"))
c.dispatch(effect)
assert(c.model.s == "Action1")
}
"EffectActionBatchOnly" - {
val c = circuit
val ab = ActionBatch(SetS("Action2"), SetD(Data(100, true)))
val effect = SetEffectActionBatchOnly(ab)
c.dispatch(effect)
assert(c.model.s == "Action2")
assert(c.model.data.i == 100)
}
"EffectActionBatchOnlyNoChange" - {
val c = circuit
val ab = ActionBatch(NoChange, SetS("Action2"), SetD(Data(100, true)))
val effect = SetEffectActionBatchOnly(ab)
c.dispatch(effect)
assert(c.model.s == "Action2")
assert(c.model.data.i == 100)
}
}
"Processor" - {
"ModAction" - {
Expand Down
53 changes: 53 additions & 0 deletions diode-core/shared/src/test/scala/diode/EffectTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ object EffectTests extends TestSuite {
def efA = Effect(Future("A"))
def efB = Effect(Future("B"))
def efC = Effect(Future("C"))
def efD = Effect.action("D")
def efE = Effect.action("E")

"Effect" - {
"run" - {
Expand Down Expand Up @@ -116,5 +118,56 @@ object EffectTests extends TestSuite {
}
}
}
"EffectAction" - {
"run" - {
var x = ""
efD.run(y => x = y.asInstanceOf[String]).map { _ =>
assert(x == "D")
}
}
"toFuture" - {
efD.toFuture.map { z =>
assert(z == "D")
}
}
"map" - {
efD.map(x => s"$x$x").toFuture.map { z =>
assert(z == "DD")
}
}
"flatMap" - {
efD.flatMap(x => Future(s"$x$x")).toFuture.map { z =>
assert(z == "DD")
}
}
"after" - {
val now = System.currentTimeMillis()
efD.after(100.milliseconds)(diode.Implicits.runAfterImpl).map(x => s"$x$x").toFuture.map { z =>
assert(z == "DD")
assert(System.currentTimeMillis() - now > 80)
}
}
"+" - {
val e = efA + efD
val ai = new AtomicInteger(0)
e.run(x => ai.incrementAndGet()).map { _ =>
assert(ai.intValue() == 2)
}
}
">>" - {
val e = efA >> efB >> efC >> efD >> efE
var r = List.empty[String]
e.run(x => r = r :+ x.asInstanceOf[String]).map { _ =>
assert(r == List("A", "B", "C", "D", "E"))
}
}
"<<" - {
val e = efA << efB << efC << efD << efE
var r = List.empty[String]
e.run(x => r = r :+ x.asInstanceOf[String]).map { _ =>
assert(r == List("E", "D", "C", "B", "A"))
}
}
}
}
}

0 comments on commit a194cac

Please sign in to comment.