switch.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. describe('switch', function()
  2. it('errors when the source errors', function()
  3. expect(Rx.Observable.throw():switch()).to.produce.error()
  4. end)
  5. it('errors when an Observable produced by the source errors', function()
  6. local observable = Rx.Observable.create(function(observer)
  7. observer:onNext(Rx.Observable.throw())
  8. observer:onCompleted()
  9. end)
  10. expect(observable:switch()).to.produce.error()
  11. end)
  12. it('produces the values produced by the latest Observable produced by the source', function()
  13. local a = Rx.Subject.create()
  14. local b = Rx.Subject.create()
  15. local c = Rx.Subject.create()
  16. local onNext, onError, onCompleted = observableSpy(a:switch())
  17. b:onNext(1)
  18. a:onNext(b)
  19. b:onNext(2)
  20. b:onNext(3)
  21. c:onNext(7)
  22. a:onNext(c)
  23. b:onNext(4)
  24. c:onNext(8)
  25. b:onCompleted()
  26. c:onNext(9)
  27. c:onCompleted()
  28. a:onCompleted()
  29. expect(onNext).to.equal({{2}, {3}, {8}, {9}})
  30. expect(#onError).to.equal(0)
  31. expect(#onCompleted).to.equal(1)
  32. end)
  33. it('should unsubscribe from inner subscription too', function()
  34. local unsubscribeA = spy()
  35. local observableA = Rx.Observable.create(function(observer)
  36. return Rx.Subscription.create(unsubscribeA)
  37. end)
  38. local subject = Rx.Subject.create()
  39. local subscription = subject:switch():subscribe()
  40. subject:onNext(observableA)
  41. subscription:unsubscribe()
  42. expect(#unsubscribeA).to.equal(1)
  43. end)
  44. end)