combineLatest.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. describe('combineLatest', function()
  2. it('returns the observable it is called on if only the identity function is passed as an argument', function()
  3. local observable = Rx.Observable.fromRange(1, 5):combineLatest(function(x) return x end)
  4. expect(observable).to.produce(1, 2, 3, 4, 5)
  5. end)
  6. it('should call the combinator function with all values produced from all input observables once they have all produced a value', function()
  7. local observableA = Rx.Observable.fromValue('a')
  8. local observableB = Rx.Observable.fromValue('b')
  9. local observableC = Rx.Observable.fromValue('c')
  10. local combinator = spy()
  11. Rx.Observable.combineLatest(observableA, observableB, observableC, function(...) combinator(...) end):subscribe()
  12. expect(combinator).to.equal({{'a', 'b', 'c'}})
  13. end)
  14. it('should emit the return value of the combinator as values', function()
  15. local observableA = Rx.Subject.create()
  16. local observableB = Rx.Subject.create()
  17. local onNext = spy()
  18. Rx.Observable.combineLatest(observableA, observableB, function(a, b) return a + b end):subscribe(Rx.Observer.create(onNext))
  19. expect(#onNext).to.equal(0)
  20. observableA:onNext(1)
  21. observableB:onNext(2)
  22. observableB:onNext(3)
  23. observableA:onNext(4)
  24. expect(onNext).to.equal({{3}, {4}, {7}})
  25. end)
  26. it('should call onCompleted once all sources complete', function()
  27. local observableA = Rx.Subject.create()
  28. local observableB = Rx.Subject.create()
  29. local complete = spy()
  30. Rx.Observable.combineLatest(observableA, observableB, function() end):subscribe(nil, nil, complete)
  31. expect(#complete).to.equal(0)
  32. observableA:onNext(1)
  33. expect(#complete).to.equal(0)
  34. observableB:onNext(2)
  35. expect(#complete).to.equal(0)
  36. observableB:onCompleted()
  37. expect(#complete).to.equal(0)
  38. observableA:onCompleted()
  39. expect(#complete).to.equal(1)
  40. end)
  41. it('should call onError if one source errors', function()
  42. local observableA = Rx.Subject.create()
  43. local observableB = Rx.Subject.create()
  44. local errored = spy()
  45. Rx.Observable.combineLatest(observableA, observableB, function() end):subscribe(nil, errored)
  46. expect(#errored).to.equal(0)
  47. observableB:onError()
  48. expect(#errored).to.equal(1)
  49. end)
  50. it('should error if the combinator is absent', function()
  51. expect(Rx.Observable.combineLatest(Rx.Observable.fromRange(1, 3)).subscribe).to.fail()
  52. end)
  53. end)