From 5788c41c004b8efccfd64f96460f0fbe3d045852 Mon Sep 17 00:00:00 2001 From: Nikita Gazarov Date: Wed, 26 Jun 2024 00:34:58 -0700 Subject: [PATCH] Test: Add failing test for StreamFromSignal(changesOnly = False) --- .../airstream/core/PullResetSignalSpec.scala | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/test/scala/com/raquo/airstream/core/PullResetSignalSpec.scala b/src/test/scala/com/raquo/airstream/core/PullResetSignalSpec.scala index 6cf139a7..e8903a96 100644 --- a/src/test/scala/com/raquo/airstream/core/PullResetSignalSpec.scala +++ b/src/test/scala/com/raquo/airstream/core/PullResetSignalSpec.scala @@ -3,6 +3,7 @@ package com.raquo.airstream.core import com.raquo.airstream.UnitSpec import com.raquo.airstream.eventbus.EventBus import com.raquo.airstream.fixtures.{Calculation, Effect, TestableOwner} +import com.raquo.airstream.misc.StreamFromSignal import com.raquo.airstream.ownership.{DynamicOwner, DynamicSubscription, Owner, Subscription} import com.raquo.airstream.state.Var @@ -434,6 +435,75 @@ class PullResetSignalSpec extends UnitSpec { log.clear() } + it("signal.changes starts signal") { + val log = mutable.Buffer[String]() + + val bus = new EventBus[Int] + + var initialValue: Int = -1 + + def nextInitialValue: Int = { + initialValue += 1 + initialValue + } + + // This signal updates its current value every time it's + // started, as long as bus.events does not emit anything. + // This update happens in onWillStart, and is the simplest + // case to test. SwitchSignal also calls setCurrentValue + // from onWillStart. + val signal = bus + .events + .tapEach(v => log += s"bus-$v") + .startWith(nextInitialValue, cacheInitialValue = false) + .tapEach(v => log += s"signal-$v") + + val stream = + new StreamFromSignal(signal, changesOnly = true) + .tapEach(v => log += s"stream-$v") + + val owner = new TestableOwner + + log.toList shouldBe Nil + + // -- + + val sub0 = stream.addObserver(Observer.empty)(owner) + + log.toList shouldBe List( + "signal-0", + "stream-0" // #TODO: this event does not happen, but should + ) + log.clear() + + sub0.kill() + + // -- + + val sub1 = stream.addObserver(Observer.empty)(owner) + + log.toList shouldBe List( + "signal-1", + "stream-1" + ) + log.clear() + + sub1.kill() + + // -- + + val sub2 = stream.addObserver(Observer.empty)(owner) + + log.toList shouldBe List( + "signal-2", + "stream-2" + ) + log.clear() + + sub2.kill() + + } + it("onStart shared transaction is resilient to exceptions") { val accessAfterKillErrorMsg = "Accessing dynamic owner after it is killed"