flatMapLatest.lua 2.3 KB

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