asyncsubject.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. describe('AsyncSubject', function()
  2. describe('create', function()
  3. it('returns an AsyncSubject', function()
  4. expect(Rx.AsyncSubject.create()).to.be.an(Rx.AsyncSubject)
  5. end)
  6. it('does not fail if passed arguments', function()
  7. expect(Rx.AsyncSubject.create(1, 2, 3)).to.be.an(Rx.AsyncSubject)
  8. end)
  9. end)
  10. describe('subscribe', function()
  11. describe('if the AsyncSubject has already completed', function()
  12. it('calls onNext if the AsyncSubject has a value and calls onCompleted', function()
  13. local subject = Rx.AsyncSubject.create()
  14. subject:onNext(5)
  15. subject:onCompleted()
  16. local onNext, onError, onCompleted = spy(), spy(), spy()
  17. subject:subscribe(onNext, onError, onCompleted)
  18. expect(onNext).to.equal({{5}})
  19. expect(#onError).to.equal(0)
  20. expect(#onCompleted).to.equal(1)
  21. end)
  22. it('calls onError on the Observer if the AsyncSubject has an error', function()
  23. local subject = Rx.AsyncSubject.create()
  24. subject:onError('ohno')
  25. local onNext, onError, onCompleted = spy(), spy(), spy()
  26. subject:subscribe(onNext, onError, onCompleted)
  27. expect(#onNext).to.equal(0)
  28. expect(onError).to.equal({{'ohno'}})
  29. expect(#onCompleted).to.equal(0)
  30. end)
  31. end)
  32. describe('if the AsyncSubject has not completed', function()
  33. it('returns a subscription', function()
  34. expect(Rx.AsyncSubject.create():subscribe()).to.be.an(Rx.Subscription)
  35. end)
  36. end)
  37. end)
  38. describe('onNext', function()
  39. it('does not push values to subscribers', function()
  40. local observer = Rx.Observer.create()
  41. local subject = Rx.AsyncSubject.create()
  42. local function run()
  43. subject:onNext(1)
  44. subject:onNext(2)
  45. subject:onNext(3)
  46. end
  47. expect(#spy(observer, '_onNext', run)).to.equal(0)
  48. end)
  49. end)
  50. describe('onError', function()
  51. it('pushes errors to all subscribers', function()
  52. local observers = {
  53. Rx.Observer.create(nil, function() end),
  54. Rx.Observer.create(nil, function() end)
  55. }
  56. local spies = {
  57. spy(observers[1], '_onError'),
  58. spy(observers[2], '_onError')
  59. }
  60. local subject = Rx.AsyncSubject.create()
  61. subject:subscribe(observers[1])
  62. subject:subscribe(observers[2])
  63. subject:onError('ohno')
  64. expect(spies[1]).to.equal({{'ohno'}})
  65. expect(spies[2]).to.equal({{'ohno'}})
  66. end)
  67. end)
  68. describe('onCompleted', function()
  69. it('pushes the last value to all Observers if one is present then calls onCompleted', function()
  70. local observers = {}
  71. local spies = {}
  72. for i = 1, 2 do
  73. observers[i] = Rx.Observer.create()
  74. spies[i] = {}
  75. spies[i].onNext = spy(observers[i], '_onNext')
  76. spies[i].onError = spy(observers[i], '_onError')
  77. spies[i].onCompleted = spy(observers[i], '_onCompleted')
  78. end
  79. local subject = Rx.AsyncSubject.create()
  80. subject:subscribe(observers[1])
  81. subject:subscribe(observers[2])
  82. subject:onNext(1)
  83. subject:onNext(2)
  84. subject:onCompleted()
  85. for i = 1, 2 do
  86. expect(spies[i].onNext).to.equal({{2}})
  87. expect(#spies[i].onError).to.equal(0)
  88. expect(#spies[i].onCompleted).to.equal(1)
  89. end
  90. end)
  91. end)
  92. end)