with.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. describe('with', function()
  2. it('returns the observable it is called on if no other sources are specified', function()
  3. local observable = Rx.Observable.fromRange(1, 5):with()
  4. expect(observable).to.produce(1, 2, 3, 4, 5)
  5. end)
  6. it('should produce the most recent values when the first observable produces a value', function()
  7. local subjectA = Rx.Subject.create()
  8. local subjectB = Rx.Subject.create()
  9. local onNext = spy()
  10. subjectA:with(subjectB):subscribe(Rx.Observer.create(onNext))
  11. subjectA:onNext('a')
  12. subjectA:onNext('b')
  13. subjectB:onNext('c')
  14. subjectB:onNext('d')
  15. subjectA:onNext('e')
  16. subjectA:onNext('f')
  17. expect(onNext).to.equal({{'a', nil}, {'b', nil}, {'e', 'd'}, {'f', 'd'}})
  18. end)
  19. it('should complete only when the first observable completes', function()
  20. local subjectA = Rx.Subject.create()
  21. local subjectB = Rx.Subject.create()
  22. local onCompleted = spy()
  23. subjectA:with(subjectB):subscribe(Rx.Observer.create(_, _, onCompleted))
  24. subjectA:onNext('a')
  25. subjectB:onNext('b')
  26. subjectB:onCompleted()
  27. expect(#onCompleted).to.equal(0)
  28. subjectA:onNext('c')
  29. subjectA:onNext('d')
  30. subjectA:onCompleted()
  31. expect(#onCompleted).to.equal(1)
  32. end)
  33. it('should unsubscribe from all source observables', function()
  34. local unsubscribeA = spy()
  35. local observableA = Rx.Observable.create(function(observer)
  36. return Rx.Subscription.create(unsubscribeA)
  37. end)
  38. local unsubscribeB = spy()
  39. local observableB = Rx.Observable.create(function(observer)
  40. return Rx.Subscription.create(unsubscribeB)
  41. end)
  42. local subscription = observableA:with(observableB):subscribe()
  43. subscription:unsubscribe()
  44. expect(#unsubscribeA).to.equal(1)
  45. expect(#unsubscribeB).to.equal(1)
  46. end)
  47. end)