flatMapLatest.lua 2.5 KB

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