flatMapLatest.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. describe('flatMapLatest', function()
  2. it('produces an error if its parent errors', function()
  3. expect(Rx.Observable.throw():flatMapLatest()).to.fail()
  4. end)
  5. it('produces an error if the callback errors', function()
  6. expect(Rx.Observable.fromRange(3):flatMapLatest(error)).to.produce.error()
  7. end)
  8. it('unsubscribes from the source and the projected observable', function()
  9. local outerUnsubscribe = spy()
  10. local outerSubscription = Rx.Subscription.create(outerUnsubscribe)
  11. local outer = Rx.Observable.create(function(observer)
  12. observer:onNext()
  13. observer:onCompleted()
  14. return outerSubscription
  15. end)
  16. local innerUnsubscribe = spy()
  17. local innerSubscription = Rx.Subscription.create(innerUnsubscribe)
  18. local inner = Rx.Observable.create(function()
  19. return innerSubscription
  20. end)
  21. local subscription = outer:flatMapLatest(function() return inner end):subscribe()
  22. subscription:unsubscribe()
  23. expect(#innerUnsubscribe).to.equal(1)
  24. expect(#outerUnsubscribe).to.equal(1)
  25. end)
  26. it('uses the identity function as the callback if none is specified', function()
  27. local observable = Rx.Observable.fromTable({
  28. Rx.Observable.fromRange(3),
  29. Rx.Observable.fromRange(5)
  30. }):flatMapLatest()
  31. expect(observable).to.produce(1, 2, 3, 1, 2, 3, 4, 5)
  32. end)
  33. it('produces values from the most recent projected Observable of the source', function()
  34. local children = {Rx.Subject.create(), Rx.Subject.create()}
  35. local subject = Rx.Subject.create()
  36. local onNext = observableSpy(subject:flatMapLatest(function(i)
  37. return children[i]
  38. end))
  39. subject:onNext(1)
  40. children[1]:onNext(1)
  41. children[1]:onNext(2)
  42. children[1]:onNext(3)
  43. children[2]:onNext(10)
  44. subject:onNext(2)
  45. children[1]:onNext(4)
  46. children[2]:onNext(20)
  47. children[1]:onNext(5)
  48. children[2]:onNext(30)
  49. children[2]:onNext(40)
  50. children[2]:onNext(50)
  51. expect(onNext).to.equal({{1}, {2}, {3}, {20}, {30}, {40}, {50}})
  52. end)
  53. it('does not complete if one of the children completes', function()
  54. local subject = Rx.Subject.create()
  55. local flatMapped = subject:flatMapLatest(function() return Rx.Observable.empty() end)
  56. local _, _, onCompleted = observableSpy(flatMapped)
  57. subject:onNext()
  58. expect(#onCompleted).to.equal(0)
  59. subject:onNext()
  60. expect(#onCompleted).to.equal(0)
  61. subject:onCompleted()
  62. expect(#onCompleted).to.equal(1)
  63. end)
  64. end)